var activePage = 'body';

window.addEvent('domready', function() {
	activateAll();
	new ScaleView();
});

window.addEvent('load', function() {
	activateResize();
});

window.addEvent('resize', function() {
	activateResize();
});

document.addEvent('mousewheel', function(e) {
        e = new Event(e).stop();
        var step = mySlide.step - e.wheel * 10;
        mySlide.set(step);
});

function activateResize(){
//	$('menuNormalWrapper').setStyle('opacity', 0.8);
	$$('.page').each(function(ele){ele.setStyle('width', getWindowSize()[0]);});
	$$('.page h1').each(function(ele){ele.setStyle('opacity', 0.8);});
	$$('.contentIns').each(function(ele){ele.setStyle('height', getWindowSize()[1]-130);});
	$$('.areaScroll').each(function(ele){ele.setStyle('height', getWindowSize()[1]-100); ele.setStyle('opacity', 0.8);});
	activateScroll($('contentInsStff3'), $('contentIns3'), $('areaScroll3'), $('knobScroll3'));
	activateScroll($('contentInsStff4'), $('contentIns4'), $('areaScroll4'), $('knobScroll4'));
	activateScroll($('contentInsStff5'), $('contentIns5'), $('areaScroll5'), $('knobScroll5'));
	activateScroll($('contentInsStff6'), $('contentIns6'), $('areaScroll6'), $('knobScroll6'));
}

function activateAll() {
	$$('#menuIntro a').each(function(ele){
		ele.addEvent('mouseover', function(eve){ele.morph({'padding-left':20, 'opacity':0.5});});
		ele.addEvent('mouseleave', function(eve){ele.morph({'padding-left':0, 'opacity':1});});
	});
	//links
	$$('.menuIntro a, .menuIntroSmall a, .menuNormal a, .menuNormalSmall a').each(function(ele){
		ele.addEvent('click', function(eve){
			eve.stop();
			$('menuNormalWrapper').tween('top', 35);
			$$('.page').each(function(ele){ele.tween('left', 2500);});
			activePage = ele.get('rel');
			$(activePage).tween('left', 0);
		});
	});
	//gals
	$$('.galFirst').each(function(ele){
		ele.addEvent('mouseleave', function(eve){hideMoreInfo();});
	});
	$$('.galShow img').each(function(ele) {
		ele.addEvent('mouseover', function(eve){
			eve.stop();
			hideMoreInfo();
			ele.getParent().getParent().getParent().tween('width', '300');
			ele.getParent().getNext().tween('height', ele.getParent().getNext().getChildren()[0].getStyle('height'));
		});
	});
	$$('a.imageBg').each(function(ele){
		ele.addEvent('click', function(eve){
			eve.stop();
			changeBgImage(ele.get('href'));
		});
	});
}

function hideMoreInfo() {
	$$('.galFirst').each(function(ele){ele.tween('width', '50')});
	$$('.galFirstEleInfo').each(function(ele){ele.tween('height', '0');});
}

function changeBgImage(urlImage) {
	$('scalerBg').getElement('div').getElement('img').set('src', '');
	$('scalerBg').getElement('div').getElement('img').set('src', urlImage);
}

function getWindowSize() {
	var winW = 630, winH = 460;
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winW = window.innerWidth;
			winH = window.innerHeight;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
		}
	}
	var sizeWindow=new Array();
	sizeWindow[0] = winW;
	sizeWindow[1] = winH;
	return sizeWindow;
}

function activateScroll(contentDiv, containerDiv, areaScrollDiv, knobDiv) {
//	if ((contentDiv.getStyle('height').replace('px','')) > (containerDiv.getStyle('height').replace('px',''))) {
		myScrollFx = new Fx.Scroll(containerDiv.get('id'), {
			wait: false
		});
		mySlide = new Slider(areaScrollDiv, knobDiv, {
			steps: contentDiv.getStyle('height').replace('px','')-(containerDiv.getStyle('height').replace('px','')),
			mode: 'vertical',
			onChange: function(step){
				containerDiv.scrollTo(0,step);
			}
		}).set(0);
//	} else {
//		areaScrollDiv.setStyle('visibility','hidden');
//	}
}

function normalBox(className) {
	equalHeights(className);
	$$(className).each(function(ele){
		var normalBoxTop = new Element('div', {'class': 'normalBoxTop'});
		var normalBoxBottom = new Element('div', {'class': 'normalBoxBottom'});
		normalBoxTop.inject(ele, 'top');
	});
}

function activateRotate(eleClass) {
	var rotater = new Rotater(eleClass,{
		slideInterval:8000,
		transitionDuration:1000
	});
}

function activateMenu() {
	$$('.wrapperAll .menuBtn a').each(function(ele){
		ele.addEvent('click', function(eve) {
			eve.stop();
			var linkEle = ele.get('href');
			if (strpos(linkEle, 'inicio')!=false) {movePage(0);};
			if (strpos(linkEle, 'alojamiento')!=false) {movePage(1400);};
			if (strpos(linkEle, 'diseno')!=false) {movePage(2800);};
			if (strpos(linkEle, 'posicionamiento')!=false) {movePage(4200);};
			if (strpos(linkEle, 'blog')!=false) {movePage(5600);};
			if (strpos(linkEle, 'contacto')!=false) {movePage(7000);};
		});
	});
}

function strpos (haystack, needle, offset) {
    var i = (haystack+'').indexOf(needle, (offset || 0));
    return i === -1 ? false : i;
}

function closeSubMenus() {
	$$('.subMenu').each(function(ele){
		ele.setStyle('display', 'none');
	});
}


function activateMenuSlow() {
	var activated = $('extraInfo').get('html');
	$$('.menu').each(function(ele){
		var activatedExists = false;
		var idSubmenu = ele.get('id').replace('menu','');
		if (isset($('subMenu'+idSubmenu))) {
			var heightSubMenu = $('subMenu'+idSubmenu).getStyle('height').replace('px','');
			var openSubMenu = false;
			//revisar si esta activo
			$$('#subMenu'+idSubmenu+' a').each(function(ele) {
				if (ele.get('html')==activated) {
					activatedExists = true;
				}
			})
			//bajar altura
			if (activatedExists==false) {
				$('subMenu'+idSubmenu).setStyle('height', 0);
			}
			//adjuntar el click
			ele.addEvent('click', function(event){
				event.stop();										   
				if (openSubMenu==false) {
					new Fx.Tween('subMenu'+idSubmenu).start('height', heightSubMenu);
					openSubMenu=true;
				} else {
					new Fx.Tween('subMenu'+idSubmenu).start('height', 0);
					openSubMenu=false;
				}
			});
		}
	});

}

function reloadDiv(urlReload, divReload) {
	var reloadRequest = new Request({
		method: 'get',
		url: urlReload,
		onSuccess: function(textResult, xmlResult){
						$(divReload).set('html',textResult);
						if (isset($(divReload).getElement('form'))) {
							cathForm($(divReload).getElement('form'));
						}
						activateAll();
					}
	}).send(); 	
}

function reloadDivSimple(urlReload, divReload) {
	var reloadRequest = new Request({
		method: 'get',
		url: urlReload,
		onSuccess: function(textResult, xmlResult){
						$(divReload).set('html',textResult);
						activateAll();
						activateTinyMCE();
					}
	}).send(); 	
}

function injectDivSimple(urlReload, divInject) {
	var reloadRequest = new Request({
		method: 'get',
		url: urlReload,
		onSuccess: function(textResult, xmlResult){
						var newDiv = new Element('div', {'html':textResult});
						newDiv.inject($(divInject));
						activateAll();
						activateTinyMCE();
					}
	}).send(); 	
}

function reloadDivToogle(urlReload, divReload) {
	if ($(divReload).get('html')!='') {
		eraseContentDiv (divReload);
	} else {
		reloadDivSimple(urlReload, divReload);
	}
}

function eraseContentDiv (divErase) {
	$(divErase).set('html', '');
}

function showHide(eleId) {
	if ($(eleId).getStyle('display') == 'block' || $(eleId).getStyle('display') == '')
		$(eleId).setStyle('display', 'none');
	else
		$(eleId).setStyle('display', 'block');
	return;
}

function showHideElement(ele) {
	if (ele.getStyle('display') == 'block' || ele.getStyle('display') == '')
		ele.setStyle('display', 'none');
	else
		ele.setStyle('display', 'block');
	return;
}

function hideAll(eleClass) {
	$$(eleClass).each(function(el){
		el.setStyle('display', 'none');
	});
}

function showHideAll(eleClass, ele) {
	hideAll(eleClass);
	$(ele).setStyle('display', 'block');
}

function equalHeights(className) {
	var maxHeight = 0;
	$$(className).each(function (ele) {
		var actHeight = parseInt(ele.getStyle('height').replace('px',''));
		if (actHeight > maxHeight) {
			maxHeight = actHeight;
		}
	});
	$$(className).each(function (ele) {
		ele.setStyle('height',maxHeight+'px');
	 });
}

function equalWidths(className) {
	var numClasses = 0;
	var parentWidth = 0;
	$$(className).each(function(ele){
		var eleParent = ele.getParent();
		parentWidth = eleParent.getStyle('width').replace('px','');
		numClasses++;
	});
	$$(className).each(function(ele){
		ele.setStyle('width',parentWidth/numClasses-25);
	});
}

function makeEventsDraggable() {
	$$('.close').each(function(ele){
		var dragContainerOptions = {
			handle: ele
		};
		ele.parentNode.makeDraggable(dragContainerOptions);
	});
}



function deleteParentDiv() {
	$$('.deleteParent').each(function(divDel){
		divDel.addEvent('click', function(eve) {
			eve.stop();
			divDel.getParent().getParent().dispose();
		})
	});
}

function isset(variable_name) {
	try {
		if (typeof(eval(variable_name)) != 'undefined')
			if (eval(variable_name) != null)
				return true;
	} catch(e) { }
	return false;
}

function goUp() {
	window.scrollTo(0,0);	
}

function openPrinterWindow(urlPrint) {
	window.open(urlPrint,'Print','width=750,height=600,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function popUpWindow(urlWindow, height, width) {
	window.open(urlWindow,'Print','width='+width+',height='+height+',toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}


