// JavaScript Document

function openInNewTab(URL) {
var temporalForm = document.createElement('form');
with (temporalForm) {
setAttribute('method', 'GET');
setAttribute('action', URL);
setAttribute('target', '_blank');
}

var paramsString = URL.substring(URL.indexOf('?') + 1, URL.length);
var paramsArray = paramsString.split('&');

for (var i = 0; i < paramsArray.length; ++i) {
var elementIndex = paramsArray[i].indexOf('=');
var elementName = paramsArray[i].substring(0, elementIndex);
var elementValue = paramsArray[i].substring(elementIndex + 1, paramsArray[i].length);

var temporalElement = document.createElement('input');
with(temporalElement) {
setAttribute('type', 'hidden');
setAttribute('name', elementName);
setAttribute('value', elementValue);
}

temporalForm.appendChild(temporalElement);
}

document.body.appendChild(temporalForm);
temporalForm.submit();
document.body.removeChild(temporalForm);
}

j(document).click(function(e){
mvtipo=e.target;
});

function GetSelectedText () {

if (window.getSelection) {        // Firefox, Opera, Safari
	if (mvtipo.type=='text' | mvtipo.type=='textarea'){
		var startPos = mvtipo.selectionStart;
		var endPos = mvtipo.selectionEnd;
		var misel=mvtipo.value.substr(startPos, endPos - startPos);
			if (misel.length>0){
				estoes=misel;
			}else{
				estoes=mvtipo.value;
			}
	}else{
		var range = window.getSelection ();                               
		estoes=range.toString();
	}
}else{
	if (document.selection.createRange) {        // Internet Explorer
		var range = document.selection.createRange ();
		estoes=range.text;
	}
}
return estoes;
}

function funcopia(){
		clip = new ZeroClipboard.Client();
		clip.addEventListener('mouseOver', function (client) {
			clip.setText(GetSelectedText());
		});
		clip.addEventListener('complete', function (client, text) {
			alert("Copiado, use Ctrl+V para pegar");
			clip.destroy();
		});
		clip.glue('copiar');
}

j(document).ready(function(){ 
						   
setupZoom();

var menuId = "smenu";
var menu = j("#"+menuId);

j(document).bind("contextmenu", function(e){
										 
	if (clip) clip.destroy();
										 
	setTimeout("funcopia()",100);
	
	myurl=e.target.parentNode.href;
	if (myurl==null){
	j("#lmenu").css("display", "none");
	menu.css({'display':'block', 'left':e.pageX, 'top':e.pageY});
	}else{
	menu.css("display", "none");
	j("#lmenu").css({'display':'block', 'left':e.pageX, 'top':e.pageY});
	}
	return false;
});

menu.click(function(e){
					
	if(e.target.className == "disabled"){
		return false;
	}
	else{
		switch(e.target.id){
			case "menu_anterior":
				history.back(-1);
				break;
			case "menu_siguiente":
				history.go(1);
				break;
			case "menu_recargar":
				document.location.reload();
				break;
			case "menu_web":
				document.location="http://thevitaminplanet.es";
				break;
			case "menu_favoritos":
				var title = "The Vitamin Planet - Tu tienda de nutrición online";
				var url = "http://thevitaminplanet.es";
				if(window.sidebar)
					window.sidebar.addPanel(title, url, "");
				else if(window.external)
					window.external.AddFavorite(url, title);
				break;
			case "menu_aumentar":
				document.location="./scripts/changeview.php?css=1024&url=" + document.location.href;
				break;
			case "menu_reducir":
				document.location="./scripts/changeview.php?css=764&url=" + document.location.href;
				break;
		}
		menu.css("display", "none");
	}
});

j("#lmenu").click(function(e){
	if(e.target.className == "disabled"){
		return false;
	}
	else{
		switch(e.target.id){
			case "menu_mventana":
				document.location=myurl;
				break;
			case "menu_nventana":
				openInNewTab(myurl);
				break;
		}
		menu.css("display", "none");
	}
});

j(document).click(function(e){
	if(e.button == 0 && e.target.parentNode.parentNode.id != menuId){
		menu.css("display", "none");
		j("#lmenu").css("display", "none");
		if (clip) clip.destroy();
	}
});

j(document).keydown(function(e){
	if(e.keyCode == 27){
		menu.css("display", "none");
		j("#lmenu").css("display", "none");
		if (clip) clip.destroy();
	}
});

});
