//Shadowbox.loadSkin('classic', 'js/skin'); // use the "classic" skin
//window.onload = Shadowbox.init;

$(document).ready(function(){
	var navconfig = {
     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
     interval: 100, // number = milliseconds for onMouseOver polling interval
     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
     timeout: 200, // number = milliseconds delay before onMouseOut
     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};

	$("ul#top-menu li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("ul#top-menu li").hoverIntent(navconfig); //Trigger Hover intent with custom configurations					   
							   
	$("ul.sf-menu").supersubs({ 
            minWidth:    6,   // minimum width of sub-menus in em units 
            maxWidth:    30,   // maximum width of sub-menus in em units 
            extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
                               // due to slight rounding differences and font-family 
        }).superfish({ 
		animation: {height:'show'},
		speed:     'fast',   // slide-down effect without fade-in 
		delay:     1200               // 1.2 second delay on mouseout 
	});
	
	/* $(".manufacturers").jScrollbar({
      allowMouseWheel : true,
      scrollStep : 100
    });*/
	 
	
 
	$('#headpics').cycle({ 
		timeout: 20000, 
		speed:   900,
		random: 1
	});
	
	$('#pauseButton').click(function() { 
		$('#s4').cycle('pause');
		$('#pauseButton').hide();
		$('#playButton').show();
	});
	$('#playButton').click(function() {
		$('#s4').cycle('resume');
		$('#playButton').hide();
		$('#pauseButton').show();
	});
		$('#s4').after('<div id="ad_nav" class="ad_nav">').cycle({
			fx:     'fade',
			timeout: 3000,
			pager:  '#ad_nav',
			next:   '#next', 
			prev:   '#prev',
			pauseOnPagerHover: true,
			pause: true,
			before: function() { if (window.console) console.log(this.src); }
		});
	
	$(document).pngFix(); 
	//dotree();
	
	//adjust heights of ad blocks and makes list
		$("#radgallery").hide();
		var ih = $("#main2").innerHeight();
		var rhs = $("#rcshell").innerHeight();
		var xh = Math.round((ih - rhs )/230)*230;
		//$("#xh").html(ih);
		$("#radgallery").css("height",xh);
		$("#radgallery").show();
		
		
		var ih = $("#main2").innerHeight();
		var mh = $(".overview").innerHeight();
		var lhs = $("#lhshell").innerHeight()+ 60;
		//$("#xh").html(mh);
		if(mh > 400){
			var nmh = ih - lhs;
			nmh = nmh > mh ? mh : nmh;
			
			//$("#xh").html(ih+" "+lhs+" "+mh+" "+nmh);
			$(".viewport").css("height",nmh);
			$(".manufacturers").css("height",nmh);
			
		}
		$('.manufacturers').tinyscrollbar();
});

//top-menu On Hover Over
function megaHoverOver(){
    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
			var ct = 0;
            $(this).find('.level1').each(function() { //for each ul...									  
 					ct++;
            });
			ct = ct > 5 ? 5 : ct;
			rowWidth = ct * 120; //Add each ul's width together
			//alert(ct);
			var position = $(this).position();
			if(position.left + rowWidth > 970){
				offsetLeft = 945 -(position.left + rowWidth);
				$(this).find(".sub").css({'left' : offsetLeft}); //Set Width
			}
        };
    })(jQuery); 

    if ( $(this).find(".row").length > 0 ) { //If row exists...

        var biggestRow = 0;	

        $(this).find(".row").each(function() {	//for each row...
            $(this).calcSubWidth(); //Call function to calculate width of all ul's
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });

        $(this).find(".sub").css({'width' :biggestRow}); //Set width
        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin
		

    } else { //If row does not exist...

        $(this).calcSubWidth();  //Call function to calculate width of all ul's
        $(this).find(".sub").css({'width' : rowWidth}); //Set Width

    }
}
//top-menu On Hover Out
function megaHoverOut(){
  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
  });
}

function dotree(){
	$("#tree").treeview({
			collapsed: true,
			animated: "medium",
			control:"#sidetreecontrol",
			persist: "cookie"
			//unique: true
		});
}


function select_options(id){
		
				
			$("#outofstock").hide();
			$("#instock").hide();
			$("#cartbutton").hide();
			$("#showprice").html("Fetching data...");
			$("#showprice").show();
			$("#option2").hide();
			var opt1 = $("colour").val();
			$.ajax({
				method: "get",url: "/ajax/get_options.php",data: "id="+id,
				success: function(html){ //so, if data is retrieved, store it in html
					$("#showprice").hide();
					$("#option2").html(html); //show the html inside .content div
					$("#option2").show('fast');
					//$("#cartbutton").show();
				 }
			 }); //close $.ajax(
	
 } 
 

function check_stock(id){
		
			$("#outofstock").hide();
			$("#instock").hide();
			$("#cartbutton").hide();
			$("#showprice").html("Fetching data...");
			$("#showprice").show();
			var opt1 = ($("#colour").val());	
			var opt2 = ($("#size").val());	
			$.ajax({
				method: "get",url: "/ajax/check_stock.php",data: "id="+id+"&opt1="+opt1+"&opt2="+opt2,
				success: function(html){ //so, if data is retrieved, store it in html
					//$("#instockans").html(html);
					if(html.substring(0,1) == '1'){
						$("#showprice").hide();
						$("#showprice").html("Total Price = &pound;"+html.substr(2,html.length)); //show the html inside .content div
						$("#showprice").show("slow");
						$("#instock").show("slow");
						$("#cartbutton").show("slow");
					} else {
						$("#showprice").hide();
						$("#outofstock").show("slow");
					}
				 }
			 }); //close $.ajax(
	
 } 

function stock_enquiry(id){
  // open a welcome message
  	var opt1 = ($("#colour").val());	
	var opt2 = ($("#size").val());	
    Shadowbox.open({
        player:     'iframe',
        title:      'Email me when this product is available',
        content:    '/ajax/stock_enquiry.php?id='+id+'&opt1='+opt1+'&opt2='+opt2,
        height:     350,
        width:      600
    });
}

function price_match(id){
  // open a welcome message
  	var opt1 = ($("#colour").val());	
	var opt2 = ($("#size").val());	
    Shadowbox.open({
        player:     'iframe',
        title:      'Price Match',
        content:    '/ajax/price_match.php?id='+id+'&opt1='+opt1+'&opt2='+opt2,
        height:     450,
        width:      600
    });
}

function subscribe(){
		
	var ok = "";
	var email = $("#subscribe_email").val();
	var fname = $("#subscribe_fname").val();
	var sname = $("#subscribe_sname").val();
	var source = $("#subscribe_source").val();
	var pid = $("#subscribe_pid").val();
	ok += email == "" ? "<li>email address</li>" : "";
	ok += !isEmail2(email) ? "<li>email format</li>" : "";
	ok += fname == "" ? "<li>firstname</li>" : "";
	ok += sname == "" ? "<li>surname</li>" : "";
	
	if(ok.length == 0){
		$.ajax({
			method: "get",url: "/subscribe.php",data: "email="+escape(email)+"&fname="+escape(fname)+"&sname="+escape(sname)+"&source="+escape(source)+"&pid="+escape(pid),
			success: function(html){ //so, if data is retrieved, store it in html
				$("#keepintouch").html("Thank you for subscribing"); //show the html inside .content div
				return false;
			 }
		 }); //close $.ajax(
	} else {
		$("#sub_warning").html("Please complete your<ul>"+ok+"</ul>");
		$("#sub_warning").show("slow");
	}
	return false;
 } 
 
function isEmail2(who) {
	var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	return(email.test(who));
}

// JavaScript Document
/*function toggleP(me,target) {
	$("#"+me).hide();	
	$("#"+target).fadeIn("slow");	
}
$(function() {
	$('.pics').cycle({
		timeout: 10000 ,	
		delay:  1000, 
		speed:  2000
		
	});
}); 
*/

// (C) 2000 www.CodeLifter.com
// http://www.codelifter.com
// Free for all users, but leave in this  header
if (parent.frames.length > 0) {
	    parent.location.href = self.document.location
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function use_billing(){
	document.form['firstName'].value = document.form['delfirstName'].value;
	document.form['lastName'].value = document.form['dellastName'].value;
	document.form['address1'].value = document.form['deladdress1'].value;
	document.form['address2'].value = document.form['deladdress2'].value;
	document.form['town'].value = document.form['deltown'].value;
	document.form['county'].value = document.form['delcounty'].value;
	document.form['postCode'].value = document.form['delpostCode'].value;
	document.form['country'].value = document.form['delcountry'].value;
	$("#delcountry").html(document.form['country'].value);
	if($("#usebilling").is(':checked')){
		$(".billing").attr("disabled","disabled");
	} else {
		$(".billing").attr("disabled","");
	}
}

function switch_view(id){
	var view = $("#view_proposed").html();
	if(view == "View Proposed") {
		$("#ok"+id).css("display","none");
		$("#proposed"+id).css("display","block");
		$("#view_proposed").html("View Live");
	} else {
		$("#ok"+id).css("display","block");
		$("#proposed"+id).css("display","none");
		$("#view_proposed").html("View Proposed");
	}
}

function edit_tag(tid,action,tgid){
	$('#taglist'+tgid).html("");
	$('#taglist'+tgid).load("tags.php?tid="+tid+"&mode="+action+"&tgid="+tgid,function(){
																	
	 });
}


function show_msg(msg){
		$('#show_msg').html(msg);
		$('#show_msg').slideDown();
}

function show_approval(){
	$("#approval").load("/admin/approval_list.php").slideDown();
	//$("#approval").;
}

function show_reject(id){
	$("#"+id).slideDown();
	//$("#approval").;
}

function reveal(id){
	$('#'+id).slideToggle();
	//return false;
}
function unreveal(id){
	//$('#'+id).slideUp();
	//return false;
}

