/*
 * JQuery image panning Plugin
 * 
 * Version 1.0
 * 
 * by Christian Bruun - 29. nov 2010
 * 
 * Like it/use it? Send me an e-mail: rockechris@rockechris.com
 * 
 * License: None. Use and abuse. Comes with no warranty, of course!
 * 
 * Usage:
 * $('div').panorer({ options });
 *  
 * Options: 
 * direction:	scroll direction - up/down or left/right, circular RDLU ("EW", "WE", "SN", "NS", "round", "random") - default: "random"
 * initialWait:	time to wait before first animation (number) - default: 1000 ms
 * duration:	duration of animation (number) - default: (image width / 0.2) ms
 * pause:		pause between pans (number) - default: 1000 ms
 * easing:		easing to use on animations - default: "swing"
 * image:		image to load in the div - default: null (use image from html)
 * 
 */
  widths=Array();
 widths[1]=1279;
 widths[2]=1132;
 widths[3]=1396;
 widths[4]=1243;
 widths[5]=1722;
 widths[6]=1131;
 widths[7]=1728;
 widths[8]=1365;
 widths[9]=1476;
 widths[10]=1580;
 widths[11]=1159;
 widths[12]=1227;
 widths[13]=1498;
 widths[14]=1273;
 widths[15]=1286;

function getSizes() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  //window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );
  return myHeight;
  
}
function loadSite(){	
	//$("#main_container").css('display','block');
	$("#divLoader").css('display','none');
	$("#main_container").slideDown(3000)
	setTimeout("scrollMe()",6000);
}
(function($) {
	$.fn.panorer = function(options) {
        var defaults = {
			duration:		0,
			initialWait:	1000,
			pause:			3000,
			direction:		'random',
			infinity:		false,
			easing:			'swing',
			image:			null
        };
        var opts = $.extend(defaults, options);		
		
        return this.each(function() {
			var tiss = this;
			if($(tiss).attr('tagName').toUpperCase() !== "DIV") { $(this).wrap('<div style=\'z-index:-1\'></div>'); tiss = $(this).parent(); }
		
			var bilde = $(tiss).children('img:first');
			
			
			// litt css
			$(tiss).css({ 'position': 'relative', 'overflow': 'hidden','z-index':-1});
			//$(tiss).css({ 'position': 'absolute', 'overflow': 'hidden','z-index':-5,'text-align':'center','left':0,'top':100,'width':'100%'});
			
			$(bilde).css({ 'z-index':0});
			$(tiss).children().css('z-index',0);
			// tid
			if(opts.duration == 0) {
				if($(this).children('img:first').width() > 0) { opts.duration = Math.round($(this).children('img:first').width() / 0.2); }
				else { opts.duration = Math.round($(this).width() * 4); }
			}
			
			// retning v. random
			if(opts.direction == "random") {
				var d,v = Math.random();
				if(v < 0.49) { d = "A"; }
				else { d = "B"; }

				if($(bilde).width() >= $(bilde).height()) {	// <- ->
					 opts.direction = "EW";
					 if(d=="A") { opts.direction = "WE"; }
				}
				else {
					opts.direction = "SN";
					if(s=="A") { opts.direction = "NS"; }
				}
			}

			// prøv å last bilde
			if(opts.image !== null) {
				$(tiss).append('<img src="' + opts.image + '" alt="" height="' + $(tiss).innerHeight() + '" />');
			}
			
			// selve panoreringen			
			var panLeft = function() {
				if(j>0){
					i++;
					if(i>15)i=1;
					document.getElementById('bannerImg').src='new_design/home_images/'+i+'.jpg';
					$("#bannerImg").css({ 'z-index':0});
					$(bilde).width(widths[i]);
					//alert(i);
				}
				//$("#divD").html($(tiss).width()+' = '+$(bilde).width());
				j++;
				if(opts.direction === "round") { $(bilde).animate({ left: ($(tiss).width() - $(bilde).width()) }, opts.duration, opts.easing, function() {setTimeout(panUp, opts.pause);}); return; }
				$(bilde).animate({ left: ($(tiss).width() - $(bilde).width()) }, opts.duration, opts.easing, function() {setTimeout(panRight, opts.pause);});
			};			
			var panRight = function() {				
				if(opts.direction === "round") { $(bilde).animate({ left: 0 }, opts.duration, opts.easing, function() {setTimeout(panDown, opts.pause);}); return; }
				$(bilde).animate({ left: 0 }, opts.duration, opts.easing, function() {setTimeout(panLeft, opts.pause);});
			};
			var panUp = function() {
				if(opts.direction === "round") { $(bilde).animate({ top: ($(tiss).height() - $(bilde).height()) }, opts.duration, opts.easing, function() {setTimeout(panRight, opts.pause);}); return; }
				$(bilde).animate({ top: ($(tiss).height() - $(bilde).height()) }, opts.duration, opts.easing, function() {setTimeout(panDown, opts.pause);});
			};
			var panDown = function() {
				if(opts.direction === "round") { $(bilde).animate({ top: 0 }, opts.duration, opts.easing, function() {setTimeout(panLeft, opts.pause);}); return; }
				$(bilde).animate({ top: 0 }, opts.duration, opts.easing, function() {setTimeout(panUp, opts.pause);});
			};
			
			// start
			switch(opts.direction) {
				case "WE":
					$(bilde).css({ 'position':'absolute', 'left':($(tiss).width() - $(bilde).width()) });
					setTimeout(panRight, opts.initialWait); // left first
					break;
				case "SN":
					$(bilde).css( { 'position':'absolute', 'left':'0', 'top':'0' });
					setTimeout(panUp, opts.initialWait); // up first
					break;
				case "NS":
					$(bilde).css( { 'position':'absolute', 'left':'0', 'top':($(tiss).height() - $(bilde).height()) });
					setTimeout(panDown, opts.initialWait); // down first
					break;
				default: // (case "EW":)
					$(bilde).css({ 'position':'absolute', 'left':'0' });
					setTimeout(panLeft, opts.initialWait); // right first
					break;
			}
		});
    };
var cache = [];
$.preLoadImages = function() {
	var args_len = arguments.length;
	for (var i = args_len; i--;) {
		var cacheImage = document.createElement('img');
		cacheImage.src = arguments[i];
		cache.push(cacheImage);
	}
}
})(jQuery);
