var	filesToLoadTemplate		= new Array();
var	currentFileTemplate		= 0;
function loadFilesTemplate()
{
	if (typeof filesToLoadTemplate[currentFileTemplate] == 'string')
	{
		$.getScript(SCORE_URL + 'Includes/Javascripts/' + filesToLoadTemplate[currentFileTemplate], loadFilesTemplate);
		currentFileTemplate++;
	}
	else
	{
		executeTemplate();
	}
}
$(document).ready(loadFilesTemplate);

function executeTemplate()
{
	brandsInit();
	byInit();
	productInit();
	initTemplate();
}

// Templatefill
function initTemplate()
{
	var bodyHeight = $(window).height();
	var contentHeight = $('div#content').outerHeight(true);
	var logosHeight = $('div#logos').outerHeight(true);
	var menuHeight = $('div#menu').outerHeight(true);

	var	margin = bodyHeight-(contentHeight > menuHeight ? contentHeight : menuHeight)-logosHeight;
	if (margin > 0)
		$('div#logos').css('margin-top', margin);
		
	setInterval( "slideSwitch()", 5000 );
}

/* Product detail page */
function productInit()
{
	if ($('div#content.product div#images a').length)
		productFancyboxInit();
}
function productFancyboxInit()
{
	if (typeof $.fancybox == 'function')
		productFancyboxLoaded();
	else
		$.getScript(SCORE_URL + 'Includes/Javascripts/JQuery.Plugin.Fancybox.js', productFancyboxLoaded);
}
function productFancyboxLoaded()
{
	$('div#content.product div#images a').fancybox();
}

/* Menu brands */
function brandsInit()
{
	$('a[href=#brands]').click(brandsShow);
}
function brandsShow(e)
{
	$('ul#brands').slideDown(200);
	e.preventDefault();
	return false;
}

/* Menu by */
function byInit()
{
	$('div.by>a').click(byShow);
}
function byShow(e)
{
	$('div.by ul').slideUp(200);
	$('div.by ul' + $(this).attr('href')).slideDown(200);
	e.preventDefault();
	return false;
}

// Home slider
function slideSwitch()
{
    var $active = $('#slideshow a.active');

    if ($active.length == 0 )
    	$active = $('#slideshow a:last');

    var $next = ($active.next().length ? $active.next() : $('#slideshow a:first'));
    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
