/*!
 * jQuery FancySelect Plugin
 * version: 1.0 (16-APRIL-2010)
 * @requires jQuery v1.3.2 or later
 *
 * Author: Rajiv Gonsalves
 * (c) iProgrammer Solutions Pvt Ltd. 2010
 */
;(function($) {
	$.fn.fancySelect = function() {
		var functions = new Array();
		$(this).each(function() {
			var s_class = $(this).attr('class');
			var name = $(this).attr('name');
			var width = (parseInt($(this).css('width'))) ? parseInt($(this).css('width')) : $(this).width();
			
			var id = name + '_'+ Math.round(Math.random()*1000);
			var html = '<div id="jSelect_'+id+'" class="jSelect '+s_class+'" name="'+name+'" type="jSelect"><div class="holder"><div class="pointer">&nbsp;</div><div class="jSelectCurrent"></div></div><div class="jSelectOptions" style="display:none">';
			$(this).find('option').each(function() {
				var value = $(this).attr('value');
				var text = $(this).text();
				var selected = ($(this).attr('selected')) ? ' selected = "selected" ' : '';		
				html += '<div class="jSelectOption" value="'+value+'" '+selected+'>'+text+'</div>';
			});
			html += '</div></div>';
			this.$html = $(html);

			var input_html = "<input type='hidden' name='"+name+"' value='' />";
			this.$html.prepend(input_html);
			this.$html.find('.jSelectOptions').attr('state', 'hide');
			this.$html.find('.jSelectOptions').attr('isfocus', 'no');
	


			
			var currentText = this.$html.find('.jSelectOptions').find('.jSelectOption').eq(0).html();
			var currentValue = this.$html.find('.jSelectOptions').find('.jSelectOption').eq(0).attr('value');

			this.$html.find('.jSelectOptions').find('.jSelectOption').each(function() {
				if ($(this).attr('selected') == 'selected') {
					currentText = $(this).html();
					currentValue = $(this).attr('value');
					return false;
				}
			});
			
		

			this.$html.find('.jSelectCurrent').attr('value', currentValue);
			this.$html.find('.jSelectCurrent').html(currentText);
			this.$html.find('input[name='+name+']').val(currentValue);
			
			this.$html.mouseenter(function() {
				if ($(this).find('.jSelectOptions').attr('state') == 'hide' && $(this).find('.jSelectOptions').attr('isfocus') == 'yes') {
					$(this).find('.jSelectOptions').attr('state', 'show');
					$(this).find('.jSelectOptions').slideDown();
				}
			});
			this.$html.find('.jSelectOption').mouseenter(function() {
				$(this).addClass('hover');
			});
			this.$html.find('.jSelectOption').mouseleave(function() {
				$(this).removeClass('hover');
			});


			this.$html.click(function() {
				if ($(this).find('.jSelectOptions').attr('state') == 'hide') {
					$(this).find('.jSelectOptions').attr('isfocus', 'yes');
					$(this).find('.jSelectOptions').mouseenter();
				}
			});

			this.$html.mouseleave(function() {
				if ($(this).find('.jSelectOptions').attr('state') == 'show') {
					$(this).find('.jSelectOptions').slideUp('fast', function() {
						$(this).attr('state', 'hide');	
						$(this).attr('isfocus', 'no');
					});
				}
			});
			
			this.$html.find('.jSelectOptions').find('.jSelectOption').each(function() {
				$(this).click(function() {
					$(this).parents('.jSelect').find('.jSelectCurrent').attr('value', $(this).attr('value'));
					$(this).parents('.jSelect').find('input[name='+name+']').val($(this).attr('value'));
					$(this).parents('.jSelect').find('.jSelectCurrent').html($(this).html());
					$(this).parents('.jSelect').find('.jSelectOptions').find('.jSelectOption').removeAttr('selected');
					$(this).attr('selected', 'selected');
					$(this).parents('.jSelect').change();
					$(this).parents('.jSelect').mouseleave();
				});
			});
			$(this).replaceWith(this.$html);
			var var_width = width;
			$('#jSelect_'+id).width(var_width);
			$('#jSelect_'+id).children().css('padding', '0px');
			$('#jSelect_'+id).children().css('margin', '0px');
			$('#jSelect_'+id).find('.jSelectCurrent').css('overflow', 'hidden');
			$('#jSelect_'+id).find('.jSelectCurrent').css('display', 'inline-block');
			$('#jSelect_'+id).find('.pointer').css('display', 'inline-block');
			$('#jSelect_'+id).find('.holder').css('display', 'inline-block');
			if ($.browser.msie === true) {
				$('#jSelect_'+name).find('.jSelectOptions').css('margin-left', '2px');
			}
			$('#jSelect_'+id).find('.holder').width(var_width);
			$('#jSelect_'+id).find('.jSelectCurrent').width(function() {

				return $(this).parent().width() - $(this).parent().find('.pointer').width()-10;	
				
			});
			
			$('#jSelect_'+id).find('.jSelectOptions').css('width', function() {
				return $(this).parent().find('.holder').width() + 'px';	
			});

		});
	}
	$.fn.j_change = function(func) {
		$(this).each(function() {
			var name = $(this).attr('name');
			alert(name);
		});
		
	}
	$.fn.set_options = function(data) {		
			html = '<div class="jSelectOption" value="0">Please select...</div>'
			for (i=0;i<data.length ;i++ ) {
				var value = data[i][0];
				var text = data[i][1];
				html += '<div class="jSelectOption" value="'+value+'">'+text+'</div>';
			}
			html += '</div>';
			
			$(this).find('.jSelectCurrent').attr('value', 0);
			$(this).find('.jSelectCurrent').html('Please select...');
			$(this).find('.jSelectOptions').html(html);
			$(this).find('.jSelectOption').mouseenter(function() {
				$(this).addClass('hover');
			});
			$(this).find('.jSelectOption').mouseleave(function() {
				$(this).removeClass('hover');
			});

			$(this).find('.jSelectOptions').find('.jSelectOption').each(function() {
				$(this).click(function() {
					$(this).parents('.jSelect').find('.jSelectCurrent').attr('value', $(this).attr('value'));
					$(this).parents('.jSelect').find('input[name='+$(this).parents('.jSelect').attr('name')+']').val($(this).attr('value'));
					$(this).parents('.jSelect').find('.jSelectCurrent').html($(this).html());
					$(this).parents('.jSelect').find('.jSelectOptions').find('.jSelectOption').removeAttr('selected');
					$(this).attr('selected', 'selected');
					$(this).parents('.jSelect').change();
					$(this).parents('.jSelect').mouseleave();
				});
			});
	};
	$.fn.org_val = $.fn.val;
	$.fn.val = function(value) {
		if ($(this).attr('type') == 'jSelect')	{
			if ( value === undefined ) {
				var return_value = $(this).find('.jSelectOptions').find('.jSelectOption').eq(0).attr('value')
				$(this).find('.jSelectOptions').find('.jSelectOption').each(function() {
					if ($(this).attr('selected') == 'selected') {
						return_value = $(this).attr('value');

					}
				});
				return return_value;
			}
			else {
				var name = $(this).attr('name');
				$(this).find('.jSelectOptions').find('.jSelectOption').each(function() {
					if ($(this).attr('value') == value) {
		
						var text = $(this).html();
						$(this).attr('selected', 'selected')
						$(this).parents('.jSelect').find('.jSelectCurrent').attr('value', value);
						$(this).parents('.jSelect').find('.jSelectCurrent').html(text);
						$(this).parents('.jSelect').find('input[name='+name+']').org_val(value);
					}
					else {
						$(this).removeAttr('selected');
					}
					return true;
				});
				
			}
		}
		else {
			return $(this).org_val(value);
		}
	}
})(jQuery);

$(document).ready(function() {
	$(this).find('select').fancySelect();


	$("ul.sf_menu li").mouseenter(function() {
		if ($(this).find('div').size()) {
			$(this).find('div').fadeIn();
		}
	});

	$("ul.sf_menu li").mouseleave(function() {
		if ($(this).find('div').size()) {
			$(this).find('div').fadeOut();
		}
	});
	$('.watermark').each(function() {
		var watermark = $(this).attr('watermark');
		$(this).val(watermark);
		$(this).addClass('watermarked');
		$(this).focus(function() {
			if ($(this).val() == watermark) {
				$(this).removeClass('watermarked');
				$(this).val('');
			}
		});

		$(this).blur(function() {
			if ($(this).val() == '') {
				$(this).addClass('watermarked');
				$(this).val(watermark);
			}
		});
	});

	$('.more_details_link').click(function() {
			var box = $(this).parents('.offer_row:first').find('.shippingInformationMain');

			if ($(box).is(':visible')) {
				$(box).slideUp();
			}
			else {
				$(box).slideDown();
			}
	return false;
	});

});



