jQuery.fn.exists = function(){return jQuery(this).length>0;}
$(document).ready(function(){
	$.ajax({

		url: ("https:" == document.location.protocol) ? "https://www.janilink.com/ajaxShoppingCart.php" : "http://www.janilink.com/ajaxShoppingCart.php" , 
		success: function(data){
			$('div#myCart__Top').html(data);
		}
	})
	// [BOF] SEARCH BAR
	function checkSearchBox()
	{
		if($('#searchBox__Top input').attr('value'))
			$('#searchBox__Top input').css('background-image','none');
		else
			$('#searchBox__Top input').css('background-image','url("../tim/keyword-btn-bg.jpg")');
	}
	$('#searchBox__Top input').change(function(){
		checkSearchBox();
	});
	// [EOF] SEARCH BAR


	$('div#cartBlock').show('slow');
	// [BOF] Top Login Block Event - FLAG
		var topAndLeft = 'top:20px;left:-202px;';
		var inbox_serviceTime_eng = '<font color="#b2060e">Service Time</font><br /><font color="#19718b">- MON-FRI : 9AM ~ 6PM<br />- SAT : 9AM ~ 3PM<br />- SUN : Closed<br />';
		var inbox_serviceTime_mex = '<font color="#b2060e">Servicios Tiempo</font><br /><font color="#19718b">- LUN-VIE : 9AM ~ 6PM<br />- SAB : 9AM ~ 3PM<br />- DOM : Closed<br />';
		var inbox_serviceTime_kor = '<font color="#b2060e">Service Time</font><br /><font color="#19718b">- 월-금 : 9AM ~ 6PM<br />- 토 : 9AM ~ 3PM<br />- 일 : 휴무<br />';
		$('div#flagEnglish').mouseover(function(){
			$('div#flagEnglish__Box').html('<div class="flagBox" style="'+topAndLeft+'"><font color="black"><b>Call Anyone</b></font><br />'+inbox_serviceTime_eng+'</font></div>');
			//$('div#flagEnglish__Box div').show('fast');

			$('div#flagEnglish__Box div').animate({
				left: '+=30',
				height: 'toggle'
			},200, function() {
				// Animation complete.
			});
		}).mouseout(function(){
			$('div#flagEnglish__Box div').animate({
				left: '+=30',
				height: 'toggle'
			},200, function() {
				$('div#flagEnglish__Box').html('');
			});		
		});

		$('div#flagMexican').mouseover(function(){
			$('div#flagMexican__Box').html('<div class="flagBox" style="'+topAndLeft+'"><font color="black"><b>Español - Llamar Camilo</b></font><br />'+inbox_serviceTime_mex+'</div>');
			$('div#flagMexican__Box div').animate({
				left: '+=30',
				height: 'toggle'
			},200, function() {
				// Animation complete.
			});
		}).mouseout(function(){
			$('div#flagMexican__Box div').animate({
				left: '+=30',
				height: 'toggle'
			},200, function() {
				$('div#flagMexican__Box').html('');
			});
		});

		$('div#flagKorean').mouseover(function(){
			$('div#flagKorean__Box').html('<div class="flagBox" style="'+topAndLeft+'"><font color="black"><b>한국말 서비스 - 민부장</b></font><br />'+inbox_serviceTime_kor+'</div>');
			$('div#flagKorean__Box div').animate({
				left: '+=30',
				height: 'toggle'
			},200, function() {
				// Animation complete.
			});
		}).mouseout(function(){
			$('div#flagKorean__Box div').animate({
				left: '+=30',
				height: 'toggle'
			},200, function() {
				$('div#flagKorean__Box').html('');
			});		
		});

	// [EOF] Top Login Block Event
	// [BOF] PRODUCTS RATING
	if($('input#ratingSum').exists())
	{
		var ratingWidth;
		if($('input#ratingSum').attr('value') != null)
		{

			switch ($('input#ratingSum').attr('value'))
			{
				case "0.5":	ratingWidth = 17;	break;
				case "1":	ratingWidth = 35;	break;
				case "1.5":	ratingWidth = 52;	break;
				case "2":	ratingWidth = 69;	break;
				case "2.5":	ratingWidth = 86;	break;
				case "3":	ratingWidth = 103;	break;
				case "3.5":	ratingWidth = 120;	break;
				case "4":	ratingWidth = 137;	break;
				case "4.5":	ratingWidth = 153;	break;
				case "5":	ratingWidth = 178;	break;
			}
			if($('input#ratingSum').attr('value') != 0)
			{
				$('#ratingProduct').fadeIn('slow');
				$('div#ratingColorStar').animate({"width" : "+="+ratingWidth}, 1500);
			}
		}
	}

	
	// [EOF] PRODUCTS RATING

	
	// [BOF] THUMBNAILS - DETAIL PAGE

	$('div#thumbsBlockP_Left').mouseover(function(){$(this).animate({opacity:'0.5'},200)
	}).mouseout(function(){	$(this).animate({opacity:'1'},200) });
	$('div#thumbsBlockP_Right').mouseover(function(){$(this).animate({opacity:'0.5'},200)
	}).mouseout(function(){ $(this).animate({opacity:'1'},200) });
	
	if($('div#thumbsBlockP').exists())
	{
		var amountMove_Thumb = 60;
		var Thumblist_width = $('div#thumbsImgList div').width() -  $('div#thumbsImgList').width();
		var rightLimit = Thumblist_width;
		var leftLimit = 0;
	}
	$('div#thumbsBlockP_Left').click(function(){
		if(leftLimit > 0)
		{
			leftLimit = leftLimit - amountMove_Thumb;
			rightLimit = rightLimit + amountMove_Thumb;
			$('div#thumbsImgList div').animate({left:'+='+amountMove_Thumb+'px'});
		}
		
	});
	$('div#thumbsBlockP_Right').click(function(){
		if(rightLimit > 0)
		{
			leftLimit = leftLimit + amountMove_Thumb;
			rightLimit = rightLimit - amountMove_Thumb;
			$('div#thumbsImgList div').animate({left:'-='+amountMove_Thumb+'px'});
		}
	});
	// [EOF] THUMBNAILS - DETAIL PAGE
	

	// [BOF] CHECKOUT PAGE - CREDIT CARD CHECK
	$('input[name=cc_number]').keyup(function(){
		if($(this).val().match(/^[6][0][1][1]/gi))
		{
			alert('We do not accept Discover Card.');
			$(this).val('');
		}
	});


	


	// AJAX SEARCH TIP

	$(document).keyup(function(e){
		if($('#searchBoxTip_con div').exists())
		{
			var selected = false;
			if(e.which == 38)
			{
				$('#searchBoxTip_con div').each(function(key,value){
						if($(this).hasClass('searchBoxTip_Hover'))
						{
							if(!selected)
								if($(this).prev().prev().text() != null)
									$('#txtSearch').val($(this).prev().prev().text());

							if($(this).prev().text() != null)
								$('#txtSearch').val($(this).prev().text());
							$(this).removeClass('searchBoxTip_Hover').addClass('searchBoxTip_Out');								$(this).prev().removeClass('searchBoxTip_Out').addClass('searchBoxTip_Hover');

							selected = true;
							
							return false;
						}
				});
				if(!selected)
				{
					var lastBox = $('#searchBoxTip_con div:last');
					lastBox.removeClass('searchBoxTip_Out').addClass('searchBoxTip_Hover');
					$('#txtSearch').val($('#searchBoxTip_con div:last').text());
				}
			}
			if(e.which == 40)
			{
				$('#searchBoxTip_con div').each(function(){
					if($(this).hasClass('searchBoxTip_Hover'))
					{
						if($(this).next().text() != null)
							$('#txtSearch').val($(this).next().text());
						$(this).removeClass('searchBoxTip_Hover').addClass('searchBoxTip_Out');
						$(this).next().removeClass('searchBoxTip_Out').addClass('searchBoxTip_Hover');
						selected = true;
						return false;
					}
				});
				if(!selected)
				{
					var lastBox = $('#searchBoxTip_con div:first');
					lastBox.removeClass('searchBoxTip_Out').addClass('searchBoxTip_Hover');
					$('#txtSearch').val($('#searchBoxTip_con div:first').text());
				}
			}

			
		}
	});
	$('#txtSearch').keyup(function(e){

		if(e.which > 47 && e.which < 91 || e.which == 8 || e.which == 46)
		{
			if(!$('#searchBoxTip').exists())
				$('#txtSearch').parent().append('<div id="searchBoxTip"></div>');
			$.ajax({
				url: ("https:" == document.location.protocol) ? "https://www.janilink.com/ajaxSearchBox.php" : "http://www.janilink.com/ajaxSearchBox.php" , 
				cache:false,
				data:'searchKeyword='+$('#txtSearch').val(),
				type:'post',
				success:function(data){

					if($('#txtSearch').val() != '')
					{
						$('#searchBoxTip').html(data);
						$('[class=searchBoxTip_Oneline]').livequery(function(event){
							$(this).hover(function(){
								$(this).removeClass('searchBoxTip_Out').addClass('searchBoxTip_Hover');
							}, function(){
								$(this).removeClass('searchBoxTip_Hover').addClass('searchBoxTip_Out');
							});
						});
						
						var KeywordTxt = $('#txtSearch').val().split(" ");

						$('#searchBoxTip_con div').each(function(){
							for(var ii=0 in KeywordTxt)
							{
								var rgEx = new RegExp(KeywordTxt[ii],"gi");
								$(this).html($(this).text().replace(rgEx,'<span class="bold">'+KeywordTxt[ii]+'</span>'));
							}
						});
					}
				}
			});
		}
		if($('#txtSearch').val() == '')
			$('#searchBoxTip_con').remove();
	});

	$('#txtSearch').focusout(function(e){
		$('#searchBoxTip_con div').each(function(){
			if($(this).hasClass('searchBoxTip_Hover'))
				$('#txtSearch').val($(this).text());
		});
		$('#searchBoxTip_con').remove();
	});

	//Loginbox Enter
	$('[class=loginSecBox] input').keyup(function(e){
		if(e.which == 13)
			document.login.submit();	
	});

	$('.thumbP').livequery('click',function(){
		var imgM = $(this).attr('src').replace(/img\/p\/T/,'img/p/M');
		var imgO = $(this).attr('src').replace(/img\/p\/T/,'img/p/O');
		var imgO = imgO.replace(/img\/p\/M/,'img/p/O');
		$('#productMainIMG').html('<a href="'+imgO+'" class="thickbox"><img src="'+imgM+'" width="150" height="184"></a>');
		tb_init('a.thickbox, area.thickbox, input.thickbox');
	});

	/*	Cleaning tips subscription	*/
	$('#subscribeEmail_Btn').livequery('click',function(){
		var emailAddr = $('#subscribeEmail_Field').val();
		if(emailAddr.match(/@/) && emailAddr != '')
		{
			$.ajax({
				type: "POST",
				url: "ajaxProcess.php", 
				data: 'emailAddr='+emailAddr,
				success: function(data){
					if(data.match(/success/))
					{
						$('#subscribeEmail_Field').val('');
						alert('Thanks!');
					}
					else if(data.match(/duplicated/))
						alert('Your email is already added to our list.');
					else
						alert('We are sorry please try again later.');
				}
			});
		}
		else
			alert('Please check your email address.');
	});

	$('#topBlock .t2 .ts2').fadeIn(2000);
	$(".childDivCat .dd2").hover(function(){

		var img = $(this).children('input').val();
		var objectW = $(this).parent('.childDivCat').width() + 250;

		if(img != '')
			img = 'images/'+img;
		else
			img = 'img/template/no-category-image.jpg';




		
		$(this).parent().parent().append('<img src="'+img+'" style="left:'+objectW+'px;display:none;margin-left:5px;position:absolute;" class="ct_pop" />')
		$('.ct_pop').animate({
			width:'89px',
			height:'toggle'
		},100);

	},function(){
		$('.ct_pop').remove();
	});

	$('#searchBtn_Left').livequery('click',function(){
		document.quickfind.submit();
	});

	$("#leftCatBlock div").hover(function(){

		if($(this).attr('id').match(/parentCat_/gi))
		{
			$("div", this).show(); 
			$(this).children('.clnk').css('background-image','url("img/template/hover-category-mn.jpg")'); 
			$(this).children('.clnk').css('color','#004691');
		}

	},function(){
		if($(this).attr('id').match(/parentCat_/gi))
		{
			$("div", this).hide(); 
			$(this).children('.clnk').css('background-image','none'); 
		}

	});
});