/*
onVon.com jQuery Code
@ Author: shabingliang
@ E-mail: shabingliang@gmail.com
@ Time:   2008-9-28
Be sure to retain this text if you want to use this code.
*/

//Tab Switch plug-in
jQuery.fn.layerSwitch=function(){
	return this.each(function(){
		var tabType=$(this).attr("evt");
		if(!tabType){
			tabType="mouseover"
		}		
		var $tabs=$(this).children("div");
		$tabs.each(function(){				
			$(this).bind(tabType,function(){
				$tabs.each(function(){
					$(this).removeClass().addClass("off");
					$($(this).attr("title")).removeClass().addClass("hide");			
				});
				$(this).addClass("on");
				$($(this).attr("title")).addClass("show");
			});
		});
	});
}

//tabSwitch plug-in.  Use in the product_info.php page
jQuery.fn.tabSwitch=function(){
	return this.each(function(){
		$this=$(this);
		$li=$this.children("li");
		$li.each(function(){
			$(this).click(function(){
				$li.each(function(){
					$(this).removeClass().addClass("tab_b");
					$($(this).attr("ref")).hide();
				});
				if(($(this).attr("id")=="imagesTab") && !$($(this).attr("ref")+" img").is("img")){
					for(var i=0;i<imageArr.length;i++){
						$($(this).attr("ref")).append("<img src='"+imageArr[i]+"' style='border-bottom:1px #fff solid;'/><br/>");
					}
				}
				$(this).removeClass().addClass("tab_f");
				$($(this).attr("ref")).fadeIn("slow");
				return false;								   
			});
		});
	});
}

//LiveChatScroll plug-in
jQuery.fn.liveChatScroll=function(){
	return this.each(function(){
		var headlineTimeout;
		var $this=$(this);
		var totalHeight = $this.height();
		
		// Set up the rotator.
		var currentHeadline = 0, oldHeadline = 0;
		var hiddenPosition = totalHeight   + 2;
		$this.children("p:eq("+currentHeadline+")").css("top","0");
		var $p=$this.children("p");
		var headlineCount = $p.length;
		
		var rotateInProgress = false;
		
		// Rotator function.
		var headlineRotate = function(){
			if (!rotateInProgress){
				rotateInProgress = true;
				headlineTimeout = false;
				currentHeadline = (oldHeadline + 1) % headlineCount;
				$p.filter(':eq(' + oldHeadline + ')')
				.animate({top: -hiddenPosition}, 'slow', function(){
					$(this).css('top',hiddenPosition);
				});
					
				$p.filter(':eq(' + currentHeadline + ')')
				.animate({top: 0},'slow', function(){
					rotateInProgress = false;
					if (!headlineTimeout) {
						headlineTimeout = setTimeout(headlineRotate, 3000);
					}
				});
				oldHeadline = currentHeadline;
			}
		};
		
		headlineTimeout = setTimeout(headlineRotate,3000);
		
		var $chat_div=$("#chat_div");	
		$p.children("a").click(function(){										
			if($chat_div.css("display")=="block"){
				if (!headlineTimeout) {
					headlineTimeout = setTimeout(headlineRotate, 1000);
				}
				
				$chat_div.slideUp("slow",function(){
					$("#nav_chat").css({backgroundColor:"#146eb4"});
					$chat_div.css("display","none");
				});
				return false;
			}	
			else if($chat_div.css("display")=="none"){
				clearTimeout(headlineTimeout);
				headlineTimeout = false;
				
				var name = $(this).attr("title");
				var msn = $(this).attr("msn");
				var skype = $(this).attr("skype");
				var email = $(this).attr("email");
				var yahoo = $(this).attr("yahoo");
			
				var str = "";
				str += "<ul class='gray_trangle_list'>";
				if(email!=null && email != ""){
					str += "<li><span class='big black b'>Email</span><BR/><span class='pad_1em'><a href=mailto:"+email+">"+email+"</a></span></li>";
				}
				if(msn!=null && msn != ""){
					str += "<li><span class='big black b'>MSN</span><BR/> <span class='pad_1em'><a href=msnim:chat?contact="+msn+">"+msn+"</a></span></li>";
				}
				if(skype!=null && skype != ""){
					str += "<li><span class='big black b'>SKYPE</span><BR/> <span class='pad_1em'>" + skype + "</span></li>";
				}
				if(yahoo!=null && yahoo != ""){
					str += "<li><span class='big black b'>YAHOO</span><BR/> <span class='pad_1em'>" + yahoo + "</span></li>";
				}
				str += "</ul>";	
				$("#chat_div_name").html(str);
				$("#nav_chat").css({backgroundColor:"#fef1d2"});
				$chat_div.slideDown("slow",function(){
					$chat_div.css("display","block");
				});	
				return false;
			}
		});
		
		$("#chat_div_close_img").click(function(){
			if (!headlineTimeout) {
				headlineTimeout = setTimeout(headlineRotate, 1000);
			}
			$chat_div.slideUp("slow",function(){
				$("#nav_chat").css({backgroundColor:"#146eb4"});
				$chat_div.css("display","none");
			});
		});	
	});
}


//popMenu plug-in
jQuery.fn.popMenu=function(){
	return this.each(function(){
		var $this=$(this);
		var closeMenu = true;
		function controlMenu(){
			if(closeMenu){
				$("#litbCon2").hide();
				$this.css({backgroundPosition:"0 0"});
			}else{
				$("#litbCon2").show();
				$this.css({backgroundPosition:"0 -16px"});
			}
		}
		$this.mouseover(function(){closeMenu=false;controlMenu();});
		$("#litbCon2").hover(function(){
			closeMenu=false;setTimeout(controlMenu, 2000);
		},function(){
			closeMenu=true;setTimeout(controlMenu, 2000);
		});
	});	
}

//ScreenScrollUp plug-in
jQuery.fn.screenScrollUp=function(){
	return this.each(function(){
		var $scroll=$(this);		
		
		var moveStep=1;
		//scroll init value
		var step=0;
		//control need scroll
		var canScroll=true;	
		//scroll box hight
		var containerHeight=$scroll.height();
		var actualHeight=0;
		var $li=$scroll.children("ul:first").children("li");
		var totalItems=$li.length;	
		var itemHeight=$li.eq(0).height();
		//scroll context totall height
		var actualHeight=totalItems*itemHeight;			
		
		var diff=actualHeight-containerHeight;
		
		//not need scroll
		if(diff<=0){
			canScroll=false;
		}
		
		function scroll(){
			step+=moveStep;	
			
			if(step>=actualHeight){
				step=0;	
			}
			$scroll.scrollTop(step);
			clearInterval(interval);
			if(step%itemHeight==0){
				interval=setInterval(scroll,1500);
			}
			else{
				interval=setInterval(scroll,20);
			}
		}
		
		var interval;
		if(canScroll){
			$scroll.append($scroll.children("ul:first").clone());
			interval=setInterval(scroll,20);
		}
		$scroll.hover(function(){
			clearInterval(interval);
		},function(){
			moveStep=1;
			interval=setInterval(scroll,20);
		});
	});
}


jQuery.fn.popLayer=function(){
	return this.each(function(){
		var $this=$(this);
		var shipping_info='<h2 class="blue">Shipping Methods:</h2><div style="margin-top:10px;color:black; font-size:12px;">onVon.com offers almost international shipping methods:<br/>DHL, UPS, FEDEX, TNT: Normally takes <b style="color:#f8743f">3-12</b> days.<br/>Exact Time of transit varies depending on where you\'re located and where your package is coming from.<br/><img src="images/dhl.jpg" /><img src="images/fd.jpg" /><br/><img src="images/tnt.jpg" /><img src="images/ups.jpg" /></div>';
		var payment_option='<h2 class="blue">Payment methods:</h2><div style="color:black; font-size:12px;">PayPal<br/>Western Union Wired Payment<br/>Bank Transfer</div><h2 class="blue">Attention Please:</h2><div style="color:black; font-size:12px;">We will prepare your order after the payment is confirmed.<br/>BUT If you pay us by Credit Card or the unverified PayPal account for your first order, you will be requested to provide us some info. to verify your account at the first time. We will prepare your order after your account is confirmed.<br/>Please do remember to mark the order No. in the payment info. if you pay us by Bank Transfer or Western Union so that we can check after the payment is confirmed.Thanks!<br/>Time of transit varies depending on where you\'re located and where your package is coming from.</div>';
		var posX,posY;
		var width = 339;
		var height = 328; 
		
		$this.children("li").children("a").each(function(){
			$(this).click(function(){		
				var str ="";
				var element=$(this).attr("title");
				switch (element){
					case 'Shipping Methods':
							str += shipping_info;
							break;
					case 'Payment methods':
							str += payment_option;
							break;
					case 'questions':
							str += questions;
							break;                              
					default : str += "";
							break;
					
				}	
				var offset=$(this).offset();
				posX=offset.left-width;
				poxY=offset.top-125;
				var $pop=$("#pop_window");
				$pop.hide();
				$pop.children("div.png").html(str);
				$pop.css({position:"absolute",width:"339px",height:"328px",top:poxY,left:posX}).show("slow");
				return false;				
			});
		});
		$("#floatBox_img").click(function(){
			$("#pop_window").hide();
		});
	});
}


//Products ajax review function,productID need define outside the function
function ajaxReview(){
	$("#submitReview").click(function(){
		var customerName=$("#c_name").val();
		var customerEmail=$("#c_email").val();
		var customerSay=$("#c_content").val();
			
		var error_message="";  
		var error;
		if(customerName.length<2){
			error_message = "The name you input is wrong! \n";
			error = 1;
		}
		else if(customerName.length>50){
		   error_message = "The name you input is wrong! \n";
		   error = 1;
		} 
	
		var email=customerEmail;
		var re =  /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
		if (!re.test(email)){
		  error_message = error_message + ' Please,input the accurate Email. \n';
		  error = 1;
		}
		
		var content=customerSay;
		if(content.length<10){
		  error_message = error_message + " The content of the review is so short . \n";
		  error = 1;
		}else if(content.length>225){
			error_message = error_message + " The content must small the 225 characters. \n";
			error = 1;
		}
		//Error,back
		if (error == 1){
			alert(error_message);
			return false;
		} 
		
		var pma_review="products_id="+productID+"&name="+customerName+"&email="+customerEmail+"&content="+customerSay;
		
		var url="ajax_review.php?"+pma_review;
		
		$.get(url,function(data){
			var d=new Date();
	  		var timeText=" at ["+d.toGMTString()+"]";
	  
			if(data=="true111"){
				var html='<li style="font-size:12px;"><div class="image"><img alt="" src="images/1.jpg" class="avatar" width="48" height="48"></div><div class="cmthead"><p class="cmtdate"><strong>'+customerName+'</strong>&nbsp;at&nbsp;'+ timeText +'&nbsp;</p></div><div class="cmtbody"><p>'+ customerSay +'</p></div></li>'; 
				$("#commentlist").append(html);
				$("#post_review :reset").trigger("click");
			}
			else{
				alert(data);
			}
		});
		
	}).ajaxStart(function(){
		//$("#loading").show();	
	}).ajaxStop(function(){
		//$("#loading").hide();
	});
	$("#loading").hide();
}
//productID need define outside the function
function checkshipping(){
	var pam="noo="+$("#anoo").val()+"&countries="+$("#acou").val()+"&shippingway="+$("#asw").val();
	var showurl="ajax_shop.php?"+pam+"&products_id="+productID;
	$.get(showurl,function(data){
		/*
		data=currency(data);
		if(data>0){
		   	$("#shippingcost").html(currencyStr(data));
		 }else{
			$("#shippingcost").html(data);
		 }
		 */
		 $("#shippingcost").html(data);
	});
}


function rewrite_url(pname , pid){
	if(pid == null || pid == "undefined" ) {return ""};
	var re = /[^a-zA-Z0-9]/ig;
	return url = baseURL + pname.replace(re,"-") + "-p-" + pid + ".html";
}

function flashWrite( id, flashUri, vWidth, vHeight, winMode ) {
	var _obj_ = "";
	_obj_ = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="' + vWidth + '" height="' + vHeight + '" id="' + id + '" align="middle">';
	_obj_ += '<param name="movie" value="' + flashUri + '" />';
	_obj_ += '<param name="quality" value="high" />';
	_obj_ += '<param name="wmode" value="' + winMode + '" />';
	_obj_ += '<param name="bgcolor" value="#ffffff" />';
	_obj_ += '<embed src="' + flashUri + '" quality="high" wmode="' + winMode + '" bgcolor="#ffffff" width="' + vWidth +'" height="' + vHeight + '" id="' + id + '" align="middle" allowscriptaccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></embed>    ';
	_obj_ += '</object>';
	document.write( _obj_ );
}
jQuery(document).ready(function(){
	$("#nav_chat_sales").liveChatScroll();
	$("#litbBtn").popMenu();
	$("#boxswitch").layerSwitch();
	$("#recentlyorder").screenScrollUp();
});