// JavaScript Document


$(document).ready(function(){

$("ul.tabs").tabs("> .pane");
	 $("input.qty").keypress(function (e) {
	if($("#placeholder").attr('name')){
     // var value = $(this).val();
var code = (e.keyCode ? e.keyCode : e.which);
 if(code==48) { //Enter keycode
   //Do something
	  $("input.qty").val("1");
	  alert("Quantity must be greater than 1.");
 }

	
	}
    }).keyup();

    // load default 1st platform
		var name = $("#placeholder").attr('name'); // placeholder sku
	var title = $("#"+name).attr('alt');
	//var src = $('#'+name).attr("src");
	//$("#placeholder").attr("src", src);
    $("p#placeholder_content").text(title);
	
	var placeholder = $("#placeholder").attr("src"); // large image
	var platform = placeholder;
	$("p#placeholder_content").text(""); // set main text
	//$("#placeholder_content").append("Please rate: ");
     // stop normal link click

    //var menu = ["Platform", "Kit"];
    //for ( var i = 0; i <= 1; i++ ){
    // $("p#placeholder_content").append("<a href='#' title="+menu[i]+" >" + menu[i] + "</a>");
      // add markup to container and apply click handlers to anchors
	//}
	
	
 

   $("a.generate[href^=#?w=50]").click(function(active){
//	   console.log(this, $(this), active);
	   
 var tempsrc = $(this).attr('href');
	var kit_skus=tempsrc.match(/[A-Za-z0-9]+/g);
//alert(kit_skus[3]);
  // alert('SELECTED'+$(this).attr('class'));
   // $("div p.p2").html(""); // set main text
//	$("p.p3").html("");
    var name = $(this).attr('name');
	var src = $('#'+name).attr("src");
//	alert("here "+name+" .. "+src); // sku & image
    var flag=0;
/*	if($(this).hasClass('active_platform')){
		// remove selection
		var active = "active_platform";
		$('a.generate.aplatform.active_platform').removeClass(active);
		$(".pimg").css("border-color", "#ccc");
	}else if($(this).hasClass('active_kit')){
		// remove selection
		var active = "active_kit";
		$('a.generate.akit.active_kit').removeClass(active); 
		$(".kimg").css("border-color", "#fff");
	}else */
	if($(this).hasClass('aplatform')){ 
		var click_name=name;
		var off_name='kimg';
//		var model = $("p.pp2").attr('id');
		var model = $("div.p"+name+" p.pp2").attr('id');
		var old_id=$(".pimg").attr('id');
		var active = "active_platform";
		var old_name = $('a.generate.aplatform.active_platform').attr('name'); 
		$('a.generate.aplatform.active_platform').removeClass(active); 
		$(this).addClass(active);
		$("div.pimg").css("border-color", "#f5f5f5");
		$("div."+name).css("border-color", "#83adc4");
		var p1 = $("div.p"+name+" p.pp1").text();
		$("p.p1").html( p1 );
		var p2 = $("div.p"+name+" p.pp2").html();
		$("p.p2").html( p2 );
		var p3 = $("div.p"+name+" p.pp3").html();
		$("p.p3").html( p3 );
		var p4 = $("div.p"+name+" p.pp4").text();
		$("p.p4").html( p4 );
		$("#pthumb img").attr('src', "images/products_categories/th/"+name+"_th.jpg");
		$("#pthumb img").css('visibility', "visible");
		$("#model_1").attr('value', model);
//		$("select[name=skuColor1] option").attr('selected', "selected");
		$("span.Qty1").css('visibility', "visible");
		$("tr#quote1").css('background-image', "none");
		$("#model_2").attr('value', "");
		$("select[name=skuColor2] option").attr('selected', "");
		$("span.Qty2").css('visibility', "hidden");
		$("tr#quote").css('background-position', "50% 50%");
		flag=1;
		// remove kit
		var platform = name;
			$('a.generate.akit.active_kit').removeClass(active);
			$("div.kimg").css("border-color", "#EEE");
			$("p.k1").html('');
			$("p.k2").html('');
			$("p.k3").html('');
			$("p.k4").html('');
			$("#kthumb img").css('visibility', "hidden");
			var kit=0;
	}else if($(this).hasClass('akit')){ 
		var click_name=name;
		var off_name='pimg';
		var old_id=$(".kimg").attr('id');
//		var model = $("p.kk2").attr('id');
		var model = $("div.k"+name+" p.kk2").attr('id');
		var active = "active_kit";
		$('a.generate.akit.active_kit').removeClass(active); 
		$(this).addClass(active);
		$("div.kimg").css("border-color", "#EEE");
		$("div."+name).css("border-color", "#83adc4");
		var k1 = $("div.k"+name+" p.kk1").text();
		$("p.k1").html( k1 );
		var k2 = $("div.k"+name+" p.kk2").html();
		$("p.k2").html( k2 );
		var k3 = $("div.k"+name+" p.kk3").html();
		$("p.k3").html( k3 );
		var k4 = $("div.k"+name+" p.kk4").text();
		$("p.k4").html( k4 );
		$("#kthumb img").attr('src', "images/products_categories/th/"+name+"_th.jpg");
		$("#kthumb img").css('visibility', "visible");
		$("tr#quote").css('background-position', "-1000% -1000%");
		$("#model_2").attr('value', model);
		$("select[name=skuColor2] option").attr('selected', "selected");
		$("span.Qty2").css('visibility', "visible");
		flag=2;
		//var kit = $('#'+name).attr("src"); // get kit name 
		var kit = name;
			if ($('a.generate.aplatform.active_platform').attr('class')){
				// store active platform image else if not active use kit image only
				var pname = $('a.active_platform img').attr('id'); // small SKU
				//var platform = $('#'+pname).attr("src"); // get platform name
				var platform = pname;
				}else{ var platform = 0; }
	}

	
	var showcase = '';
	var cname = $(this+' img').attr('src');
	// NO AJAX METHOD: for images
	if (platform && kit){ 
		var showcase = "images/products_categories/showcases/"+platform+"_"+kit+".jpg";
	}else if (platform){
		var pname = $('a.active_platform img').attr('id');
		var showcase = "images/products_categories/lg/"+platform+".jpg";
	}else{
		var showcase = "images/products_categories/lg/"+kit+".jpg";
	}
	$("#placeholder").attr("name", click_name); $("#placeholder").attr("src", showcase);
	//$(".place1 p.p1").html(showcase);
	
		// remove kit
		
	// get active series and subseries
//	var sersub = $('ul.sub_active li a.active').text();
//	var series = $('ul > li.current > a.active').text();
	var series = $('.tabs a.current').text();
	var sersub = $('.tsub > a.current').text();
	temp=series.match(/([A-Z][a-z]+|[A-Z][a-z]+\s\w+)/g);
	series=temp[0];
	sersub=temp[1];
		if (flag==1){
	$("img.xkimg").css("z-index", "-1");
	$(".kimg").attr('name', function (arr){ // iterate all kits
		return arr;	
	}).each(function () { 
		 if($(this).attr('id')){
		 var kit=$(this).attr('id');
		 //var stringx=name+series+sersub+temp[3]+kit;  // match this kit
		 var stringx=name+kit;
		var flagsku=tempsrc.match(stringx);  // tempsrc is all showcases, match kit in showcases
		// if no match, then dim the img based on the sku
			//alert(stringx+" = "+flagsku+" & "+tempsrc);
			if(flagsku){
					$("img#x"+kit).css("z-index", "-1");
				
		}else{
					$("img#x"+kit).css("z-index", "3");
			}
		 }
	});

	if( $.browser.msie){
		userAgent = $.browser.version;
		userAgent = userAgent.substring(0,userAgent.indexOf('.')); 
		version = userAgent;

		if(version >= 9){
			  var xmlhttp;
   if (window.ActiveXObject)
     {
     xmlhttp=new ActiveXObject("Microsoft.XMLHttp");
     }
   else
     {
     xmlhttp=new XMLHttpRequest();
     } 	
if (xmlhttp!=null){
			$.post("/load-content/", {type: "POST", img: showcase, ser: series, subs: sersub, dataType: "XML"}, function(xml) {
       			// format and output result
	  			// alert(placeholder+' && '+$(this).html());
	   				 if(parseInt(xml)!=0)    //if no errors
	            {
				
				// $('ul#second-carousel li').css('visibility', 'hidden');
				// $('ul#second-carousel li').css('position', 'absolute');

				$(xml).find("title").each(function(){
			//			alert('li.'+$(this).html());
    					$('li.'+$(this).text()).css('visibility','visible');
						$('li.'+$(this).text()).css('position','relative');
					
  				});
				}
				
				xmlhttp=null;
			});
}
			return false;
		}else{
				  var xmlhttp;
   if (window.ActiveXObject)
     {
     xmlhttp=new ActiveXObject("Microsoft.XMLHttp");
     }
   else
     {
     xmlhttp=new XMLHttpRequest();
     } 
if (xmlhttp!=null){
	var numRand = Math.floor(Math.random()*101);
			var xml = null;
			$.ajax({type: "POST", url:"/load-content/", cache: false, data:"img="+showcase+"&ser="+series+"&subs="+sersub+"&rand="+numRand, async:false, dataType: "html", success: function(xml) {
       			// format and output result
	  			// alert(placeholder+' && '+$(this).html());
	   			 if(parseInt(xml)!=0)    //if no errors
	            {
				
			//	$('ul#second-carousel li').css('visibility', 'hidden');
			//	$('ul#second-carousel li').css('position', 'absolute');
				nshow = xml.match(/li[A-Z]{3}\d*/g);

				jQuery.each(nshow,function(){
    					$('li.'+this).css('visibility','visible');
						$('li.'+this).css('position','relative');
						
  				});
       		
				}
				xmlhttp=null;
	 }});
}
	
		// Set up the requestxmlhttp=null
		// code for Mozilla, etc.

//xmlhttp.onreadystatechange= function(){$("div p.p2").html("test"); alert(xmlhttp.responseXML);};		
//xmlhttp.open("POST","/load-content/",true);				
//xmlhttp.send("?img="+showcase+"&ser="+ series+"&subs="+sersub);
				
		
		return false;
		}
			
	return false;
	  }else{
			$.post("/load-content/", {type: "POST", img: showcase, ser: series, subs: sersub, dataType: "XML"}, function(xml) {
       			// format and output result
	  			// alert(placeholder+' && '+$(this).html());
	   			
			//	$('ul#second-carousel li').css('visibility', 'hidden');
			//	$('ul#second-carousel li').css('position', 'absolute');



				$(xml).find("title").each(function(){
				
    					$('li.'+$(this).text()).css('visibility','visible');
						$('li.'+$(this).text()).css('position','relative');
  				});
       		
				
				xmlhttp=null;
			});
}
			return false;
	}
	return false;
   });
	


 //When you click on a link with class of poplight and the href starts with a # 
$('a.poplight[href^=#?w=100]').click(function() {

	var aname = $(this).attr('id');
	var name = $("img#placeholder").attr('src');
	var asku = name.split('.');
	//var src = $('#'+name).attr("src");
	//var span_id = $('span.'+name).attr('id'); // get id from span based on class name
	
	jQuery("#pop_img").html('<img src="'+asku[0]+'_zm.jpg" width="800px" styly="z-index:2000"/>'); 
	
    var popID = $(this).attr('rel'); //Get Popup Name
    var popURL = $(this).attr('href'); //Get Popup href to define size

    //Pull Query & Variables from href URL
    var query= popURL.split('?');
    var dim= query[1].split('&');
    var popWidth = dim[0].split('=')[1]; //Gets the first query string value

    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({ 'width': '800px' }).prepend('<a href="#" class="close"><img src="includes/templates/freetemplate2/images/close.gif"  style="float: right;margin: -18px -18px 0 0;opacity: .80;" title="Close Window" alt="Close" /></a>');

    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
   var popMargTop = ($('#' + popID).height() + 2) / 2;
   var popMargLeft = ($('#' + popID).width() + 2) / 2;

    //Apply Margin to Popup
    $('#' + popID).css({
        'margin-top' : -popMargTop,
        'margin-left' : -popMargLeft
    });

    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

    return false;
});


 //When you click on a link with class of poplight and the href starts with a # 
$('a.poplight[href^=#?w=200]').click(function() {

	var aname = $(this).attr('name');
	var name = "images/products_categories/lg/"+aname+".jpg";

	//var span = $('#placeholder_content').text(); // get span content
	//var span = $('p.p1').text();
	var span = $(this).attr('title');
	jQuery("#pop_img").html('<div><img src="'+name+'" width="800px"/></div><div style="text-align:bottom; padding-top:10px; margin-bottom:-10px;">&nbsp;&nbsp;'+span+'</div>').prepend('<a href="#" class="close"><img src="includes/templates/freetemplate2/images/close.gif"  style="float: right; bottom:-15px; vertical-align:bottom; position:absolute; right:-15px;opacity: .80;" title="Close Window" alt="Close" /></a>'); 
	
    var popID = $(this).attr('rel'); //Get Popup Name
    var popURL = $(this).attr('href'); //Get Popup href to define size

    //Pull Query & Variables from href URL
    var query= popURL.split('?');
    var dim= query[1].split('&');
    var popWidth = dim[0].split('=')[1]; //Gets the first query string value

    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({ 'width': '800px' });

    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
  // var popMargTop = ($('#' + popID).height() + 2) / 2;
   var popMargLeft = ($('#' + popID).width() + 2) / 2;

    //Apply Margin to Popup
    $('#' + popID).css({
      //  'margin-top' : -popMargTop,
        'margin-left' : -popMargLeft
    });
	
	
    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
   
	$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

  if ($.browser.msie && $.browser.version.substr(0,1)<=7) {$('#fade').css({'z-index' : 0});}


    return false;
});

//Close Popups and Fade Layer
$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
    $('#fade , .popup_block').fadeOut(function() {
        $('#fade, a.close').remove();  //fade them both out
    });
    return false;
});

	
	
});
