// Main onload handler


/* tid = setInterval(function() {
        if ( window.blurred ) { return; }    
        time -= delta;
        if ( time <= 0 ) {
            clearInterval(tid);
            myFunction(); // time passed - do your work
        }
    }, delta);
*/

window.onblur = function() { pause_slide(); };
window.onfocus = function() { if (showcase_auto) { resume_slide(showcase_intervalID); } };

var showcase_intervalID = '';
var showcase_auto = true;
var showcase_auto_interval = 8000;
var current_slide_index = 1;
var slide_cnt = 6;

$(function() {
//window.onload = function() {
	// Remove the no-js class from the html element
	document.documentElement.removeAttribute('class');

	// Call the functions which have been flagged  from the requesting view
	for (var i in onload_funcs)
	{
		window[onload_funcs[i]]();	
	}

	showcase_intervalID = window.setInterval(next_slide_pre, showcase_auto_interval);

	// Hovering over the slider causes it to pause. Take care of the hover event here
	$('#showcase').hover(
		function () {
			if (showcase_auto) {
				pause_slide(); // disable autoscrolling on hover in
			}
		}, 
		function () {
			if (showcase_auto) {
				resume_slide(showcase_auto_interval); // enable autoscrolling on hover in
			}
		}
	);
//}
});


/*
 * Resume (or start) the timer on the scroller
 */
function resume_slide(interval)
{
	window.clearInterval(showcase_intervalID);
	showcase_intervalID = window.setInterval(next_slide_pre, showcase_auto_interval);
}

/*
 * Pause (or stop) the timer on the scroller
 */
function pause_slide()
{
	window.clearInterval(showcase_intervalID);
	showcase_intervalID = '';
}

function next_slide_pre()
{
	if (current_slide_index + 1 > slide_cnt)
	{
		var next_slide_index = 1;
	}
	else
	{
		var next_slide_index = current_slide_index + 1;
	}
	
	next_slide(next_slide_index);
}

function next_slide(to_target_slide)
{
	if (current_slide_index == to_target_slide) return false;
	
	if (current_slide_index == 1)
	{
		$('#main_slide').fadeOut();
		$('#secondary_slides').fadeIn();
		$('#slide_'+to_target_slide).fadeIn();
	}	
	else if (to_target_slide == 1)
	{
		$('#main_slide').fadeIn();
		$('#secondary_slides').fadeOut();
		$('#slide_'+current_slide_index).fadeOut();
	}
	else
	{
		$('#slide_'+to_target_slide).fadeIn();
		$('#slide_'+current_slide_index).fadeOut();
	}
	
	
	$('#slider_nav_'+current_slide_index).removeClass('selected');
	$('#slider_nav_'+to_target_slide).addClass('selected');
	
	current_slide_index = to_target_slide;
	
}




/*
 * smooth_anchors_init
 * Handler for animating moves between anchors on the same page
 */
function smooth_anchors_init()
{
	// Manage clicks on links winthin their own page
	// Smooth scroll to hash tag
	$("a[href*=#].smooth").click(function(e) {
		e.preventDefault();
		
		var $parent = $(this).parent();
		if (! $parent.hasClass('accordion_closed') && $parent.hasClass('accordion_header')) { return; }

      // duration in ms
      var duration = 500;

      // easing values: swing | linear
      var easing = 'swing';

      // get / set parameters
      var newHash = this.hash;
      //var target = $(this.hash + ', a[name='+this.hash.slice(1)+']').offset().top;
		var target = $(document.getElementById(newHash.slice(1))).offset().top;

      var oldLocation = window.location.href.replace(window.location.hash, '');
      var newLocation = this;

      // make sure it's the same location      
      if(oldLocation + newHash == newLocation)
      {
         // set selector
         if($.browser.safari) var animationSelector='body:not(:animated)';
         else var animationSelector='html:not(:animated)';

         // animate to target and set the hash to the window.location after the animation
         $(animationSelector).animate({ scrollTop: target }, duration, easing, function() {

            // add new hash to the browser location
            //window.location.href = newLocation;
         });
      }
   });	
}


/*
 *	accordions_init
 * Initializes events handlers for the showcase slider
 */
function slide_toggle_btn_init()
{
	// Control the visibility of the slider
	$('#slide_toggle_btn').click(function(e) {
		e.preventDefault();
		if ($('#slide_toggle_btn').hasClass('opened'))
		{
			$('#showcase_wrapper .slide_2 img').animate({'margin-top': -50}, 500);
			$('#showcase_wrapper').animate( {
				'padding-top' : 0,
				'margin-bottom' : 0
			}, 500, 'easeOutQuad', function() {
				$('#slide_toggle_btn').removeClass('opened');
			});
		}
		else
		{
			$('#showcase_wrapper .slide_2 img').animate({'margin-top': 0}, 500);
			$('#showcase_wrapper').animate( {
				'padding-top' : 372,
				'margin-bottom' : -10
			}, 500, 'easeOutQuad', function() {
				$('#slide_toggle_btn').addClass('opened');
			});
		}
	});	
}

/*
 *	accordions_init
 * Initializes events handlers for the accordions
 */
function accordions_init()
{
	// Manage clicks on the accordion headers
	// open or close the accordion content
	$('h2.accordion_header').click(function(e) {
		if ($(this).hasClass('accordion_closed'))
		{
			$(this).next().slideDown(300);
			$(this).removeClass('accordion_closed');
		}
		else
		{
			$(this).next().slideUp(300);
			$(this).addClass('accordion_closed');
		}
	});	
}

 
function toggle_grid()
{
	$(document.body).toggleClass('show_grid');
}

function init_rating_ctrl()	
{
	$('#rating_ctrl_1 a').click(function(e) {
		e.preventDefault();
		var value = parseInt(this.getAttribute('id').substr(1));
		if (value < 1 || value > 5) { return false; }
		$('#rating_sel_1').val(value);
		//alert(parseInt(this.getAttribute('id').substr(1)));
	});
}

