/*
Supersized - Fullscreen Slideshow jQuery Plugin
By Sam Dunn (www.buildinternet.com // www.onemightyroar.com)
Version: supersized.2.0.js // Relase Date: 5/7/09
Website: www.buildinternet.com/project/supersized
Thanks to Aen for preloading, fade effect, & vertical centering
*/

var globalSliderCnt = 0;

(function($){

	//Resize image on ready or resize
	$.fn.supersized = function() {
		$.inAnimation = false;
		$.paused = false;
		var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
		
		$(window).bind("load", function(){
			$('#loading').hide();
			$('#supersize').fadeIn('fast');
			$('#content').show();
			if ($('#slideshow .activeslide').length == 0) $('#supersize a:first').addClass('activeslide');
			if (options.slide_captions == 1) $('#slidecaption').html($('#supersize .activeslide').find('img').attr('title'));
			if (options.navigation == 0) $('#navigation').hide();
			//Slideshow
			if (options.slideshow == 1){
				if (options.slide_counter == 1){ //Initiate slide counter if active
					$('#slidecounter .slidenumber').html(1);
	    			$('#slidecounter .totalslides').html($("#supersize > *").size());
	    		}
				slideshow_interval = setInterval("nextslide()", options.slide_interval);
				if (options.navigation == 1){ //Skip if no navigation
					$('#navigation a').click(function(){
   						$(this).blur();  
   						return false;  
   					});
				}
			}
		});

		$(document).ready(function() {
			$('#supersize').resizenow(); 
		});

		$(window).bind("resize", function(){
    		$('#supersize').resizenow(); 
		});
		
		$('#supersize').hide();
		$('#content').hide();
	};
	
	//Adjust image size
	$.fn.resizenow = function() {
		var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
	  	return this.each(function() {
	  		
			//Define image ratio
			var ratio = options.startheight/options.startwidth;
			
			//Gather browser and current image size
			var imagewidth = $(this).width();
			var imageheight = $(this).height();
			var browserwidth = $(window).width();
			var browserheight = $(window).height();
			var offset;

                        //var itemName = $(this).children().eq(globalSliderCnt).attr("id");
                        //var chunks = itemName.split("_");

                        if(imageSizes[globalSliderCnt][0] == 'f' ){
                             //Resize image to proper ratio
                             //Original Code
			     if ((browserheight/browserwidth) > ratio){
			         $(this).height(browserheight);
			         $(this).width(browserheight / ratio);
			         $(this).children().eq(globalSliderCnt).height(browserheight);
			         $(this).children().eq(globalSliderCnt).width(browserheight / ratio);
			     } else {
			         $(this).width(browserwidth);
			         $(this).height(browserwidth * ratio);
			         $(this).children().eq(globalSliderCnt).width(browserwidth);
			         $(this).children().eq(globalSliderCnt).height(browserwidth * ratio);
			     }



             /************* new codes *******************/
             /************* new codes *******************/
             /************* new codes *******************/

                             //Correctives
                                    //Correctives
                                $topCorrection = ( ((browserheight - $(this).height())/2) );
                                if($topCorrection < 0 ){
                                  $topCorrection = globalTopSet;
                                }
                                $leftCorrection = ( ((browserwidth - $(this).width())/2) );
                                //if($leftCorrection < 0 ){
                                //  $leftCorrection = globalLeftSet;
                                //}


             /************* new codes *******************/
             /************* new codes *******************/
             /************* new codes *******************/




			     //if (options.vertical_center == 1){
				$(this).children().eq(globalSliderCnt).css('left', $leftCorrection );
				$(this).children().eq(globalSliderCnt).css('top', $topCorrection );
			     //}
                        }else if(imageSizes[globalSliderCnt][0] == 's' ){
                             //special centered and percentage scaled image size
                             $(this).width($(window).width());
    			     $(this).height($(window).height());

		             $(this).children().eq(globalSliderCnt).width(imageSizes[globalSliderCnt][1]+'%');
   		             $(this).children().eq(globalSliderCnt).height(imageSizes[globalSliderCnt][2]+'%');

                             $(this).children().eq(globalSliderCnt).css('left', (browserwidth - $(this).children().width())/2);
			     $(this).children().eq(globalSliderCnt).css('top', imageSizes[globalSliderCnt][3] + 'px' );
                        }else{

                              /*
                             $frogShit_W = $(this).children().eq(globalSliderCnt).children().width();
                             $frogShit_H = $(this).children().eq(globalSliderCnt).children().height();
                             $frogShit = $(this).children().eq(globalSliderCnt).children().attr("id");
                             var chunkys = $frogShit.split("_");
                             imageSizes_A = new Array();
                             imageSizes_A.push(Array(chunkys[0],480,480,chunkys[3]));
                             */
                             // alert("kings :: " + $frogShit_W + " _ " + $frogShit_H + " _ " + $frogShit + " _ " + imageSizes_A);

                             //special centered and controlled sized images
                             var diffW = $(window).width() - imageSizes[globalSliderCnt][1];
                             var diffH = $(window).height() - imageSizes[globalSliderCnt][2];
                             if(diffH < 0 ){
                               diffH = 0;
                             }

                             $(this).width($(window).width() - diffW);
    			     $(this).height($(window).height() - diffH);


		             $(this).children().eq(globalSliderCnt).width($(window).width() - diffW);
   		             $(this).children().eq(globalSliderCnt).height($(window).height() - diffH);

                             $(this).children().eq(globalSliderCnt).css('left', (browserwidth - imageSizes[globalSliderCnt][1])/2);
			     //$(this).children().eq(globalSliderCnt).css('top', imageSizes[globalSliderCnt][3] + 'px' );
			     $(this).children().eq(globalSliderCnt).css('top', diffH/2 );
                        }

                        $(this).children().eq(globalSliderCnt).children().show();

			return false;
		});
	};

	$.fn.supersized.defaults = {
			startwidth: 4,  
			startheight: 3,
			vertical_center: 1,
			slideshow: 1,
			navigation:1,
			transition: 1, //0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
			pause_hover: 0,
			slide_counter: 1,
			slide_captions: 1,
			slide_interval: 6000
	};

})(jQuery);



	//Slideshow Next Slide
	function nextslide() {
	      if($.inAnimation) return false;
	      else $.inAnimation = true;

	      var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
	      var currentslide = $('#supersize .activeslide');
	      currentslide.removeClass('activeslide');

	      if ( currentslide.length == 0 ) currentslide = $('#supersize a:last');

  	      var nextslide =  currentslide.next().length ? currentslide.next() : $('#supersize a:first');
	      var prevslide =  nextslide.prev().length ? nextslide.prev() : $('#supersize a:last');

	      //Display slide counter
              if (options.slide_counter == 1){
	    	  var slidecount = $('#slidecounter .slidenumber').html();
		  currentslide.next().length ? slidecount++ : slidecount = 1;
		  $('#slidecounter .slidenumber').html(slidecount);
	      }


	      //global counter value - personal additive
	      currentslide.next().length ? ' ' : globalSliderCnt = -1;
              globalSliderCnt +=1;


	      $('.prevslide').removeClass('prevslide');
	      prevslide.addClass('prevslide');

	      //Captions require img in <a>
	      if (options.slide_captions == 1) $('#slidecaption').html($(nextslide).find('img').attr('title'));

	          nextslide.hide().addClass('activeslide')
	    	  if (options.transition == 0){
       	    	      prevslide.hide();
	    	      nextslide.show(); $.inAnimation = false;       	    	      
	    	  }

	    	  if (options.transition == 1){
	    	      nextslide.fadeIn( transparencyRate_FadeIn , function(){$.inAnimation = false;});
      	    	      prevslide.fadeOut( transparencyRate_FadeOut , function(){ });
	    	  }
	    	  if (options.transition == 2){
	    	      nextslide.show("slide", { direction: "up" }, 'slow', function(){$.inAnimation = false;});
	    	  }
	    	  if (options.transition == 3){
	    	      nextslide.show("slide", { direction: "right" }, 'slow', function(){$.inAnimation = false;});
	    	  }
	    	  if (options.transition == 4){
	    	      nextslide.show("slide", { direction: "down" }, 'slow', function(){$.inAnimation = false;});
	    	  }
	    	  if (options.transition == 5){
	    	      nextslide.show("slide", { direction: "left" }, 'slow', function(){$.inAnimation = false;});
	    	  }

	          $('#supersize').resizenow();//Fix for resize mid-transition

	}
	

