
var interSearch = "";

$(document).ready(function(){
	//check if productbox exists?
	if( $("#productbox").length > 0 ){
		//$("#productbox").load(baseurl+"/product/tab");
		$("#productbox").load(productTabURL);
	}else{
		//$("#productbox-master").load(baseurl+"/product/tab");
		$("#productbox-master").load(productTabURL);
	}
	
	if( $("#recentview").length > 0 ){
		//$("#recentview").load(baseurl+"/product/recent");
		$("#recentview").load(productRecentURL);
	}
	
	if( onProduct ){
		//alert( "product id => "+productId );
		jQuery.ajax({
			url			: productRecentURL, //baseurl+'/product/recent/',
			type		: 'post',
			data		: 'act=save&id='+productId,
			dataType:'json',
			success:function(data){
			}
		});
		
		
		//tab festures, download & accessories ========
		//When page loads...
		$(".ptab_content").hide(); //Hide all content
		$("ul.ptabs li:first").addClass("active").show(); //Activate first tab
		$(".ptab_content:first").show(); //Show first tab content
	
		//On Click Event
		$("ul.ptabs li").click(function() {
	
			$("ul.ptabs li").removeClass("active"); //Remove any "active" class
			$(this).addClass("active"); //Add "active" class to selected tab
			$(".ptab_content").hide(); //Hide all tab content
	
			var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
			$(activeTab).fadeIn(); //Fade in the active ID content
			return false;
		});
		//===============================================
		
	}
	
	$("#recentBtn").click(function(){
		$('#recentbox').slideDown();
	});
	$("#rclose").click(function(){
		$('#recentbox').slideUp();
	});
	
	//promo
	//$(".promo_left").load(baseurl+"/herobox/show/left/1");
	$(".promo_left").load(heroboxShowLeftURL);
	//$(".promo_mid").load(baseurl+"/herobox/show/center/1");
	$(".promo_mid").load(heroboxShowCenterURL);
	//$(".promo_right").load(baseurl+"/herobox/show/right/1");
	$(".promo_right").load(heroboxShowRightURL);
	
	//search
	jQuery(function($){
  	$("#searchbox").Watermark("Search");
  });
  
  $(".search").keyup(function() 
	{
		window.clearInterval(interSearch);
		interSearch = "";
		interSearch = window.setInterval("goSearch('" + escapeHtml($(this).val()) + "')", 1000);
		/*
		var searchbox = $(this).val();
		var dataString = 'searchword='+ searchbox;
	
		if(searchbox=='')
		{
		}
		else
		{
			$.ajax({
				type: "POST",
				url: searchURL, //baseurl+"/search",
				data: dataString,
				cache: false,
				success: function(html)
				{
					$("#displaysearch").html(html).show();
				}
			});
		}
		*/
	});
	
	
	
	$(document).click(function(){
		$("#displaysearch").html('');
		$("#displaysearch").hide();
	});
	
		//rate ==========================================
		$("[id^=rating_]").hover(function() {


			rid = $(this).attr("id").split("_")[1];
			$("#rating_"+rid).children("[class^=star_]").children('img').hover(function() {
	
				$("#rating_"+rid).children("[class^=star_]").children('img').removeClass("ratehover");
	
				/* The hovered item number */
				var hovered = $(this).parent().attr("class").split("_")[1];
				
				//kasih value ke rate
				$("#rating-value").val(hovered);
				
				while(hovered > 0) {
					$("#rating_"+rid).children(".star_"+hovered).children('img').addClass("ratehover");
					hovered--;
				}
				
			});
		});
	
		/*
		$("[id^=rating_]").children("[class^=star_]").click(function() {
	
			
			var current_star = $(this).attr("class").split("_")[1];
			var rid = $(this).parent().attr("id").split("_")[1];
	
			$('#rating_'+rid).load('send.php', {rating: current_star, id: rid});
	
		});
		*/
		//====================================================
	
		menu = new dhtmlXMenuObject("dmenu", "modern_red");
		menu.setImagePath(baseurl+"/assets/js/dhtmlx/dhtmlxMenu/imgs/");
		menu.loadFromHTML("menuData", true);
		menu.attachEvent('onClick',function(id){
			//alert(id);
			var t = id.split('|'); 
			if( t[0] == 'so7'){
				if( t[2] == 'out'){
					window.open(t[1], '_blank');
				}else{
					location.href=t[1];
				}
			}else{
				return false;
			}
		});
	
});

//mengganti image product
	function changeImg(url,im){
		$("#imgpro").attr("src", url);
		$("#proa").attr("href", baseurl+"/data/user/products/"+im);
	}
	function showCompare(){
		$("#compareBox").show();
	}
	
//compare
function gocomp(){
	location.href = $('#secomp').val();
}


function goSearch(str){
	
		window.clearInterval(interSearch);
		interSearch = "";
	
		var searchbox = str;
		var dataString = 'searchword='+ searchbox;
	
		if(searchbox=='')
		{
		}
		else
		{
			$.ajax({
				type: "POST",
				url: searchURL, //baseurl+"/search",
				data: dataString,
				cache: false,
				success: function(html)
				{
					$("#displaysearch").html(html).show();
				}
			});
		}
}

function escapeHtml(unsafe) {
  return unsafe
      .replace(/&/g, "&amp;")
      .replace(/</g, "&lt;")
      .replace(/>/g, "&gt;")
      .replace(/"/g, "&quot;")
      .replace(/'/g, "&#039;");
}

function addslashes(str) {
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\'/g,'\\\'');	
	str=str.replace(/\0/g,'\\0');
	return str;
}
