/*

	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
    ================================ */
var tabHeights = new Array();
thunder.client.project.pageLoaded = function () {
    //thunder.client.project.flashReplace();
    //thunder.client.modify.rollImages();
    //thunder.client.modify.linkOptions();
    //thunder.client.modify.tabSet();
    //thunder.client.modify.treeMenu(false);
    //thunder.client.modify.treeMenu(false, [thunder.client.modes.separatorTreeMenu]);
    thunder.client.modify.treeMenu(false, [thunder.client.project.accentTreeMenu]);
    thunder.client.modify.selfLabelFields();
    //thunder.client.modify.requireFields();
    //thunder.client.modify.dropSelector(true);
    //thunder.client.modify.scrollFeature(640, 3000, 500);
    thunder.client.workarounds.alphaImages();
    thunder.client.workarounds.labelAsBrowser();
    $('.search-title').click(thunder.client.project.selectSearch);
    $('.hide-feature-search').mouseover(thunder.client.project.deselectSearch);
    thunder.client.project.setGlowHeight();


    $('.tab-window').each(function (i, item) {
        tabHeights[i] = $(item).innerHeight();
        $(item).attr("id", i);
    });

    $('.tab').click(thunder.client.project.tab);

    $('.defaultTab').trigger('click');

    $('.category-link-button').mouseover(function () { $(this).find('.category-over-image').stop(true, false).animate({ opacity: 1 }, { duration: 300 }); }).mouseout(function () { $(this).find('.category-over-image').stop(true, false).animate({ opacity: 0 }, { duration: 300 }); });
    $('.category-over-image').css('opacity', 0);
    thunder.client.project.flashReplace();

    //thunder.client.project.initNutrition();    

    $('.adjusted-more').css('left', $('.adjusts-more').width() + 100);
    $(window).load(thunder.client.project.setGlowHeight);    
}


function hide()
{
	$(this).css('display','none');
}

thunder.client.project.setGlowHeight = function () {
    $('.page-glow').each(function () { $(this).height($(this).parent().height() + 60); });
    $('.blog-glow').each(function () { $(this).height($(this).parent().height() + 60); });
}


var popupAvailable = false;
thunder.client.project.initNutrition = function () {
    if ($(".nutrition-popup").length > 0) {
        popupAvailable = true;
        var mHtml = $(".nutrition-popup").html();
        $(".nutrition-popup").remove();
        $("body").append('<div class="nutrition-popup">' + mHtml + '</div>');

        $(window).resize(thunder.client.project.setNutritionPopupPosition);
        $(window).scroll(thunder.client.project.setNutritionPopupPosition);
    }
}


thunder.client.project.setNutritionPopupPosition = function () {
    if (popupAvailable) {
        var x = $(".nutLink").offset().left + $(".nutLink").outerWidth() + 20; //(($(window).width() / 2) - ($(".nutrition-popup").outerWidth() / 2)) + jQuery(window).scrollLeft();
        var y = (($(window).height() / 2) - ($(".nutrition-popup").outerHeight() / 2) + jQuery(window).scrollTop());

        $(".nutrition-popup").css("top", y + "px").css("left", x + "px");
    }
}

thunder.client.project.nutritionOpen = function () {
    if (popupAvailable) {
        thunder.client.project.setNutritionPopupPosition();
        $(".nutrition-popup").show();
    }
}

thunder.client.project.nutritionClose = function () {
    if (popupAvailable) {
        thunder.client.project.setNutritionPopupPosition();
        $(".nutrition-popup").hide();
    }
}

thunder.client.project.tab = function () {
    //    $(this).next().fadeToggle();
    $(".tab-window").removeClass('defaultOpen');


    t = $(this).next();

    $('.tab-window-on').removeClass('tab-window-on').stop(true, false).animate({ height: 0 }, { complete: hide, queue: false, duration: 500 });

    t.addClass('tab-window-on')
    .stop(true, false)
    .show()
    .animate({ height: tabHeights[$(t).attr('id')] }, { queue: false, duration: 500, complete: function () { thunder.client.project.setGlowHeight(); } });

}

thunder.client.project.tab2 = function () {
    //    $(this).next().fadeToggle();
    $(".tab-window").removeClass('defaultOpen');
    t = $(this).next();
    $('.tab-window-on').removeClass('tab-window-on').stop(true, false).animate({ height: 0 }, { complete: hide, queue: false, duration: 500 });
    t.addClass('tab-window-on').stop(true, false).css('display', 'block').animate({ height: 290 }, { queue: false, duration: 500 });

}

thunder.client.project.accentTreeTimer = 'none';

thunder.client.project.accentTreeCollapse = function()
{
	thunder.client.project.accentTreeTarget.children('a').css('font-style', '');
	thunder.client.project.accentTreeTarget.find('.tertiary').css('display', 'none');
	thunder.client.project.accentTreeTarget.parent().parent().stop(true, false).animate({left: 0, height: 223, width: 220}, {duration: 500, queue: false}).find('.dropdown-background').stop(true, false).animate({height: 223, width: 220}, {duration: 500, queue: false});
	thunder.client.project.accentTreeTimer = 'none';
}

function appendErro(str){
   throw new Error("DEBUG: "+str)
}

function li(str){
   setTimeout("appendErro('"+str+"')", 1)
}

thunder.client.project.accentTreeMenu = {
	over: function(obj)
	{
		var o = $(obj);
		if(o.hasClass('tertiary-container'))
		{
			if(thunder.client.project.accentTreeTimer!='none')
			{
				clearTimeout(thunder.client.project.accentTreeTimer);
				thunder.client.project.accentTreeTimer = 'none';
				$('.tertiary-container').not(this).children('a').css('font-style', '');
				$('.tertiary-container').not(this).children('.tertiary').css('display', 'none');
			}
			o.find('.tertiary').css('display', 'block');

			var rightAmount = parseInt(o.parents('.thunder-nav').attr('thunder:right'));
			var leftAmount = -150 - rightAmount;
			o.parent().parent().stop(true, false).animate({left: leftAmount, height: 223, width: 517}, {duration: 500, queue: false}).find('.dropdown-background').stop(true, false).animate({height: 223, width: 517}, {duration: 500, queue: false});
			o.children('a').css('font-style', 'italic');
		}
		else
		{
			o.find('.accent').stop(true, false).animate({width: o.width()}, {duration: 500, queue: false});
			o.find('.dropdown-background').stop(true, false).animate({height: 223, width: 220}, {duration: 500, queue: false});
			o.find('.thunder-dropdown').stop(true, false).animate({left: 0, height: 223}, {duration: 500, queue: false});
		}
	},
	out: function(obj)
	{
		var o = $(obj);
		if(o.hasClass('tertiary-container'))
		{
			if(thunder.client.project.accentTreeTimer=='none')
			{
				thunder.client.project.accentTreeTimer = setTimeout(thunder.client.project.accentTreeCollapse, 2000);
				thunder.client.project.accentTreeTarget = o;
			}
		}
		else
		{
			if(thunder.client.project.accentTreeTimer!=='none')
			{
				clearTimeout(thunder.client.project.accentTreeTimer);
				thunder.client.project.accentTreeTimer = 'none';
			}
			o.find('.accent').stop(true, false).animate({width: 0}, {duration: 500, queue: false});
			o.find('.dropdown-background').stop(true, false).animate({height: 0, width: 220}, {duration: 500, queue: false});
			o.find('.thunder-dropdown').stop(true, false).animate({left: 0, height: 0}, {duration: 500, queue: false});
			o.find('.tertiary').css('display', 'none');
		}
	},
	init: function(obj)
	{
	}
}

thunder.client.project.deselectSearch = function()
{
	p = $('.feature-search');
	p.children().stop(true, false).removeClass('search-section-on').animate({height: 20}, {duration: 500, queue: false});
	p.animate({height: 60}, {duration: 500, queue: false});
}

thunder.client.project.selectSearch = function()
{
	var p = $(this).parent();
	$('.search-section').not(p).stop(true, false).removeClass('search-section-on').animate({height: 20}, {duration: 500, queue: false});
	p.addClass('search-section-on').stop(true, false).animate({height: 80}, {duration: 500, queue: false});
	p.parent().animate({height: 118}, {duration: 500});
}

thunder.client.project.flashReplace = function () {    
    $('.swf-banner').flash(
		{
		    swf: '/themes/main/flash/grecian-banner4.swf',
		    height: 288,
		    width: 929,
		    allowFullScreen: true,
		    allowScriptAccess: 'always',
		    hasVersion: 9,
		    wmode: 'transparent',
		    flashvars: {
		        basePath: ''
		    }
		}
	);
}

$(window).load(thunder.client.project.pageLoaded);








$(document).ready(function () {
    $(".collapsible legend").click(function () {
        var parent = $(this).parent(".collapsible");
        if ($(parent).hasClass("collapsed")) {

            $(this).parent(".collapsible").find("div").slideDown(500, function () {
                $(this).parent(".collapsible").removeClass("collapsed");
                $(this).parent(".collapsible").addClass("expanded");

                //we have completed the slide..check for the height of the div
                var divHeight = $(this).parent(".collapsible").find("div").outerHeight();
                var divHeightPlusScroll = divHeight + $(this).parent(".collapsible").find("div").offset().top;



                var windowHeight = $(window).height();
                var windowHeightPlusScroll = windowHeight + jQuery(window).scrollTop();



                if (divHeightPlusScroll >= windowHeightPlusScroll) {
                    scrollToObjectv2($(this).parent(".collapsible"));
                }


                thunder.client.project.setGlowHeight();
            });
        }
        else {
            $(this).parent(".collapsible").find("div").slideUp(500, function () {
                $(this).parent(".collapsible").removeClass("expanded");
                $(this).parent(".collapsible").addClass("collapsed");
                thunder.client.project.setGlowHeight();
            });
        }
    });
});

function scrollToObjectv2(obj, callBckFcn) {
    var y = $(obj).offset().top;
    $('html, body').animate({ scrollTop: y }, function () {
        if (typeof callBckFcn == 'function') callBckFcn.call(this);
    });
}

