//Products are now gained from the database products.js.aspx

var CurrentSectionIdx = 0;
var currentProductSection;
var transitioning = false;
var backupIfr = "";
/*
* Sets up page navigation stuff. Called each time
* a new page is loaded.
*/
var ProductOrderArray;
var ProductOrderArraySelectedIndex = 0;
function initPageNav() {
    initItems();
    initProductSortArray();
    $(document).ready(function () {

    });
}
function initPageNav2() { }
function initItems() {

    var items = $(".item");
    for (var i = 0; i < items.length; i++) {
        var item = $(items[i]);
     
        item.children('.mover').children('a').tooltip({
            track: false,
            delay: 0,
            showURL: false,
            showBody: " - ",
            fade: 250
        });
      
        //item.css('height', item.height() + 10);
        // item.children(".item_name").hide();
        item.mouseenter(function () {
            //$(this).children(".mover").animate({ "margin-top": -16 }, 100);
            $(this).children(".mover").children("a").children(".rollerthumb").animate({ "max-height": 240, "max-width": 160 }, 100);
        });
        item.mouseleave(function () {
            $(this).children(".mover").children("a").children(".rollerthumb").animate({ "max-height": 180, "max-width": 160 }, 100);
            
            //$(this).children(".mover").animate({ "margin-top": 0, "width": 150 }, 200);

        });
        item.click(handleItemClick);
    }
}
function initScrollBar() {
	$('.products').jScrollPane({reinitialiseOnImageLoad: false, scrollbarWidth: 23});
	$('.jScrollPaneContainer').css('width', $('.main_content').width());
	$('.products').css('width', $('.main_content').width());
}
var JQ_options = {
    zoomWidth: 500,
    zoomHeight: 440,
    xOffset:10,
    yOffset: 0,
    position: "right" //and MORE OPTIONS
};
/*
Sets up page for individual product
*/
function initProductPage() {
    if ($('#.product_info .video').is(":visible")) {
        currentProductSection = $('.product_info .video');
        $('.product_info .info').hide();
    } else {
        currentProductSection = $('.product_info .info');
        $('.product_info .info').show();
    }
	
	$('.product_info .specs').hide();
	$('.video').height($('.product_info').height() + 70);
	$('.specs').height($('.product_info').height() -20);
	$('.info').height($('.product_info').height() -20);
	
	$('.video_button').click(function() {
		showSection($('.video'), $(this), "hidden");
	});
	$('.specs_button').click(function() {
		showSection($('.specs'), $(this), "hidden");
	});
	$('.info_button').click(function() {
		showSection($('.info'), $(this), "auto");
    });
    $('.dealers_button').click(function () {
        showSection($('.dealer'), $(this), "auto");
    });
	var viewButtons = $('.view_col li');
	for (var i = 0; i < viewButtons.length; i++) {
	    var button = $(viewButtons[i]);
	    button.click(function () {
	        if (!$(this).hasClass('selected_thumb')) {
	            showImage($(this).attr('id'))

	        }
	    });
	    button.mouseenter(function () {
	        if (!$(this).hasClass('selected_thumb')) {
	            $(this).addClass('bordered');
	        }
	    });
	    button.mouseleave(function () {
	        if (!$(this).hasClass('selected_thumb')) {
	            $(this).removeClass('bordered');
	        }
	    });
	}
    var newLink = $(".photo_view #jq_photoThumb_0");    
	//newLink.jqzoom(JQ_options);

	$('.photo_view div').hide();
	$('.photo_view div').first().show();
	$('.photo_view div').first().addClass('current');
	$('.view_col li').first().addClass('bordered');
	$('.view_col li').first().addClass('selected_thumb');

	backupIfr = $('.video').html();
    /*
	$('.jqzoom').mouseenter(function () {
	    if ($('#.product_info .video').is(":visible")) {
	        currentProductSection = $('.product_info .info');
	        $('.product_info .info').show();
	        $('.product_info .video').hide();
        } 
	});
    */
}

function showImage(imageName) {
    var newLink = $(".photo_view #jq_" + imageName);
	var newPhoto = $(".photo_view #" + imageName);
	var oldPhoto = $(".photo_view .current");
	var oldPhotoThumb = $(".view_col .selected_thumb");
	oldPhotoThumb.removeClass('selected_thumb');
	oldPhotoThumb.removeClass('bordered');
	var newPhotoThumb = $(".view_col #" + imageName);
	newPhotoThumb.addClass('selected_thumb');
	newPhotoThumb.addClass('bordered');
	oldPhoto.removeClass('current');
	oldPhoto.hide();
	newPhoto.addClass('current');
	newPhoto.fadeIn();

	//newLink.jqzoom(JQ_options);
	//$('enlarge_button').attr('href', 'images/product/');
}

function showSection(section, button, overflowType) {
    //$(".video").html('');

    if (!transitioning) {
        transitioning = true;
        if (isIE) {
            $('.video').hide();
        } else {
            if ($(section).attr('class') == 'video') {
                $('.video').html(backupIfr);
            } else {
                $('.video').html('');
            }
        }
   
        $('.product_nav .selected').removeClass('selected');
		button.addClass('selected');
		currentProductSection.hide();	
        
        	
		currentProductSection = section;
		$('.product_info').css("overflow", overflowType);

		if (!isIE) {
		    currentProductSection.fadeIn(function () {
		        transitioning = false;
		    });
		} else {
		  //  alert(currentProductSection);
		    currentProductSection.show();
		    transitioning = false;
		}
	
	
	}
}

function handleItemClick() {
    var productName = $(this).attr("id");
    getSelectionIndex(productName);
   // console.log(productName);
	var productPath = products[productName];
	loadPage(productPath, null);
}
function getSelectionIndex(id) {
    ProductOrderArraySelectedIndex = 0;
    //console.log("LOOK FOR ID: " + id);
    $.each(ProductOrderArray, function () {
       // console.log("PRODUCT ARRAY: " + this);
        if (this == id) {
           // console.log("found it"); 
            return false;
        }
        ProductOrderArraySelectedIndex++;
    });
  //  console.log("ProductOrderArraySelectedIndex",ProductOrderArraySelectedIndex);
}
function flipProductPage(dir) {
    newIndex = ProductOrderArraySelectedIndex + dir;
   // console.log("NewIndex:" + newIndex);
    if (newIndex>=ProductOrderArray.length) {
     //  console.log("OVERSIZE" );
        newIndex=0;
    }
    if (newIndex < 0) {
     //  console.log("Below zero");
        newIndex = ProductOrderArray.length-1;
    }
    ProductOrderArraySelectedIndex = newIndex;
   // console.log("Arr Length: " + ProductOrderArray.length);
 //   console.log("Flip: " + ProductOrderArraySelectedIndex);

     var productName = ProductOrderArray[ProductOrderArraySelectedIndex];

     var productPath = products[productName];

 //   console.log("Flip: " + productName);
 //    console.log("Flip: " + productPath);
	loadPage(productPath, null,dir);

}
function initProductSections() {
    
	var sectionButtons = $('.sub_nav li');
	var sections = $('.product_section');
	sections.hide();
	$('.sub_nav li').removeClass('selected');
    $(sectionButtons[CurrentSectionIdx]).addClass('selected');
	$(sections[CurrentSectionIdx]).show();
	
	sectionButtons.click(sectionButtonHandle);
	$('.more_info').hide();
	$('.more_info').first().show();
	$('.products').height($('.products').height() - 53);

}

function fixThemBars() {
    var transparentBackgrounds = $('.transparentBackground');
   // console.log(transparentBackgrounds);
    for (var i = transparentBackgrounds.length - 1; i >= 0; i--) {
        var tb = $(transparentBackgrounds[i]);
        // find width and height of parent
        var width = tb.parent().width();
        var height = tb.parent().height();

        var browserwidth = $(window).width();
        var browserheight = $(window).height();
        // set transparent background to match parent
        tb.css("width", width);
        tb.css("height", height);

    }
}

function sectionButtonHandle() {


	var oldSectionButton = $('.sub_nav .selected');
	oldSectionButton.removeClass('selected');
	var oldSection = $('#sec_' + oldSectionButton.attr('id'));

	var newSectionButton = $(this);
	var newSection = $('#sec_' + newSectionButton.attr('id'));
	newSectionButton.addClass('selected');
	CurrentSectionIdx = parseInt(newSectionButton.attr('idx'));

	oldSection.hide();
	newSection.show();

	//  console.log('WANNA SHOW: #more_info_' + newSectionButton.attr('id'));

	if (newSectionButton.attr('bgid') != null) {
	    CurrentBackgroundID = newSectionButton.attr('bgid');
	    doReload();
	}

	$('.more_info').hide();
	$('#more_info_' + newSectionButton.attr('id')).show();
	fixThemBars();

	initScrollBar();
	initProductSortArray();
}
function initProductSortArray() {
    ProductOrderArraySelectedIndex = 0;
    ProductOrderArray = [];
    $(".products").find(".item:visible").each(function () { ProductOrderArray.push(this.id); });
   // console.log(ProductOrderArray);

}
function initCollectionPage() {
	var overlays = $('.overlay');
	overlays.hide();



	$('.close_button').hide();

    $('.product_thumbs div').mousedown(function() {
		$(this).click(handleItemClick);
	});
	
	    /*
    var buttons = $('.product_thumbs div');
    for (var i = 0; i < buttons.length; i++) {
		var button = $(buttons[i]);
		button.mousedown(function() {
			var oldOverlay = $('.selected_overlay');
			oldOverlay.removeClass('selected');
			oldOverlay.hide();
			$('.close_button').hide();
			var productName = $(this).attr('id');
			var overlay = $('#' + productName + "_overlay");
			overlay.fadeIn();
			overlay.addClass('selected_overlay');
			$('.close_button').fadeIn();
		});
	}
	$('.close_button').mousedown(function() {
		var oldOverlay = $('.selected_overlay');
		oldOverlay.removeClass('selected');
		oldOverlay.hide();
		$(this).hide();
	});
    */
}
