/*

	Filename             sitewide.js
	Detail               Site functionality
	Author:              thunder::tech inc.
	License:             CLIENT is defined as the owner of online property from which this file resides or this code is referenced in.
						 ADDITIONAL PARTY is defined as anyone other than thunder::tech or CLIENT.
						 No right is granted to ADDITIONAL PARTY to sell, distribute, modify or otherwise transfer the following source code without explicit written permission by CLIENT or thunder::tech.

*/

/*  ================================
     Sitewide JavaScript
    ================================ */

thunder.client.project.pageLoaded = function()
{
	//thunder.client.modify.rollImages();
	//thunder.client.modify.linkOptions();
	//thunder.client.modify.tabSet();
	thunder.client.modify.treeMenu(false, true);
	thunder.client.modify.selfLabelFields();
	//thunder.client.modify.requireFields();
	//thunder.client.workarounds.alphaImages();	
}

$(thunder.client.project.pageLoaded);
$(window).ready(function(){ 	checkSubPage(); });
$(window).load(function(){ 	checkSubPage(); });

function checkSubPage(){
    var subContent = $(".subContent");
    var thunderNav = $(".thunder-nav");

    if ($("#isIdeastream").val() == 'true') {
        if (subContent.outerHeight() >= thunderNav.outerHeight()) {
            var newHeight = subContent.outerHeight() - 30;
            thunderNav.css("height", newHeight + "px");
        }
        else {
            var newHeight = thunderNav.outerHeight() - 38;
            $(".innerContent").css("height", newHeight + "px");
        }
    }
    else {        
        if (subContent.outerHeight() >= thunderNav.outerHeight()) {
            var newHeight = subContent.outerHeight() - 22;            
            thunderNav.css("height", newHeight + "px");
        }
        else {
            var newHeight = thunderNav.outerHeight() - 38;            
            $(".innerContent").css("height", newHeight + "px");
        }
    }

    
}

function initMenu(){
    $(".thunder-nav li a").mouseover(function() {
        $(".flyout").show();
        var x = 248;
        var y = $(this).position().top;
        var nWidth = $(".flyout .middle").width() - 24;		            
        $(".flyout .top .sliver").css("width", nWidth + 'px');
        $(".flyout .bottom .sliver").css("width", nWidth + 'px');
        $(".flyout").css("top", y + 'px').css("left", x + 'px');
    }).mouseout(function() {
        $(".flyout").hide();
    });
}

var totalImages = 0;
var currentIndex = 0;
var images;
var galleryBox;
function enlarge(img){
    var myImg = new Image();    
    $(myImg).load(function(){
        var t = '';
        $.each($(".galleryThumb"), function(i, item){
            t += '<a href="javascript: changeSwapper(\'' + $(item).attr("id") + '\');"><img src="' + $(item).find("img").attr("src") + '" style="width: 50px; margin: 0px 4px;"/></a>';
        });
        
        galleryBox = $(this).jBox().showWindow('<div style="width: 99%; text-align: center; padding: 2px;"><img src="' + img + '" id="swapper"/><br />' + t + '</div>', true);        
    })   
    .attr('src',img);       
}

function changeSwapper(img){
    var myImg = new Image();    
    $(myImg).load(function(){
        $("#swapper").attr("src",'/services/imagehandler.ashx?action=getLarge&image=' + img);
        galleryBox.position();               
    })   
    .attr('src','/services/imagehandler.ashx?action=getLarge&image=' + img);         
}


function productAdded(inventoryid) {
    var quantity = $("#quantityTxt");
    
    if (quantity.attr('value') > 0) {    
        var colorDD = $("#colorDD").val();
        var color = $("#" + colorDD + ' option:selected').text();
        
        showProductAdded(inventoryid, quantity.attr('value'), color)          
    }
    else {
        alert('Please enter a valid quantity!');
    }
}

function showProductAdded(inventoryid, quantity, color){
    if (inventoryid > 0 && quantity > 0) {
        $.ajax({
            type: "POST",
            url: "/services/inventoryHandler.ashx",
            dataType: "json",
            data: "action=addToCart&color=" + color + "&inventoryid=" + inventoryid + "&quantity=" + quantity,        
            success: function(mJSON){
                if (mJSON.code == '200'){
                    $("body").append(mJSON.response);
                    var productAddedContainer = $("#productAddedContainer");
                    productAddedContainer.fadeIn();
                    rePosition($(".productAdded"));
                                        
                    $(".cartItemsCount").html("Cart $" + mJSON.subTotal + " (" + mJSON.totalproducts + " Items)");
                }
                else {
                    alert('We encountered an issue..');
                }                           
            },
            error: function(e){
                alert('We encountered an issue.');
            }
        });  
    }   
    else {
        alert('We encountered an issue...');
    }
}

function keepShopping(){
    var productAddedContainer = $("#productAddedContainer");
    productAddedContainer.fadeOut();
}

function gotoCheckout(){
    var productAddedContainer = $("#productAddedContainer");
    productAddedContainer.fadeOut();
    window.location = '/cart.aspx';
}


function rePosition(obj) {
    
    var x = (($(window).width() / 2) - ($(obj).outerWidth() / 2)) + jQuery(window).scrollLeft();
    var y = jQuery(window).scrollTop() + 50;
    $(obj).css("left", x + "px").css("top", y + "px");
}

function gotoCustomerService() {
    $(this).jBox({'fixedWidth':'400px', 'fixedHeight':'250px'}).showFrame('/CustomerService.aspx', true);
}

function gotoShippingAndReturns() {    
    $(this).jBox({ 'fixedWidth': '700px', 'fixedHeight': '500px' }).showFrame('/ShippingReturn.aspx', true);
}

function haveASuggestion() {
    $(this).jBox({ 'fixedWidth': '350px', 'fixedHeight': '500px' }).showFrame('/suggestion.aspx', true);
}


function gotoLink(link) {
    window.location = link;
}
