// JavaScript Document

var $j = jQuery.noConflict();

/*************************************************
* Nueva funcion para el menu. 14:01 20/01/2010
*************************************************/
function despliega(el,level){
	$j(el).find("ul.level"+level).slideDown('fast').show(); //Drop down the subnav on click
	$j(el).hover(function() {
		}, function(){	
			$j(el).find("ul.level"+level).slideUp('fast'); //When the mouse hovers out of the subnav, move it back up
	});
}

/*
$j(document).ready(function(){
*/

/*
	 $("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled - Adds empty span tag after ul.subnav
	
	$("ul.topnav li span").click(function() { //When trigger is clicked...
		
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() { 
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	}); 
*/
	
	// Aņade la posibilidad de desplegar el submenu con MOUSEOVER (pasar por encima)
	
/*	$j("ul.topnav li a").mouseover(function() { //When trigger is clicked...
		
		//Following events are applied to the subnav itself (moving subnav up and down)
		$j(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

		$j(this).parent().hover(function() {
		}, function(){	
			$j(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() { 
			$j(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$j(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});

});*/
var posTempForAll = 65;

function prepareSlideDownUpCart(){
	/* BEGIN NEW SECTION FOR THE MENU BLOCK DINAMIC */
	//if (!navigator.appVersion.match('MSIE 6.')) {
		var header, header_offset, header_copy, header_copy1;
		Event.observe(window, 'load', function() {
			var headers = $$('.col-4');
			for(var i=0; i<headers.length;i++) {
				if(!headers[i].hasClassName('skip-header')) {
					header = headers[i];
				}
			}
			if (!header) {
				return;
			}
			header_offset = Position.cumulativeOffset(header)[1];
			var buttons = $$('.content-header-floating-nodisplay')[0];
			if (buttons) {
				try{
					new Insertion.Before(buttons, '<div class="content-buttons-placeholder"></div>');
					buttons.placeholder = buttons.previous('.content-buttons-placeholder');
					buttons.remove();
					buttons.placeholder.appendChild(buttons);

					header_offset = Position.cumulativeOffset(buttons)[1];
				}catch(E){
					
				}

			}
			header_copy = document.createElement('div');
			//header_copy.appendChild(header.cloneNode(true));
			
			header_copy1 = document.createElement('div');
			$(header_copy1).addClassName('floating-container');

			header_copy1.appendChild(header.cloneNode(true));

			header_copy.appendChild(header_copy1);

			//document.body.appendChild(header_copy);
			document.body.appendChild(header_copy);

			$(header_copy).addClassName('content-header-floating');
			//		if ($(header_copy).down('.content-buttons-placeholder')) {
			if ($(header_copy).down('.content-header-floating')) {
				$(header_copy).down('.content-buttons-placeholder').remove();
			}
		});

		function floatingTopButtonToolbarToggle() {
			try{
			if (!header || !header_copy || !header_copy.parentNode) {
				return;
			}
			var s;
			var posX;
			var posY;
			// scrolling offset calculation via www.quirksmode.org
				posY = document.documentElement.scrollTop;
				if (self.pageYOffset){
					s = self.pageYOffset;
					posY = s;
					posTempForAll = posY;
				}else if (document.documentElement && document.documentElement.scrollTop) {
					s = document.documentElement.scrollTop;
					posX = s;
				}else if (document.body) {
					s = document.body.scrollTop;
					posX = s;
				}
				
			var buttons = $$('.content-buttons')[0];
			//var buttons = $$('.content-header-floating-nodisplay');
			var StrDiv = '<div class="floating-container">';
			StrDiv += '<div class="col-4 f-left">';
			StrDiv += '<div class="container">';
			StrDiv += '<div class="shopping-cart-mini">'+$('access-cart').innerHTML;
			StrDiv +='</div></div></div></div>';
			header_copy.innerHTML = StrDiv;
			if (s > header_offset) {
				if (buttons) {
					if (!buttons.oldParent) {
						buttons.oldParent = buttons.parentNode;
						buttons.oldBefore = buttons.previous();
					}
					if (buttons.oldParent==buttons.parentNode) {
						var dimensions = buttons.placeholder.getDimensions() // Make static dimens.
						buttons.placeholder.style.width = dimensions.width + 'px';
						buttons.placeholder.style.height = dimensions.height + 'px';

						buttons.hide();
						buttons.remove();
						//$(header_copy).down('div').appendChild(buttons);
						$(header_copy).down('div').appendChild(buttons);
						buttons.show();
					}
				}

				//header.style.visibility = 'hidden';
				header_copy.style.display = 'block';
			} else {
				if (buttons && buttons.oldParent && buttons.oldParent != buttons.parentNode) {
					buttons.remove();
					buttons.oldParent.insertBefore(buttons, buttons.oldBefore);
					//buttons.placeholder.style.width = undefined;
					//buttons.placeholder.style.height = undefined;
				}
				header.style.visibility = 'visible';
				header_copy.style.display = 'none';
			}
			//setPositionCartDescription();
			var eleP = $('div-shopping-cart-mini');
			Position.prepare();
			var offsetC = Position.page(eleP);
			var arroffSetC = String(offsetC).split(',');
			if (posY==undefined || posY==NaN){
				posY = 10;
			}else{
				posY = posY + 9;
			}
			arroffSetC[0] = 0;
			//TODO: AQUI SOLUCION PARA IE
			var posY = posY + 1;
			if ($('div-shopping-description').visible()){
				new Effect.Move($('tooltip-shoppingcart'), { x: arroffSetC[0], y: posY, mode: 'absolute',duration: 0.1 });
				obj = document.getElementsByClassName('div.floating-container');
				var p = Position.page(obj);
			}else{
				new Effect.Move($('tooltip-shoppingcart'), { x: arroffSetC[0], y: posY, mode: 'absolute',duration: 0.1 });
			}
			
			}catch(E){
				alert(E);
			}
		}
		
		Event.observe(window, 'scroll', floatingTopButtonToolbarToggle);
		Event.observe(window, 'resize', floatingTopButtonToolbarToggle);
	//}
}

function setPositionCartDescription(){
	var s;
	var posY;
	// scrolling offset calculation via www.quirksmode.org
	if (self.pageYOffset){
		s = self.pageYOffset;
		posY = s;
	}else if (document.documentElement && document.documentElement.scrollTop) {
		s = document.documentElement.scrollTop;
		posX = s;
	}else if (document.body) {
		s = document.body.scrollTop;
		posX = s;
	}
//	alert(posY+'--'+posTempForAll);
	if (posY==undefined || posY==NaN){
		posY = posTempForAll;
	}else{
		posY = posTempForAll + 60;
	}
	
	
	var eleP = $('div-shopping-cart-mini');
	Position.prepare();
	var offsetC = Position.page(eleP);
	var arroffSetC = String(offsetC).split(',');
	//if ($('tooltip-shoppingcart').visible()){
		new Effect.Move($('tooltip-shoppingcart'), { x: arroffSetC[0], y: posY, mode: 'absolute',duration: 0 });
	//}else{
	//	new Effect.Move($('tooltip-shoppingcart'), { x: arroffSetC[0], y: posY, mode: 'absolute',duration: 0.1 });
	//}
}

function prepareSlideDownUpCart2(){
	/* BEGIN NEW SECTION FOR THE MENU BLOCK DINAMIC */
	if (!navigator.appVersion.match('MSIE 6.')) {
		var header, header_offset, header_copy, header_copy1;

			var headers = $$('.col-4');
			for(var i=0; i<headers.length;i++) {
				if(!headers[i].hasClassName('skip-header')) {
					header = headers[i];
				}
			}
			if (!header) {
				return;
			}
			header_offset = Position.cumulativeOffset(header)[1];
			var buttons = $$('.content-header-floating-nodisplay')[0];
			if (buttons) {
				try{
					new Insertion.Before(buttons, '<div class="content-buttons-placeholder"></div>');
					buttons.placeholder = buttons.previous('.content-buttons-placeholder');
					buttons.remove();
					buttons.placeholder.appendChild(buttons);

					header_offset = Position.cumulativeOffset(buttons)[1];
				}catch(E){
					
				}

			}
			header_copy = document.createElement('div');
			//header_copy.appendChild(header.cloneNode(true));
			
			header_copy1 = document.createElement('div');
			$(header_copy1).addClassName('floating-container');

			header_copy1.appendChild(header.cloneNode(true));

			header_copy.appendChild(header_copy1);

			//document.body.appendChild(header_copy);
			document.body.appendChild(header_copy);

			$(header_copy).addClassName('content-header-floating');
			//		if ($(header_copy).down('.content-buttons-placeholder')) {
			if ($(header_copy).down('.content-header-floating')) {
				$(header_copy).down('.content-buttons-placeholder').remove();
			}

		function floatingTopButtonToolbarToggle() {

			if (!header || !header_copy || !header_copy.parentNode) {
				return;
			}
			var s;
			// scrolling offset calculation via www.quirksmode.org
			if (self.pageYOffset){
				s = self.pageYOffset;
			}else if (document.documentElement && document.documentElement.scrollTop) {
				s = document.documentElement.scrollTop;
			}else if (document.body) {
				s = document.body.scrollTop;
			}

			var buttons = $$('.content-buttons')[0];
			//var buttons = $$('.content-header-floating-nodisplay');

			if (s > header_offset) {
				if (buttons) {
					if (!buttons.oldParent) {
						buttons.oldParent = buttons.parentNode;
						buttons.oldBefore = buttons.previous();
					}
					if (buttons.oldParent==buttons.parentNode) {
						var dimensions = buttons.placeholder.getDimensions() // Make static dimens.
						buttons.placeholder.style.width = dimensions.width + 'px';
						buttons.placeholder.style.height = dimensions.height + 'px';

						buttons.hide();
						buttons.remove();
						//$(header_copy).down('div').appendChild(buttons);
						$(header_copy).down('div').appendChild(buttons);
						buttons.show();
					}
				}

				//header.style.visibility = 'hidden';
				header_copy.style.display = 'block';
			} else {
				if (buttons && buttons.oldParent && buttons.oldParent != buttons.parentNode) {
					buttons.remove();
					buttons.oldParent.insertBefore(buttons, buttons.oldBefore);
					//buttons.placeholder.style.width = undefined;
					//buttons.placeholder.style.height = undefined;
				}
				header.style.visibility = 'visible';
				header_copy.style.display = 'none';

			}
		}

		//Event.observe(window, 'scroll', floatingTopButtonToolbarToggle);
		//Event.observe(window, 'resize', floatingTopButtonToolbarToggle);
	}
}


prepareSlideDownUpCart();
/* END NEW SECTION FOR THE MENU BLOCK DINAMIC */

