var active_prod_nav = '#tab_Highlights';

function resetTabImages(){
    $(".tabImage").each(
        function(){
            newp = $(this).attr('src').replace('_selected','_up');
            $(this).attr('src',newp);
        }
    );
}

function updateUrl(anchor){
    /*p = event.target.href.split("#")[1];
    link = $("#"+event.target.href);*/
    window.location.href = anchor;
}

function checkAddressAndLoadContent(){
   var a = $.address.path();
    if(a.length == 1){
        a = 'highlights'; // set to default content
    }
	
    
	if(a.indexOf('/') != -1) {
		a = a.split('/')[1];
	}
	cased = a.charAt(0).toUpperCase() + a.substring(1).toLowerCase();
    cid = "#tab_"+cased;
	
    $(cid).trigger('click');
	//changeContent(cid);
}

function onOverlayLoad(hash){
    hash.w.center();
	hash.o.hide();
	hash.o.fadeIn(500);
    hash.w.fadeIn(500);
}
function onOverlayClose(hash){ 
    hash.w.fadeOut(500);
    hash.o.fadeOut(500, function() {hash.o.remove()});
    $("#photoOverlay img").attr('src','/img/spacer.gif'); 
}

function centerOverlay(){
	$("#photoOverlay").center();
}

function addImageListeners(){
    $('a.productOverlayLink').click(
        function(event){
            event.preventDefault();
            dest = $('#photoOverlay').children('img').eq(0);
            src = $(this).children('img').eq(0);
            big = $(src).attr('src').replace('_small','_large');
            $(dest).attr('src',big);
            $(dest).addClass('jqmClose');
            var jqmParams = {modal:true, overlay:80,onShow:onOverlayLoad,onHide:onOverlayClose};
            $("#photoOverlay").jqm(jqmParams);
            $("#photoOverlay").jqmShow();
    });
}

function initOverlayListener(){
    $(".overlayImage").load(function(event){
        centerOverlay();
    });
}

$(function(){
    $('.productDetailTab a').hover(
        function(){
            i = $(this).children('img').eq(0);
            newp = $(i).attr('src').replace('_up','_over');
            $(i).attr('src',newp);
        },
        function(){
            i = $(this).children('img').eq(0);
            newp = $(i).attr('src').replace('_over','_up');
            $(i).attr('src',newp);
        }
    );

    $('.productDetailTab a').click(
        function(event){
            event.preventDefault();
            updateUrl($(this).attr('href'));
            resetTabImages();
            i = $(this).children('img').eq(0);
            newp = $(i).attr('src').replace(/_(over|up)/,'_selected');
            $(i).attr('src',newp);
            content = $("#"+this.id.replace('tab_','data_')).html();
            $("#productViewLowerMiddle").html(content);
            addImageListeners();
        }
    );
    //initOverlayListener();
    centerOverlay();

	if (active_prod_nav !== '') {
    	img = $(active_prod_nav).children('img').eq(0);
    	newp = $(img).attr('src').replace('_up','_selected');
    	$(img).attr('src',newp);
	}

    $.address.change(function(event) {
		checkAddressAndLoadContent();				  
	});
	checkAddressAndLoadContent();
});

$(function(){
    $(".productButtons a").hover(
        function(){
            $(this).parent().removeClass('productButtonLinkWrapperOff');
            $(this).parent().addClass('productButtonLinkWrapperOn');
        },
        function(){
            $(this).parent().removeClass('productButtonLinkWrapperOn');
            $(this).parent().addClass('productButtonLinkWrapperOff');
        }
    );
});
