/**
	Slideshow
	id = wi-1 and wi-2
	crossfade 4 sec
	w-1 and w-2 ready
	temporary item to preload in cache w-1 or w-2
*/

$(window).ready(function () {
	var $speed = 4000;
	var $speedFade = 1000;
	var $w1 = $('#wi-0');
	var $w2 = $('#wi-1');
	var $wi1 = $('#wi-0 img');
	var $wi2 = $('#wi-1 img');
	var path = 'public/images/slideshow/';
	var N = 6;
	var $countUp = 0;
	var $flag = 0;
	var $firstLoad = true;
	var $awiB = [];//image src
	var $awiM = [];//image src
	var $awi = [];//image src
	
	var FullscreenrOptionsM = {  width: 1024, height: 454, bgID: 'img' };
	var FullscreenrOptionsB = {  width: 1920, height: 852, bgID: 'img' };

	for (i=0;i<=N;i++){
		$awiB.push(path+'HomePix'+(i+1)+'-big.jpg');
	}
	for (i=0;i<=N;i++){
		$awiM.push(path+'HomePix'+(i+1)+'-mini.jpg');
	}
	//INI
		if ( ($(window).height()<660) || ($(window).width()<1024) ){
			$awi = $awiM;
			$('#wi-0 img').fullscreenr(FullscreenrOptionsM);
			$('#wi-1 img').fullscreenr(FullscreenrOptionsM);
		} else {
			$awi =  $awiB;
			$('#wi-0 img').fullscreenr(FullscreenrOptionsB);
			$('#wi-1 img').fullscreenr(FullscreenrOptionsB);
		}
		//if height of image < windows height
		if ($(window).height()<=$('.wi img').height()+$('#menu').height()+$('#header').height()){
			$('.wi').css({position:'absolute',top:'0px',bottom:''});	
		} else {
			$('.wi').css({position:'fixed',top:'',bottom:0+'px'});	
		}
		
			if (!detectIApple()){
				$('#wi-0 img').animate({src:$awi[$countUp]}, 100);
			} else {
				$('#wi-0 img').delay($speed).attr({'src':$awi[$countUp]})
			}
	//RUN
	function animWi(i,j){
			$('#wi-'+i).fadeIn($speedFade,function(){
				if ($firstLoad){
					$firstLoad = false;
					if ($(window).height()<=$('#wi-0').height()+$('#menu').height()+$('#header').height()){
						$('.wi').css({position:'absolute',top:'0px',bottom:''});	
					} else {
						$('.wi').css({position:'fixed',top:'',bottom:0+'px'});	
					}
				}
			});
			if ($countUp >= N-1){
				$countUp = 0;
			} else {
				$countUp++;
			}
			$('#wi-'+j).fadeOut($speedFade,function(){
			if (!detectIApple()){
					$('#wi-'+j+' img').wait($speed).animate({src:$awi[$countUp]}, 100);
			} else {
				$('#wi-'+j+' img').delay($speed).queue(function(){
					var img1 = new Image();

					$('#wi-'+j).find('img').remove();
					$('#wi-'+j).append(img1);
					$(img1).addClass('ig');
					$('#wi-'+j+' .ig').attr({'src':$awi[$countUp]}).bind("load", function () {
						animWi(j,i);
					});;
				})
			}
			});
	}
	$('#wi-0 img').bind("load", function () {
			animWi(0,1);
	
	});
	$('#wi-1 img').bind("load", function () {
			animWi(1,0);
	});
	
	function detectIApple(){
		if((navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
		 if (document.cookie.indexOf("iphone_redirect=false") == -1) return true;
		}
		return false;
	}
	
});

$.fn.fullscreenr = function(options) {

    var defaults = { width: 1920,  height: 852, bgID: 'bgimg' };
    var options = $.extend({}, defaults, options); 
    $(document).ready(function() { 
        $(options.bgID).fullscreenrResizer(options);
	});
    $(window).bind("resize", function() {
        $(options.bgID).fullscreenrResizer(options);
		//if height of image < windows height
		  if ($(window).height()<=$('.wi img').height()+$('#menu').height()+$('#header').height()){
			$('.wi').css({position:'absolute',top:'0px',bottom:''});	
		  } else {
			$('.wi').css({position:'fixed',top:'',bottom:0+'px'});	
		  }
    });     
    return this;        
}; 

$.fn.fullscreenrResizer = function(options) {
    // Set bg size
    var ratio = options.height / options.width; 
    // Get browser window size
    var browserwidth = $(window).width();
    var browserheight = $(window).height();
    // Scale the image
   // if ((browserheight/browserwidth) > ratio){
  //      $(this).height(browserheight);
  //      $(this).width(browserheight / ratio);
 //  } else {
        $(this).width(browserwidth);
        $(this).height(browserwidth * ratio);
  //  }
    // Center the image
  //  $(this).css('left', (browserwidth - $(this).width())/2);
  //  $(this).css('top', (browserheight - $(this).height())/2);
    return this;        
    };

