/* JS
* Copyright 2009 noponies.
* Version 0.1 Beta - Intial Release to the wild - comments, yes please!
*/
(function($){

	var img_prop;
	var imageArray = [];
	var defaults = {};
	var firstLoad = true;
	
	$.fn.extend({ 
		npFullBgImg: function(imgPath, options) {
			defaults = {
				 fadeInSpeed: 1000,
   				 center: false
			};
			
			var opts = $.extend(defaults, options);
			var targetContainer = $(this); 
			//create image
			var img  = new Image();
			//add to array
 	 		imageArray.unshift(img);
 	 		
 	 		if(firstLoad === true) {
 	 			$(targetContainer).fadeTo(10, 0)
 	 		}
			
	        $(img).load(function () {
	        	//this is a hack to stop a flash of the image sometimes
  				$(img).fadeOut(10, 0);
	         	$(img).css({display: 'none', left:0, top: 0, position: 'fixed', 'z-index': -100});
	            //add image to container
	            $(targetContainer).append(img);
	            //resize image
				resizeImg($(window).width(), $(window).height(), $(img).width(), $(img).height());
				
				if(firstLoad === true) {
 	 				$(targetContainer).fadeTo(10, 1);
 	 				firstLoad = false;
 	 			}
					            
	            $(img).fadeIn(defaults.fadeInSpeed, function () {
		            if(imageArray.length > 1) {
		            	imageArray.pop();
		            	$(targetContainer).children().eq(0).remove();   	
		            }
		            
		          	if( typeof opts.callback == 'function' ){
						opts.callback.call(this, targetContainer, options);
					}

	            });
				
				
				/*
				$(img).css({'left': 1.2*$(window).width()+'px'});
				$(img).show().animate( {left: 0} , 1000 , 'easeInOutElastic', function () {
		            if(imageArray.length > 1) {
		            	imageArray.pop();
		            	$(targetContainer).children().eq(0).remove();   	
		            }
		            
		          	if( typeof opts.callback == 'function' ){
						opts.callback.call(this, targetContainer, options);
					}
	            });
				*/
	        }).error(function () {
	            // got an error
	            alert('image not loaded');
	        }).attr('src', imgPath + '?random=' + (new Date()).getTime());
	    },
		salir: function(url){
			var img = $(this).find('IMG').first();
			$(img).animate( {left: -1.2*$(window).width()} , 1000 , 'easeInOutElastic', function () {
				window.location = url;
				//$(img).show().animate( {left: 0} , 1000 , 'easeInOutElastic');
			});
		}
	});
	
	$(window).bind("resize", function(){
			resizeImg($(window).width(), $(window).height(), $(imageArray[0]).width(), $(imageArray[0]).height());
			$('#wrapper').height( $(window).height() );
			resizeContent();
			
	});

  	function resizeImg(sw, sh, imgw, imgh, targetContainer){
			if ((sh / sw) > (imgh / imgw)) {
					img_prop = imgw/imgh;
					destHeight = sh;
					destWidth = sh * img_prop;
				} else {
					img_prop = imgh/imgw;
					destWidth = sw;
					destHeight = sw * img_prop;
				}

			$(imageArray[0]).attr({
				width: destWidth,
				height: destHeight
			});
			
			if(defaults.center) {
				var xVal = sw * .5 - $(imageArray[0]).width() * .5;
				var yVal = sh * .5 - $(imageArray[0]).height() * .5;
				$(imageArray[0]).css({left:xVal, top: yVal});
			}
	}
	
	
})(jQuery);

var CONTAINER_WIDTH_DEF;
var CONTENT_WIDTH_DEF;
$(function(){
	CONTAINER_WIDTH_DEF = $('#container').width();
	CONTENT_WIDTH_DEF = $('#content').width();
});

var resize_primera_vez = true;

function resizeContent(){

	//anchura
	var WINDOW_WIDTH = $(window).width();
	var WINDOW_HEIGHT = $(window).height();
	/*
	var WINDOW_WIDTH = $(window).width();
	if(WINDOW_WIDTH > 1100 && WINDOW_WIDTH < 1500){
		var sumar = WINDOW_WIDTH-1100;
	}else if(WINDOW_WIDTH >= 1500){
		var sumar = 400;
	}else{
		var sumar = 0;
	}
	$('#container').width(CONTAINER_WIDTH_DEF + sumar);
	$('#content').width(CONTENT_WIDTH_DEF + sumar);
	*/
	if(WINDOW_WIDTH < 1220){
		$('#menu_wrapper').css({ 'position': 'static', 'margin-bottom': '100px' });
		//$('#content').css({ 'padding-bottom': '100px' });
		
		$('#colophon').appendTo('#slidebar');
		$('#colophon').css({  'left': $('#menu').position().left + 14 +'px' });
		$('.ie #colophon').css({  'left': $('#menu').position().left + 13 +'px', 'width': '157px' });
	}else{
		$('#menu_wrapper').css({ 'position': 'fixed', 'margin-bottom': '0px' });
		$('#content').css({ 'padding-bottom': '0px' });
		$('#colophon').appendTo('#footer');
		if($('#footer').position().left != null){
			$('#colophon').css({ 'left': $('#footer').position().left +'px' });
		}
	}
	if(WINDOW_HEIGHT < 720){
		$('#menu_wrapper').css({ 'position': 'static', 'margin-bottom': '100px' });
	}
	$('#content_bg').css({ 'left': $('#content').position().left +'px' });
	
	//altura
	var WINDOW_HEIGHT = $(window).height();
	var DOCUMENT_HEIGHT = $(document).height();
	if(WINDOW_HEIGHT < DOCUMENT_HEIGHT){
		if( DOCUMENT_HEIGHT < $('#slidebar').height() ){
			$('#content').height( $('#slidebar').height()+20 );
		}else{
			//$('#content').height( DOCUMENT_HEIGHT );
		}
		$('#footer').height( DOCUMENT_HEIGHT );
	}else{
		if( DOCUMENT_HEIGHT < $('#slidebar').height() ){
			$('#content').height( $('#slidebar').height()+20 );
		}else{
			//$('#content').height( WINDOW_HEIGHT );
		}
		$('#footer').height( WINDOW_HEIGHT );
	}

}
