$(document).ready(function() {
	Cufon.replace(['h1, h2, h3, #home-nav, #nav, p.about'], {hover: true});
});

//GENERIC SITE----------------------------------------------------------------------------->

//preload images
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

//product details
function InitProductDetail(){
    //sub nav
    $('div#product-specs').addClass('hdn');
    $('div#product-manuals').addClass('hdn');
    $('div#product-print').addClass('hdn');

    $('#product-details LI A').click(function(evt) {
        evt.preventDefault();
	  
        var newContent = $(this).attr("rel");

    	switchProductTab(newContent);
      
    });
	
	$('.product-links A.print').click(function(evt) {
			 evt.preventDefault();
			  if($.browser.opera){$('html').animate({scrollTop: $('#product-details').offset().top}, 2000);} else $('html,body').animate({scrollTop: $('#product-details').offset().top}, 2000);
    
        var newContent = $(this).attr("rel");

    	switchProductTab(newContent);
		
			 });
	
	$('.info-links LI A').click(function(evt) {
        evt.preventDefault();
    	 if($.browser.opera){$('html').animate({scrollTop: $('#product-details').offset().top}, 2000);} else $('html,body').animate({scrollTop: $('#product-details').offset().top}, 2000);
    
        var newContent = $(this).attr("rel");

    	switchProductTab(newContent);
      
    });
	$('#btnPrintall').click(function() {
		window.open('/product/print.cfm?productuid='+$("#productuid").val()+'&'+'all=all',"mywindow",
    "location=0,status=1,scrollbars=1,width=700,height=900,toolbar=0");
		return false;
	});
	$("#chkSelectallfeature").click(function() {
		$("input[name='chkFeaturesection']").attr('checked', this.checked);		
	});
	$("#chkSelectallspec").click(function() {
		$("input[name='chkSpecsection']").attr('checked', this.checked);		
	});
	$('#btnPrintSelect').click(function() {
		var s_features=''; var s_specs='';
		$("input[name='chkFeaturesection']:checked").each(
			  function() {
				  s_features= this.value + ';' + s_features ; 
			  }
		);	
		$("input[name='chkSpecsection']:checked").each(
			  function() {
				  s_specs= this.value + ';' + s_specs  ; 
			  }
		);	
		
		if (s_features.length ==0 && s_specs.length == 0){
		 $.prompt('Please select some product information section to print.');
		}else
		window.open('/product/print.cfm?productuid='+$("#productuid").val()+'&'+'specs='+s_specs+'&features='+s_features,"mywindow",
    "location=0,status=1,scrollbars=1,width=700,height=900,toolbar=0");
		return false;
	});

}

function switchProductTab(newContent){
	  $("#product-details LI A").removeClass("selected");
      $("#product-details LI A[rel="+newContent+"]").addClass("selected");
        
        switch(newContent){
            case 'product-features':
                $('div#product-features').removeClass('hdn');
                $('div#product-specs, div#product-manuals, div#product-print').addClass('hdn');
                break;
            case 'product-specs':
				$('div#product-specs').removeClass('hdn');
                $('div#product-features, div#product-manuals, div#product-print').addClass('hdn');
                break;
            case 'product-manuals':
				$('div#product-manuals').removeClass('hdn');
                $('div#product-features, div#product-specs, div#product-print').addClass('hdn');
                break;
           case 'product-print':
		   		$('div#product-print').removeClass('hdn');
                $('div#product-features, div#product-specs, div#product-manuals').addClass('hdn');
                break; 
        }
}

function InitProductImages(){
    //test number of thumbnails
    var count = $('.product-move ul li').length;
    //hide initial state of pop up next prev links if there is only 1 image

    if(count < 2){
        $("#popupGallery A#prevBtn").css('visibility', 'hidden');
        $("#popupGallery A#nextBtn").css('visibility', 'hidden');   
    }
    //if count < 6
    if(count < 6){
        //Hide arrow btns
        $('div#product-carousel a.next').css('visibility', 'hidden');
        $('div#product-carousel a.prev').css('visibility', 'hidden');
    }

    //set initial values for links
    var prevItem = -1;
    var currentItem = 0;
    var nextItem = 1;
    var totalItems = $('.product-move ul li a').length;
    var galleryImages = new Array();

    $(".product-move ul li a").each(

        function() {
            galleryImages.push($(this).attr("href"));
            $( this ).bind (
                "click",
                function(evt){
                    evt.preventDefault();
                    prevItem = Number($(this).attr("class")) - 1;
                    currentItem = Number($(this).attr("class"));
                    nextItem = Number($(this).attr("class")) + 1;
                    populateLinks();
                    var newMainImage = $(this).attr("rel");
                    var newFullImage = $(this).attr("href");
                    $("#large-image").attr("src", newMainImage);
                    $("#gallery-image img").attr("src", newFullImage);
                }
            );
        }
    );

    

    //populate prev link
    $("#popupGallery A#prevBtn").attr("href", galleryImages[prevItem]);
    $("#popupGallery A#prevBtn").attr("class", prevItem);

    //populate next link
    $("#popupGallery A#nextBtn").attr("href", galleryImages[nextItem]);
    $("#popupGallery A#nextBtn").attr("class", nextItem);

    //hide prev link initially
    $("#popupGallery A#prevBtn").css('visibility', 'hidden');

    //handle lightbox thumbnail clicks

    $("ul#gallery-thumbs li a").click(function (evt) {
        evt.preventDefault();
        prevItem = Number($(this).attr("class")) - 1;
        currentItem = Number($(this).attr("class"));
        nextItem = Number($(this).attr("class")) + 1;

        populateLinks();

        var newFullImage = $(this).attr("href");
		
        //change main image
        $("#gallery-image img").attr("src", newFullImage);
    });

    

    $("#popupGallery A#prevBtn").click(function (evt) {
        evt.preventDefault();
        var newFullImage = $(this).attr("href");

        //change main image
        $("#gallery-image img").attr("src", newFullImage);

        prevItem = Number($(this).attr("class")) - 1;
        currentItem = Number($(this).attr("class"));
        nextItem = Number($(this).attr("class")) + 1;

        populateLinks();
    });

    

    $("#popupGallery A#nextBtn").click(function (evt) {
        evt.preventDefault();
        var newFullImage = $(this).attr("href");
        //change main image
        $("#gallery-image img").attr("src", newFullImage);
		
        prevItem = Number($(this).attr("class")) - 1;
        currentItem = Number($(this).attr("class"));
        nextItem = Number($(this).attr("class")) + 1;

        populateLinks();
    });

    

    function populateLinks(){
        //populate prev link
        $("#popupGallery A#prevBtn").attr("href", galleryImages[prevItem]);
        $("#popupGallery A#prevBtn").attr("class", prevItem);
		
        //populate next link
        $("#popupGallery A#nextBtn").attr("href", galleryImages[nextItem]);
        $("#popupGallery A#nextBtn").attr("class", nextItem);
		
        if(currentItem == 0){
            //hide prev btn
            $("#popupGallery A#prevBtn").css('visibility', 'hidden');
        }else{
            //show prev btn
            $("#popupGallery A#prevBtn").css('visibility', 'visible');
        }
        if(currentItem == totalItems - 1){
            //hide next btn
            $("#popupGallery A#nextBtn").css('visibility', 'hidden');
        }else{
            //show next btn
            $("#popupGallery A#nextBtn").css('visibility', 'visible');
        }
    }
    //product carousel
    $(".product-move").jCarouselLite({
        visible: 5,
        speed: 500,
        easing: 'easeInOutCubic',
        btnNext: ".next",
        btnPrev: ".prev",
        circular: false
    });
}


jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function checkUserGuide(){
 if (!document.product.fileName.value && !document.product.productType.selectedIndex){
	alert ("Please enter a model number or select a product category from the dropdown menu.");
	return false
	}

}

function InitProductHome(){
    //carousel
    $(".product-home-move").jCarouselLite({
        visible: 1,
        auto: 4000,
        speed: 1400,
        easing: 'easeInOutCubic',
        btnNext: ".nextBtn",
        btnPrev: ".prevBtn"
        //btnGo: [".hpMove1", ".hpMove2", ".hpMove3"]
    });
    
    $('#product-home-list LI A').mouseenter(function(evt) {
         evt.preventDefault();
        $(this).parent().addClass("selected");
    });
    $('#product-home-list LI A').mouseleave(function(evt) {
         evt.preventDefault();
        $("#product-home-list LI").removeClass("selected");
    });
    
    $('#product-home-list LI H3 A').mouseenter(function(evt) {
         evt.preventDefault();
        $(this).parent().parent().parent().addClass("selected");
    });
    $('#product-home-list LI H3 A').mouseleave(function(evt) {
         evt.preventDefault();
        $("#product-home-list LI").removeClass("selected");
    });

}




function goTo(obj) {
	if(obj.options[obj.selectedIndex].value != "")
	document.location = obj.options[obj.selectedIndex].value;
}

function InitProductList(){
    var count = 0;
	$("#frmProducts").clearForm();

    $( "input" ).click(
        function( objEvent ){
            if(this.checked == true ){
                //check that there are not already 6 items selected
                if(count != 5){
                    count = count + 1;
                    $(this).parent().find('LABEL').css('display', 'none');
                    $(this).parent().find('a.compare').css('display', 'block');
                }else{
					$.prompt('You cannot compare more than 5 items');                 
                    this.checked = false;
                }
            }else{
                count = count - 1;
                $(this).parent().find('LABEL').css('display', 'block');
                $(this).parent().find('a.compare').css('display', 'none');
            }
  
        }
    );

    $( "a.compare" ).click(
        function( objEvent ){
            if(count < 2){
               $.prompt('Please select at least 2 products to compare');
            }else{
               $('#frmProducts').submit();
            }
  
        }
    );
}

 $.fn.clearForm = function() {

        return this.each(function() {
  
          var type = this.type, tag = this.tagName.toLowerCase();

          if (tag == 'form')

            return $(':input',this).clearForm();
  
          if (type == 'text' || type == 'password' || tag == 'textarea')
  
            this.value = '';

          else if (type == 'checkbox' || type == 'radio')

            this.checked = false;

          else if (tag == 'select')

            this.selectedIndex = -1;
 
        });

      };

function OneLink(sHostname){ 
		document.location.href = document.location.protocol + "//" + sHostname + document.location.pathname + document.location.search;
		}
 