function sort(url){
	var sortby = $("select[@name=sort_by]").val();
	url += "&sort="+sortby;
	location.href = url;
}

function pageto(url){
	var page_num = $("select[@name=page]").val();
	url += "&page="+page_num;
	location.href = url;
}

function change_perpage(url){
	var perpage = $("select[@name=perpage]").val();
	url += "&per="+perpage;
	location.href = url;
}

function showSKU(url){
	$("#SKUs").empty();
	$.get(
		url,
		{},
		function(data){
			var result = eval('('+data+')');
			$("#SKUs").empty();
			if(result.success){
				$("#SKUs").append(result.success);
			}else if(result.error){
				$("#SKUs").append("<div style='background:#fff;color:#f60;font-size:11px;font-weight:bold;width:150px;'>"+result.error+"</div>");
			}
		}
	);
}

function moveSKU(event,imgae){
	try{
	var offset = Position.cumulativeOffset(imgae);
	$("#SKUs").css("left",document.documentElement.scrollLeft + event.clientX + 15);
	$("#SKUs").css("top",document.documentElement.scrollTop + event.clientY);
	$("#SKUs").show();
	}catch(e){}
}

function hideSKU(){
	$("#SKUs").empty();
	$("#SKUs").hide();
}



function viewBigImg(){
var url =$(".big_pic").css('background-image').replace(/url\(/,"").replace(/\)/,'').replace(/\"/g,'');
var url_index = url.lastIndexOf("/");
var url2 = url.substr(0,url_index)+"/"+url.substr(url_index+1,url.length);


window.open(url2);
}
		
$(document).ready(function(){
	/****************************************************************
	 *
	 * add tree
	 *
	 ****************************************************************/
	$("#categorys").hide();
	$("#categorys").treeview({persist: "location",collapsed:true,unique: false});
	var expandLi = '<li class="collapsable" style="padding: 3px 0px 1px 16px; visibility:hidden;">';
	expandLi += '<div class="hitarea"/>';
	expandLi += '<a class="main selected" href="#">test</a>';
	expandLi += '<ul style="display: block;">';
	expandLi += '<li>';
	expandLi += '<a href="#">test</a>';
	expandLi += '</li>';
	expandLi += '<li>';
	expandLi += '<a href="#">test</a>';
	expandLi += '</li>';
	expandLi += '<li class="last">';
	expandLi += '<a href="#">test</a>';
	expandLi += '</li>';
	expandLi += '</ul>';
	expandLi += '</li>';
	$("#categorys").append(expandLi);
	$("#categorys").show();
	/****************************************************************
	 *
	 * add press "Enter" event for search
	 *
	 ****************************************************************/
	 $("input[@name=search_word_products]").keydown(
		function(e){
		try{
			if(e.which == 13){
			location.href=("/product/search?key="+$("input[@name=search_word_products]")[0].value);
			}
		}catch(exc){alert(exc);}
		}
	 );
	 
	 $("input[@name=search_word_competitor]").keydown(
		function(e){
			try{
			if(e.which == 13){
			location.href=("/product/compet?key="+$("input[@name=search_word_competitor]")[0].value);
			}
			}catch(exc){alert(exc);}
		}
	 );

	 $("#go_search_products").click(
		function(){
		location.href=("/product/search?key="+$("input[@name=search_word_products]")[0].value);
		}
	 );
	 
	 $("#go_search_competitor").click(
		function(){
		location.href=("/product/compet?key="+$("input[@name=search_word_competitor]")[0].value);
		}
	 );
	 
	 /****************************************************************
	 *
	 * zoom in
	 *
	 ****************************************************************/
	
	if($("#frame") && $(".big_pic")){
	var drag = false;
	
		
	/* $("#frame").draggable({
		containment:"parent",*/
		/*drag:function(e,ui){
			var top = ui.position.top*4;
			var left = ui.position.left*4;
			$(".big_pic").css("background-position","-" + left + "px -" + top + "px");
		},*/
/*		start:function(e,ui){
			$(".big_pic").empty();
			drag = true;
		},
		stop:function(){
			drag = false;
			var top = $("#frame").css("top").replace("px","");
			var left = $("#frame").css("left").replace("px","");
			if($("#frame").css("top") != '0'){
				$("#frame").css("top",(top-1)+"px");
			}
			if($("#frame").css("left") != '0'){
				$("#frame").css("left",(left-1)+"px");
			}
		}
	});*/ 
	 
	var objw=0,objh=0;
	var sh =0,sw =0;
	var movOBJ = false;
	var bigIMG =false;
	function setViewobj(){
		try{
		movOBJ = $("#frame");
		bigIMG = $(".big_pic");
		bigIMG.empty();
		movOBJ.css("display","block");
		objw =document.getElementById("frame").parentNode.offsetLeft;
		objh =document.getElementById("frame").parentNode.offsetTop;
		sh =document.documentElement.scrollTop; 
		sw =document.documentElement.scrollLeft;
		//movOBJ.parent().css("cursor","pointer");
		//movOBJ.css("cursor","pointer");
		drag = true;
		}catch(e){setTimeout(setViewobj,200);}
		};
		
	 $("#frame").parent().mouseover(function(){
		setViewobj();
		}); 
	 
	 $("#frame").parent().mouseout(function(){
		try{movOBJ.css("display","none");}catch(e){}
		drag = false;
		}); 
		 
	
		$(".big_pic").click(function(){
		try{viewBigImg();}catch(e){}
		}); 
		
		$(".big_pic").mouseover(function(){
		try{$(".big_pic").css("cursor","pointer");}catch(e){}
		}); 
	/*$("#frame").mousemove(function(){
		if(drag){
			var top = $(this).css("top").replace("px","")*4;
			var left = $(this).css("left").replace("px","")*4;
			$(".big_pic").css("background-position","-" + left + "px -" + top + "px");
		}
	});*/
	 	$("#frame").parent().mousemove(function(event){
		try{
			if(!drag)setViewobj();
			var h =sh + event.clientY - objh-20;
			var w =sw + event.clientX - objw-18;
			movOBJ.css('left',w);
			movOBJ.css('top',h);
			bigIMG.css("background-position","-" + w*4 + "px -" + h*4 + "px");
		}catch(e){}
	});
	 
	}
	 /****************************************************************
	 *
	 * tab function
	 *
	 ****************************************************************/
	 $("ul.product_tabs li").click(
		function(){
			if($(this).attr("class") == "tab"){
				$(".active_tab").removeClass("active_tab").addClass("tab");
				$(this).removeClass("tab").addClass("active_tab");
				var div_name = $(this).attr("div_name");
				$(".tab_div").hide();
				$("#"+div_name).show();
			}
		}
	);
	 
});


