var menuOver = false;
var pwdFieldClear = '<input name="data[Merchant][password]" type="text" class="password-field" id="password" value="Password" />';
var pwdFieldObscure = '<input name="data[Merchant][password]" type="password" class="password-field" id="password" value="" />';

$(document).ready(function(){
	
	$.ajaxSetup({cache: false});
	
	//$('div.disabled').parent('div').hide();
	
	if($.browser.msie && parseInt($.browser.version) == 6) {
		//$(document).pngFix();
	}
	
	$('.date-pick').datepicker({
		inline: true
	});
	
	$("div.navi span.depth_0 a").hoverIntent(
	  function () {
		$(this).parent("span").next('div').fadeTo("fast", 0.90).addClass('show-menu');
	  }, 
	  function () {
		  if(!menuOver) {
			  $(this).parent("span").next('div').fadeTo("fast", 0.00).removeClass('show-menu');
		  }
	  }
	);
	
	$("div.navi div div div").hover(
	  function () {
		  menuOver = true;
	  }, 
	  function () {
		  menuOver = false;
		  $(this).removeClass('show-menu');
	  }
	);
	
	$('div.home-second').show();
	$('#mycarousel').jcarousel({
		scroll: 4,
		wrap: 'both',
		initCallback: homeCarouselLoaded
    });
	
	$('#mycarousel2').jcarousel({
		scroll: 3,
		wrap: 'both'					
    });
	
	$('#mycarousel3').jcarousel({
		scroll: 1,
		wrap: 'both'
    });
	
	$('#sale_message').change(function () {
		if($(this).val() == 'Other') {
			$('#sale_message_other_contain').slideDown();
		} else {
			$('#sale_message_other_contain').slideUp();
		}
	});
	$('#sale_message').change();
	
	$('.btn-test-image').click(function () {
		if($('#image_url').val() != 'http://' && $('#image_url').val() != '') {
			window.open($('#image_url').val());
		} else {
			alert('Please enter an image URL.');	
		}
		return false;
    });
	
	var cycle_paused = false;
	$('.cycle').cycle({ 
	    fx:     'scrollLeft', 
	    timeout: 5000,
	    before: beforeSlide
	 });

	$('ul#slide-nav a').click(function () {
		var index = $("ul#slide-nav li").index($(this).parent('li'));
		$('ul#slide-nav a').removeClass('active');
		$(this).addClass('active');
		$('.cycle').cycle(index);
		$('.cycle').cycle('pause');
		cycle_paused = true;
    });
	
	$('a.btn-playpause').click(function () {
		if(cycle_paused) {
			$('.cycle').cycle('resume');
			$(this).find('img').attr('src','/images/btn-first-navi-pause.gif');
			cycle_paused = false;
		} else {
			$('.cycle').cycle('pause');
			$(this).find('img').attr('src','/images/btn-first-navi.gif');
			cycle_paused = true;
		}
	});
	
	$('table#subscription-selector select').change(function () {
		var selected = $(this).val();
		$('table#subscription-selector select').val($('option:first', this).val());
		$(this).val(selected);
    });
	
	$('#email_address').focus(function(){
	  if($(this).val() == 'E-mail Address') {
		  $(this).attr('value','');
	  }
	});
	$('#email_address').blur(function(){
	  if($(this).val() == '') {
		  $(this).val('E-mail Address');
	  }
	});
	
	$('a.autoLinkHelp').cluetip({
    	showTitle: false
  	});
	
	$('a.btn-change-category').click(function () {
		$('#subcat_contain').show();
		$('#subcat_selected_contain').hide();
	});	
	
	checkCategorySelection();
	createAndBindCategorySelects();
	bindPasswordFocus();
	bindPasswordBlur();
	
	$('.subcats select').change();
	
	$('#product_name').keyup(function(){
 		limitChars('product_name', 50, 'product_name_count');
 	});

	$('#short_description').keyup(function(){
 		limitChars('short_description', 200, 'short_description_count');
 	});
	
	$('#long_description').keyup(function(){
 		limitChars('long_description', 1000, 'long_description_count');
 	});
	
	$('#product_name').keyup();
	$('#short_description').keyup();
	$('#long_description').keyup();
	
	$('div.required label').append('*');
	
	$('#q').focus(function(){
	  if($(this).val() == 'Search') {
		  $(this).attr('value','');
	  }
	});	
	
});

function homeCarouselLoaded(c) {
	
}

function limitChars(textid, limit, infodiv)
{
	var text = $('#'+textid).val();	
	var textlength = text.length;
	if(textlength > limit)
	{
		$('#' + infodiv).html('You cannot write more than '+limit+' characters!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else
	{
		$('#' + infodiv).html((limit - textlength) +' characters left.');
		return true;
	}
}


function checkCategorySelection() {
	if($('#product_category_id').get(0)) {
		if($('#product_category_id').val().length > 0 && $('#product_category_id').val() != '0') {
			$('#subcat_contain').hide();
			$('#subcat_selected_contain').show();
		}
	}
}

function createAndBindCategorySelects() {
	$('.subcats select').change(function () {
		var selected = $(this).val();
		var current = this;
		if(selected.length > 0) {
			$.getJSON("/products/get_categories_by_parent_id/"+selected+"/"+new Date().getTime(), function(json){
				if(json.length > 0) {
					var options = "";
					var new_name = "";
					options += '<option value="">Please select...</option>';
					for (var i = 0; i < json.length; i++) {
						options += '<option value="'+json[i].id+'">'+json[i].title+'</option>';
					}
					$(current).parent('div').nextAll().remove();
					var select_html = '<div class="select"><br /><label>Select the Product Sub-Category</label><select class="subcat_select">';
					select_html += options;
					select_html += '</select></div>';
					$('.subcats').append(select_html);
					createAndBindCategorySelects();
				} else {
					$('#product_category_id').val(selected);
					$(current).css('border','1px solid green');
				}
			});
		}
    });
}

function bindPasswordFocus() {
	$('#password').focus(function(){
		if($(this).val() == 'Password') {
			$(this).replaceWith(pwdFieldObscure);
			$('#password').focus();
		}
		bindPasswordBlur();
	});
}

function bindPasswordBlur() {
	$('#password').blur(function(){
		if($(this).val() == '') {
			$(this).replaceWith(pwdFieldClear);
			bindPasswordFocus();
			bindPasswordBlur();
		}
	});
}

function beforeSlide() {
	var index = $("div.cycle div").index(this);
	if(index >= 0) {
		$('ul#slide-nav a').removeClass('active');
		$($('ul#slide-nav li').get(index)).find('a').addClass('active');
	}
}
