// JavaScript Document
var curBrand;
var origRate;
var mode3D=false;
$(document).ready(function(){
	origRate = parseInt($("#ratinglevel").css("width"));
	$("#ratenow a").mouseout(function(){
		$("#ratinglevel").css("width",origRate + "%");
	});
	$("#ratenow a").mouseover(function(){
		$("#ratinglevel").css("width",(parseInt(ltrim($(this).attr("href"),"#")) * 20) + "%");
	});
	$("#ratenow a").click(function(){
								   var pid=$("#pID").val();
								   var rate=ltrim($(this).attr("href"),"#");
								   $.get("/ajax/rate.php",{id:pid,star:rate},function(data){
																					  origRate=20*parseInt(data);
																					  $("#ratinglevel").css("background","url(/images/ratingbg_v.gif) repeat-x");
																					  });
								   return false;
								   });
								   
	
	$(".review_action").click(function(){
									   //get the action and the id
									   var sp = this.name.split("-");
									   var mode = sp[0];
									   var rid = sp[1];
									   $.get("review_action.php",{id:rid,type:mode},function(data){
																						   window.location.reload();
																						   });
									   });
	
	$("#brandslist a").click(function(){
		//first get the slug
		var sel = $(this);
		var slug = sel.attr("href");
		slug = slug.split("/shop/");
		slug = slug[1];
		curBrand=slug;
		if(typeof autoStyle == 'undefined'){
			autoStyle = "";
		}
		$.get("/ajax/styles.php?name="+slug,{name:slug, style:autoStyle},function(data){
														 $("#stylelist").html(data);
														 styleCtrl();
														 sIFR.replace(avant, {
															selector: '#sel_style',
															wmode: 'transparent',
															css: ['.sIFR-root { color: #FFFFFF; }','a{text-decoration:none; color:#ff7979;}','a:hover{color:#c60000;}']
														});
														 sIFR.replace(avant, {
															  selector: '#stylelist .styleitem',
															  wmode: 'transparent',
															  css: ['.sIFR-root { color: #FFFFFF; }','a{text-decoration:none; color:#FFFFFF;}','a:hover{color:#DDDDDD;}']
														 });
														 
														 });
		
		return false;
	});
	
	$("#index_cycle").cycle();
	$("#shop_cycle").cycle();
	
	$("#imgctrl a").click(function(){
								   var which=$(this).attr("href");
								   if(which=="img1"){
									   $("#threeD").fadeOut("slow");
									   mode3D=false;
									   $("#img2").fadeOut("slow",function(){
																		 $("#img1").fadeIn("slow");
																		  });
								   } else if(which=="img2"){
									   $("#threeD").fadeOut("slow");
									   mode3D=false;
									   $("#img1").fadeOut("slow",function(){
																		 $("#img2").fadeIn("slow");
																		  });
								   } else if(which=="rotate"){
									   $("#img1").fadeOut("slow");
									   $("#img2").fadeOut("slow",function(){
																		  $("#threeD").fadeIn("slow");
																		  mode3D = true;
																		  });
								   }
								   return false;
								   });
	
	$("#images").mousemove(function(e){
									if(mode3D){
										var pos =$("#contents").offset();
										var X = e.pageX - pos.left -10;
										var index = Math.floor((X/310)*10);
										var imgs = $("#threeD img")
										imgs.css("display","none");
										var one = imgs.get(index);
										one.style.display = "block";
									}
									});
	
	$("#addReview").click(function(){
		$.get(this.href,null,function(data){
			$("#review_wnd").html(data);
			$("#review_wnd").fadeIn("slow");
			$("#review_wnd #submit1").css("display","none");
			$("#review_wnd #submit").css("display","block");
			$("#close_review").click(function(){
				$("#review_wnd").fadeOut("slow");
			});
			$("#write_review #submit").click(function(){
				//this function gathers the data and sends to server
				var params = new Object();
				$("#review_wnd input").each(function(){
					params[$(this).attr("name")] = $(this).val();
				});
				params["text"] = $("#text").val();
				$("#write_review").html('<img src="/images/loading.gif" />');
				$.post("/post_review.php",params,function(data){
														 $("#review_wnd").html(data);
														 },"html");
		
			});
		});
		return false;
	});
	
	styleCtrl();
	if (typeof autoBrand != 'undefined'){
		//need to go through all our brands and if they match the autoBrand, run their click event
		$("#brandslist a").each(function(){
										 var href = $(this).attr("href");
										 href = href.split("/shop/");
										 if(href[1]==autoBrand){
											 $(this).click();
											 return false;
										 }
										 });
		
	}
	
	
});

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function styleCtrl(){
	$("#stylelist .style_ctrl a").click(function(){
		var q = $(this).attr("href").split("?");
		var str = "/ajax/styles.php?" + q[1] + "&name=" + curBrand;
		$.get(str,null,function(data){
														$("#stylelist").html(data);
														styleCtrl();
														sIFR.replace(avant, {
															selector: '#sel_style',
															wmode: 'transparent',
															css: ['.sIFR-root { color: #FFFFFF; }','a{text-decoration:none; color:#ff7979;}','a:hover{color:#c60000;}']
														});
														 sIFR.replace(avant, {
															  selector: '#stylelist .styleitem',
															  wmode: 'transparent',
															  css: ['.sIFR-root { color: #FFFFFF; }','a{text-decoration:none; color:#FFFFFF;}','a:hover{color:#DDDDDD;}']
														 });
														
														});
		return false;	
	});
}
function close_wnd(){
	$("#review_wnd").fadeOut("slow");
}
