// JavaScript Document

function initPage( elements ){
		setFooterBG();
		if ( elements ) setElementsBG( elements );
		setBackgroundDimensions();
}

function initProductWindows(){
	$("#layerPrototipos a").click(function(){
		if(	$('#layerPrototipos').hasClass('openProductWindow') ){
			//$('#layerPrototipos').height(130);
			
			$('#layerPrototipos').animate({ height: '130px' });
			$('#layerPrototipos img').css('display','none');

		}else{
			//$('#layerPrototipos').height(324);
			$('#layerPrototipos').animate({ height: '324px' }, {
			  										step: function(now, fx) {
														$('#layerPrototipos img').css('display','block');
			  										}
			});
			
		}
		$("#layerPrototipos").toggleClass('openProductWindow');
		$("#layerPrototipos p").toggleClass('openWindow');
	});
	$("#layerMoldes a").click(function(){
		if(	$('#layerMoldes').hasClass('openProductWindow') ){
			$('#layerMoldes').animate({ height: '130px' });
			$('#layerMoldes img').css('display','none');

		}else{
			$('#layerMoldes').animate({ height: '324px' }, {
			  										step: function(now, fx) {
														$('#layerMoldes img').css('display','block');
			  										}
			});
		}
		$("#layerMoldes").toggleClass('openProductWindow');
		$("#layerMoldes p").toggleClass('openWindow');
	});
	$("#layerEscaparates a").click(function(){
		if(	$('#layerEscaparates').hasClass('openProductWindow') ){
			$('#layerEscaparates').animate({ height: '130px' });
			$('#layerEscaparates img').css('display','none');

		}else{
			$('#layerEscaparates').animate({ height: '324px' }, {
			  										step: function(now, fx) {
														$('#layerEscaparates img').css('display','block');
			  										}
			});
		}
		$("#layerEscaparates").toggleClass('openProductWindow');
		$("#layerEscaparates p").toggleClass('openWindow');
	});
	$("#layerEsculturas a").click(function(){
		if(	$('#layerEsculturas').hasClass('openProductWindow') ){
			$('#layerEsculturas').animate({ height: '130px' });
			$('#layerEsculturas img').css('display','none');

		}else{
			$('#layerEsculturas').animate({ height: '324px' }, {
			  										step: function(now, fx) {
														$('#layerEsculturas img').css('display','block');
			  										}
			});
		}
		$("#layerEsculturas").toggleClass('openProductWindow');
		$("#layerEsculturas p").toggleClass('openWindow');
	});	
}

function isIPad(){

    if(navigator.userAgent.match(/iPad/i) != null){
        return true;
    }else{ return false;}
}


function setElementsBG( elements ){
	if (!isIPad()){
		for( var i=0; i<elements.length; i++){
			var elementOffset = $( elements[ i ] ).offset();
			$( elements[ i ] ).attr('style', 'Background-position: 50% -' + elementOffset.top + 'px;');
		}	
	}
}

function setFooterBG(){
	var verticalOffset = $(window).height() - 36;
		$(".footerBackground").attr('style', 'Background-position: 50% -' + verticalOffset + 'px;');
}

function setUnchainer(){
		 $(".greyTable a").click(function(){
		 		$("#coverLayer").show();
		 		$(".modalWindow").show();	 
		 });
}

function setCloser(){
		 $(".btn_applyFilter").click(function(){
		 		setInitialState();
		 });
		 
}

function getBackgroundHeight(){
	var cssHeight = '';
	var viewportHeight = $(window).height();
		if( viewportHeight <= 1024 ){
			cssHeight = '_1024';
		}else if( viewportHeight <= 1100 ){
			cssHeight = '_1100';
		}else if( viewportHeight <= 1200 ){
			cssHeight = '_1200';
		}
	return cssHeight;
}

function getBackgroundClasses(){
	var bgClass = '';
	var cssHeight = getBackgroundHeight();
		//console.log('cssHeight: '+cssHeight);
	var bodyCSS = $('body').attr('class');	
		bodyCSS = bodyCSS.split(' ');
		//console.log('bodyCSS: '+bodyCSS);
		for( var i=0; i<bodyCSS.length; i++ ){
			if( !bodyCSS[ i ].indexOf('bg_') ){
				bgClass = bgClass + ( bodyCSS[ i ] + cssHeight) + ' ';	
			}else{
				bgClass = bgClass + bodyCSS[ i ] + ' ';
			}
		}	
	return bgClass;
}

function setBackgroundDimensions(){
	var bodyClasses = getBackgroundClasses();
	$('body').attr('class', bodyClasses);
	
	/*$(window).resize(function() {
  		$('#log').append('<div>Handler for .resize() called.</div>');
	});*/

}
