/* ================================
 * OVERALL COMPONENTS CONFIGURATION
 * --------------------------------
 * Simply change as necessary...
 * ================================ */

var global = {
    // Background image transitioning
    background: {
        pauseTime:      5, // seconds
        animationSpeed: 800 // [ str ('fast'|'slow'|'ease') | int split-seconds ]
                            // `-> Basically any animation input accepted by jQuery
    },

    // Nivo Slider
    nivo: {
        pauseTime: 15 // seconds
    }
};




/* =============================================
 * ONLY THE BRAVE SHALL EDIT BELOW, MUAHAHAHAHA!
 * ============================================= */

var backgroundTransition = null; // Backhground transitioning timer global

$(document).ready(function()
{
    // Initiate and setup the superfish drop-down menu
    if ( jQuery().superfish ) $('.superfish').superfish();

   
    // NIVO Slider
    if ( jQuery().nivoSlider ) $('.nivoSlider').nivoSlider({ pauseTime: (global.nivo.pauseTime * 1000) });

    // Initiate the jQuery calendar
    //$('div#calendar').datepicker();

    // Hack to remove main link's links
    $('ul.main_nav li:has(ul) > a:not(:contains("Contact Us"))').attr('href', '#');
});

function switchBackground( setTimer, restart )
{
    // Background image transitions
    var setTimer    = !!setTimer,
        restart     = !!restart,
        background  = $('#backgroundImage.background:visible').size() > 0 ? $('#backgroundImage.background:visible') : $('#backgroundImage.background:first'),
        
        next        = background.next('#backgroundImage.background:not(:visible)').size() > 0 ? background.next('#backgroundImage.background:not(:visible)') : $('#backgroundImage.background:first');
        		//console.log(next);
        		var  image       = next.css('background-image').match(/\(("|')?http:\/\/[a-zA-Z0-9\.]+(\/[a-zA-Z0-9\./:]+)("|')?\)/i)[2],
        element     = null;

    // If no next background is found, assume the first one again
    if ( next.size() == 0 ) next = $('div.background:first');

//    console.log('-');
//    console.log('setTimer: ' + setTimer);
//    console.log('restart: ' + restart);
//    console.log('.. preloading next image: ' + image);
    
    // Preload the image first
    element = $('<img src="' + image + '" />');
    element.bind('load', function()
    {
//        console.log('image preloaded');

        // Restart at the beginning
        if ( restart == true )
        {
//            console.log('restarting...');
            background.fadeTo(global.background.animationSpeed, 1, function()
            {
//                console.log('fade ended');
                $(this).show();
                if ( setTimer ) backgroundTransition = setTimeout('switchBackground(true)', (global.background.pauseTime * 1000));
            });
        }

        else
        {
//            console.log('... no restart');
            background.fadeTo(global.background.animationSpeed, 0, function(){ $(this).hide(); });
            next.fadeTo(global.background.animationSpeed, 1, function()
            {
//                console.log('fade ended');
                $(this).show();
                if ( setTimer ) backgroundTransition = setTimeout('switchBackground(true)', (global.background.pauseTime * 1000));
            });
        }                
    });
}


function showBlock(Type,e)
{
	
	$('#popup').load('templates/popupfiles/'+Type+'.html', function(response, status, xhr) {
		if(status != 'error') 
		{
			$('#popup').html(response);
			$('#popup').css({
				'left':e.pageX+20,
				'top':e.pageY,
				'display':'block'
				
				
			});
			//console.log(e.clientY);
		}
		});
	
	
	

}

function hideBlock()
{
	$('#popup').html('');
	$('#popup').css({
		'display':'none'
	});
}
