jQuery.fn.exists = function(){return jQuery(this).length>0;}
var protocol = (("https:" == document.location.protocol) ? "https://" : "http://");
var domain = "www.janilink.com/";
/*FOR IE 6 or 7 NOTIFICATION*/
	function killIt(item){
		$('#'+item).fadeOut(1000);
	}			
	/*FOR IE 6 or 7 NOTIFICATION*/



$(document).ready(function(){
	/*FOR IE 6 or 7 NOTIFICATION*/
	function killIt(item){
		$('#'+item).fadeOut(1000);
	}			
	/*FOR IE 6 or 7 NOTIFICATION*/


	//NICKS CODE FOR ADDING NAME ATTRIBUTE FOR SCROLLING LINKS//

	$(".janilinkPlayer").attr("name", "janilinkvideo");


	//NICKS CODE FOR ADDING NAME ATTRIBUTE FOR SCROLLING LINKS//
	
	$(".popupRollOver").livequery("mouseover",function(){
		$(this).children('.contents').css("display","block");
		$(this).children('.contents').css('left',$(this).width()+"px");
	}).livequery("mouseout",function(){
		$(this).children('.contents').css("display","none");
	});
	

	if($('a.janilinkPlayer').exists())
	{
		flowplayer("a.janilinkPlayer", {src: "javascript/flowplayer/flowplayer-3.2.7.swf", wmode: 'opaque'}, {
			clip: {autoPlay: false, autoBuffering: true}
		});

		
	}

	if($('a.janilinkPlayer_Capt').exists())
	{
		flowplayer("a.janilinkPlayer", {src: "javascript/flowplayer/flowplayer-3.2.7.swf", wmode: 'opaque'}, {
			clip: {autoPlay: false, autoBuffering: true}
		});

		
	}

//ADDITION OF WINDOW=OPAQUE//

	if($('a.janilinkPlayer_autostart').exists())
	{
		flowplayer("a.janilinkPlayer_autostart", {src: "javascript/flowplayer/flowplayer-3.2.7.swf", wmode: 'opaque'}, /*NICK EDIT*/ {clip: {autoPlay: true, autoBuffering: true, hideDelay: 100}});

	}



	if($('#janilinkPlayer').exists())
	{
		flowplayer("janilinkPlayer", {src: "javascript/flowplayer/flowplayer-3.2.7.swf", wmode: 'opaque'}, {
			clip: {autoPlay: false, autoBuffering: true}
			
		});

		
	}


	if($('img#popupPlayer').exists())
		$('img#popupPlayer').livequery('click',function(){

			var src;
			var contentsToShow;

			if($(this).attr('class') == 'vtype_1')
			{
				src = $(this).attr("alt");
				contentsToShow = '<a href="'+src+'" id="janilinkPlayerPopup" class="janilinkPlayer"></a>';	
				$.fancybox(
					contentsToShow,
					{
						'width'        	: 'auto',
						'height'       	: 'auto',
						'transitionIn'	: 'elastic',
						'transitionOut'	: 'none',
						'title'			: this.title,
						'onComplete'	:	function() {
							flowplayer("janilinkPlayerPopup", "javascript/flowplayer/flowplayer-3.2.7.swf",{
								clip: {autoPlay: false, autoBuffering: true}
							});
						}
					}
				);
			}
			else if($(this).attr('class') == 'vtype_0')
			{
				$.ajax({
					type		: "POST",
					url			: "http://www.janilink.com/printSubtitleVideo.php",
					data		: 'id='+$(this).attr('alt'),
					success: function(data) {
						$.fancybox(
						data,
						{
							'scrolling'		: 'no',
							'width'         : 'auto',
							'height'        : 'auto',
							'transitionIn'	: 'elastic',
							'transitionOut'	: 'none',
							'title'			: this.title,
							'onComplete'	:	function() {
								
								
							}
						}
					);
					}
				});	
				
				
			}

			
	
	
	
		});


	if($('a.popupPlayer').exists())
		$('a.popupPlayer').fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'type'			: 'swf',
			'swf'			: {
				'wmode'				: 'transparent',
				'allowfullscreen'	: 'true'
			}
		});

	if($('a.popupBox').exists())
		$('a.popupBox').fancybox({
			'scrolling'		: 'no',
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'easingIn'		: 'easeOutBack',
			'easingOut'		: 'easeInBack'

		});
	
	/* Confirmation */
	$('#confirmOrder_btn').livequery('click',function(){
		$('.waitHidden').fadeIn(100);
	});
	
	$('.showP2').livequery('click',function(){
		if($('#'+$(this).attr('alt')).css('display') == 'block')
			$('#'+$(this).attr('alt')).hide(500);
		else if($('#'+$(this).attr('alt')).css('display') == 'none')
			$('#'+$(this).attr('alt')).show(500);
	});



	$('.showP').livequery('click',function(){
		$('.showP').each(function(){
			$('#'+$(this).attr('alt')).hide();
		})

		if($('#'+$(this).attr('alt')).css('display') == 'block')
			$('#'+$(this).attr('alt')).hide();
		else if($('#'+$(this).attr('alt')).css('display') == 'none')
			$('#'+$(this).attr('alt')).show();
	});
	$.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;position:absolute;';
		var inbox_serviceTime_eng = '<span style="color:#b2060e">Service Time</span><br /><span style="color:#19718b">- MON-FRI : 8AM ~ 6PM<br />- SAT : 8AM ~ 3PM<br />- SUN : Closed<span/><br />';
		var inbox_serviceTime_mex = '<span style="color:#b2060e">Servicios Tiempo</span><br /><span style="color:#19718b">- LUN-VIE : 8AM ~ 6PM<br />- SAB : 8AM ~ 3PM<br />- DOM : Closed<span/><br />';
		var inbox_serviceTime_kor = '<span style="color:#b2060e">Service Time</span><br /><span style="color:#19718b">- 월-금 : 8AM ~ 6PM<br />- 토 : 8AM ~ 3PM<br />- 일 : 휴무<span/><br />';
		$('div#flagEnglish').mouseover(function(){
			$('div#flagEnglish__Box').html('<div class="flagBox" style="'+topAndLeft+'"><span style="color:black"><b>Call Anyone</b></span><br />'+inbox_serviceTime_eng+'</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+'"><span style="color:black"><b>Español - Llamar Camilo</b></span><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+'"><span style="color:black"><b>한국말 서비스 - 민부장</b></span><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[name=password]').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() + 203;

		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")'); 
		}

	},function(){
		if($(this).attr('id').match(/parentCat_/gi))
		{
			$("div", this).hide(); 
			$(this).children('.clnk').css('background-image','none'); 
		}

	});
	$('#continueShoppingBTN').livequery('click',function(){
		$.fancybox.close();
	});

});
function addedCartMSG()
{
	$.fancybox(
		'<i>Added Successfully!!</i><b>'+$('#myCart__Top').html()+'</b><br /><span style="cursor:pointer;color:blue;" id="continueShoppingBTN">Click here or Press key <span style="color:red">(<u>ESC</u>)</span> to continue shopping.</span><br />*To check out please login.',
		{
			'autoDimensions'	: false,
			'width'        		: 'auto',
			'height'       		: 'auto',
			'transitionIn'		: 'none',
			'transitionOut'		: 'none'
		}
	);
}

function janilinkPlayer_C(id_subtitle,id_player,timePoint)
{
	var timePoint_arr_tmp = timePoint.split(",");
	var timePoint_arr = [];
	var caption_arr = [];

	for(var i in timePoint_arr_tmp)
	{
		timePoint_arr.push(parseInt(timePoint_arr_tmp[i] * 1000));
	}

	$f(id_player, "javascript/flowplayer/flowplayer-3.2.7.swf", {
		clip: {

			autoPlay: false, autoBuffering: true,

			onMetaData: function(clip){
				var cuepointInterval = 1;
				var cuepoints = [];
				for (var i=1, t=Math.floor(clip.duration / cuepointInterval)+1; i<t; i++){			
					cuepoints.push(i * cuepointInterval * 1000);
				}

				clip.onCuepoint(cuepoints, function(clip, seconds){
					if(timePoint_arr.length != $('#info_1 span').length)
					{
						//alert("There is an error, please check comma(,).");
					}
					else
					{	
						for(var i in timePoint_arr)
						{
							if(seconds >= timePoint_arr[i])
							{
								$("#"+id_subtitle+" span").removeClass('highlight');
								$("#"+id_subtitle+" span[alt="+i+"]").addClass('highlight');

							}
						}
					}
				});
			}
		},


		onStart: function() {
			$(".subtitleJanilnkPlayer").show(500,function(){
				$("#fancybox-outer").width($('#fancybox-content').width()+10);
				$("#fancybox-outer").height($('#fancybox-content').height()+10);

				$("#fancybox-wrap").width($('#fancybox-content').width());
				$("#fancybox-wrap").height($('#fancybox-content').height());
				$("#fancybox-content").width($('#fancybox-content').width());
				$("#fancybox-content").height($('#fancybox-content').height());
				$("#fancybox-content").children('div').css('overflow','visible');
			
			});

		},

		onFinish: function() {
			$("#"+id_subtitle+" span").removeClass('highlight');
			$(".subtitleJanilnkPlayer").hide(500);
		}
	});

}
