/**
 * Copyright ActiveWebShop for Anderson Willinger
 * ----------------------------------------------
*/

/**
 * homepage background images
 * ----------------------------------------------
*/

uri = "http://"+location.host+location.pathname+location.hash;
var data = {}
var bgkclass = {
	mvc : function(position_section){
		uri = "http://"+location.host+location.pathname+location.hash;
		section = uri.split("#");
		section = section[1].split("/");
		if(position_section=="last"){
			section = section.reverse();
			section = section[1];
		}
		else{
 			section = section[position_section];
		}
		return section;
	},
	mvc_in_uri : function(position_section, uri){
		if(uri.match(/#/)){
			section = uri.split("#");
			section = section[1].split("/");
			if(position_section=="last"){
				section = section.reverse();
				section = section[1];
			}
			else{
				section = section[position_section];
			}
		}
		else{
			section = "";
		}
		return section;
	},
	pos_mvc : function(var_section){
		uri = "http://"+location.host+location.pathname+location.hash;
		section = uri.split("#");
		section = section[1].split("/");
		for (i = 0; i < section.length; i++){
			if(section[i]==var_section) position_section = i;
		}
		return position_section;
	},
	init : function(){
		width = navigator.userAgent.match(/WebKit/) ? window.innerWidth : $(document).width() ;
		height = navigator.userAgent.match(/WebKit/) ? window.innerHeight : $(document).height() ;
	
		elements = $("#bgkimg").children();
		element_random =  Math.floor ( Math.random () * ( elements.length ) );
	
		width_box = 0;
		for (var i = 0; i < elements.length; i++) {
			width_box += elements[i].width;
		};
		$("#bgkimg").css('width',width_box+'px');

		bgkclass.vars(width,height,element_random);
	},
	vars : function(width, height, element_random){
		for(i = 0; i < elements.length; i++){
			height_element = elements[i].height;
			top_element = parseInt(height - height_element)/2;
			// $(elements[i]).animate({ marginTop : top_element+'px' }, 500); 
			$(elements[i]).css("margin-top", top_element ) ;
		}
		width_element = elements[element_random].width;
		top_element = parseInt(height - height_element)/2;
		left_element = parseInt((width - width_element)/2) < 0 ? 0 : parseInt((width - width_element)/2) ;
		top_subMenu = parseInt((height/4)-20) < 125 ? 125 : parseInt((height/4)-20) ;
		top_cont = parseInt((height/3)-100) < 125 ? 130 : parseInt((height/3)-120);

		left_subCenter = $("#container")[0].offsetLeft;
		left_centerToSubCenter = parseInt(left_subCenter - left_element);

		position_left = 0;
		for (var i = 0; i < element_random; i++) {
			position_left += elements[i].width;
		}
		left_box = width_element > width ? (position_left + parseInt((width_element - width)/2)) * -1 : (position_left - left_element) * -1 ;
	},
	loads : function(){
		bgkclass.imagesmove();
	},
	resize_window : function(){
		width = navigator.userAgent.match(/WebKit/) ? window.innerWidth : $(document).width() ;
		height = navigator.userAgent.match(/WebKit/) ? window.innerHeight : $(document).height() ;
		bgkclass.vars(width,height,element_random);

		// $("#bgkimg").animate({ marginLeft : left_box+'px' }, 100);
		$("#bgkimg").css("margin-left", left_box) ;
	},
	imagesmove : function(){
		/* $("#bgkimg img").animate({ opacity: '100'}, 500);//.4
		$(elements[element_random]).animate({ opacity: '100'}, 100);
		$("#bgkimg").animate({ marginLeft: left_box+'px'}, 1000); */
		$("#bgkimg").css("margin-left", left_box) ;
	}
}

$(document).ready( function() {
	bgkclass.init();
	bgkclass.loads(); 
	$(window).resize(function(e){ bgkclass.resize_window(); return false; });
});
jQuery.extend({ //extension de metodo $
	id : function(id) {
		var id = id.split("#");
		return document.getElementById(id[1])?true:false;
	}
});




