function AbreJanela(URL) { 
  window.open(URL,'JanelaConsulta','width=380,height=550,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,maximized=no');
}

function selQuantity(e, codprod, id){
	var IE = document.all?true:false;
	var cod = codprod.replace(".", "_");
	var conteudo = "";
	var x, y;
	if (IE) {
    x = e.clientX+document.documentElement.scrollLeft -10;
    y = e.clientY+document.documentElement.scrollTop - 70;
	} else {  // grab the x-y pos.s if browser is NS
    x = e.pageX-10;
    y = e.pageY-70;
  }
	for (var i=1; i<=10;i++){
		conteudo += '<a class="btcomprar" id="'+i+'"><span>'+i+'</span></a>';
	}
	$('body').append("<div id='dv_quantity'>"+conteudo+"</div>");
	$('#dv_quantity').css({'top': y+'px','left':x+'px'});
	$("#dv_quantity").mouseleave(function(event){
		$(this).remove();
	});
	$(".btcomprar").click(function(){
		var qt = jQuery(this).attr('id');
		$.ajax({
			type: "POST",
			url: "addtocart.php",
			data: "CodProd="+codprod+"&qt="+qt,
			success: function(msg){
				$("#qt_cart").html(msg);
				//alert('adicionado ao carrinho, continue comprando..');
				//window.open('/produtos.php','_top');
				var botao = $("#a_"+id).html();
				botaoOk(id);
				window.setTimeout(function() {
					voltaBotaoNormal(id, botao);
				}, 1000);
			}
		});
		$("#dv_quantity").remove();
	});
}

function botaoOk(id){
	$("#a_"+id).html("Ok!");
	$("#a_"+id).css("background", "url(/images/button-small-green-right.gif) no-repeat right top");
	$("#p_"+id).css("background", "url(/images/button-small-green-left.gif) no-repeat left top");
}

function voltaBotaoNormal(id, botao){
	$("#a_"+id).html(botao);
	$("#a_"+id).css("background", "url(/images/button-small-black-right.gif) no-repeat right top");
	$("#p_"+id).css("background", "url(/images/button-small-black-left.gif) no-repeat left top");
}

function addToCart(lang){
	var codprod = $("input[name=CodProd]").val();
	var quant = $("input[name=quant]").val();
	if(lang=="en"){
		cartSuccess = "Product(s) added to cart!";
		cartError   = "No items selected!";
	} else if(lang=="es"){
		cartSuccess = "Producto(s) aņadido a la cesta!";
		cartError   = "No hay elementos seleccionados!";
	} else {
		cartSuccess = "Produto(s) adicionado(s) ao carrinho!";
		cartError   = "Nenhum produto selecionado!";
	}
	if(quant!=0){
		jQuery.ajax({
			type: "POST",
			url: "addtocart.php",
			data: "CodProd="+codprod+"&qt="+quant,
			success: function(msg){
				$.colorbox({html:"<div style='min-width: 200px;margin: 10px;'>"+cartSuccess+"</div>"});
				var originalClose = $.colorbox.close; 
				$.colorbox.close = function(){ 
					window.open('/produtos.php','_top');
					originalClose(); 
				};
				return true;
			}
		});
	} else {
		$.colorbox({html:"<div style='min-width: 200px;margin: 10px;'>"+cartError+"</div>"});		
	}
	return false;
}

function addToCart2(lang){
	var count = 0;
	if(lang=="en"){
		cartSuccess = "Product(s) added to cart!";
		cartError   = "No items selected!";
	} else if(lang=="es"){
		cartSuccess = "Producto(s) aņadido a la cesta!";
		cartError   = "No hay elementos seleccionados!";
	} else {
		cartSuccess = "Produto(s) adicionado(s) ao carrinho!";
		cartError   = "Nenhum produto selecionado!";
	}
	if($("input[name=selcart]:checked").length == 0){
		$.colorbox({html:"<div style='min-width: 200px;margin: 10px;'>"+cartError+"</div>"});
	} else {	
		$.each($("input[name=selcart]:checked"), function() {
			var codprod = $(this).val();
			var quant = 1;
			jQuery.ajax({
				type: "POST",
				url: "addtocart.php",
				data: "CodProd="+codprod+"&qt="+quant,
				success: function(msg){}
			});
			count++;
		});
		if(count>0){
			$.colorbox({html:"<div style='min-width: 200px;margin: 10px;'>"+count+' '+cartSuccess+"</div>"});
			var originalClose = $.colorbox.close; 
			$.colorbox.close = function(){ 
				window.open('/produtos.php','_top');
				originalClose(); 
			};
		}
	}
}

function startLightbox(imgSrc) {
	// okay now load the real image!
	newImage = new Image();
	newImage.id = 'lightboxImage';
	newImage.src = imgSrc;
	// Deal with the different browsers
	var position = {
		scrollTop : function () {
			return  window.pageYOffset ||
							document.documentElement && document.documentElement.scrollTop ||
							document.body.scrollTop;
		},
		innerHeight : function () {
			return	window.innerHeight ||
					document.documentElement && document.documentElement.clientHeight ||
					document.body.clientHeight;	
		},
		innerWidth : function () {
			return	window.innerWidth ||
					document.documentElement && document.documentElement.clientWidth ||
					document.body.clientWidth;
		}
	}

	// Get the body overflow state
	var overflowState = $('body').css('overflow');

	// Hide the scroll bars on the right
	$('body').css({overflow:"hidden"});

	// Create the lightboxShade element and append to body
	$('<div id="lightboxShade"></div>').appendTo('body');
	// Style the element
	var lightboxShadeCSS = {
		position: "absolute", top: position.scrollTop() + "px",
		left: "0px", bottom: "auto",
		width: "100%", height: position.innerHeight() + "px",
		background: "black", opacity: "0.8",
		display: "none", "z-index": "100",
		"text-align": "center",	margin: "0",
		padding: "0"
	}
	// Apply the styles
	$('#lightboxShade').css(lightboxShadeCSS);
	// Create the center box
	$('<div id="lightbox"><img id="lightboxClose" src="/si/img/close.png" style="float:right"><img id="lightboxLoading" src="/images/loading.gif" title="7-Day Weather Forecaster"/></div>').appendTo('body');
	// Add an onClick to the image to get rid of the lightbox
	
	//add function to close the lightbox when red X is  clicked 			
	$('#lightboxClose').click(function() {
		// animate the destruction of the lightbox itself 
		$('#lightbox').animate({
			width: '0px',
			height: '0px',
			top: (position.scrollTop() + (position.innerHeight() / 2)) + "px",
			marginLeft : '0px',
			opacity: '0'
		}, 500);
		// fade out the lightbox shade
		$('#lightboxShade').fadeOut(1000, function(){ 
			// make the scroll bars visible again
			$('body').css({overflow:overflowState});
			// destroy the elements
			$('#lightbox').remove();
			$('#lightboxShade').remove();
		});
	});
	
	//add function to close the lightbox when image clicked
	$('#lightboxLoading').click(function() {
		// animate the destruction of the lightbox itself 
		$('#lightbox').animate({
			width: '0px',
			height: '0px',
			top: (position.scrollTop() + (position.innerHeight() / 2)) + "px",
			marginLeft : '0px',
			opacity: '0'
		}, 500);
		// fade out the lightbox shade
		$('#lightboxShade').fadeOut(1000, function(){ 
			// make the scroll bars visible again
			$('body').css({overflow:overflowState});
			// destroy the elements
			$('#lightbox').remove();
			$('#lightboxShade').remove();
		});
	});
	// style the element
	var lightboxCSS = {
		width: "1px", height: "1px", background: "white", opacity: "1", border: "2px solid black",
		top: (position.scrollTop() + (position.innerHeight() / 2)) + "px", 
		left: "50%",  position: "absolute",	"z-index": "100", "text-align":"center"
	};
	// apply the styles
	$('#lightbox').css(lightboxCSS);

	// fade the lightbox in -- shade faster!
	$('#lightboxShade').fadeIn(500);

	// Now animate from 1x1 to a loading box
	var initialHeight = 80;
	var initialWidth = 100;
	$('#lightbox').animate({
		width: initialWidth + 'px',
		height: initialHeight + 'px',
		paddingTop: '20px',
		top: position.scrollTop() + ((position.innerHeight() / 2) - (initialHeight / 2)) + "px",
		marginLeft: "-" + (initialWidth/2) + "px"
	}, 300);
	

	// Test to see if the image is loaded, if it is show it!
	imageLoaded = function() {
		if (newImage.height > 0) {

			imageHeight = newImage.height;
			imageWidth = newImage.width;

			// Is the image bigger than our screen?  Scale it down if it is
			if (imageHeight > imageWidth) {
				if (imageHeight > position.innerHeight()) {
					var percentage = ((position.innerHeight()-20) / imageHeight);
					imageWidth = imageWidth * percentage;
					imageHeight = imageHeight * percentage;
				}
				if (imageWidth > position.innerWidth()) {
					var newPercentage = ((position.innerWidth()-20) / imageWidth);
					imageWidth = imageWidth * newPercentage;
					imageHeight = imageHeight * newPercentage;
				}				
			} else {
				if (imageWidth > position.innerWidth()) {
					var percentage = ((position.innerWidth()-20) / imageWidth);
					imageWidth = imageWidth * percentage;
					imageHeight = imageHeight * percentage;
				}
				if (imageHeight > position.innerHeight()) {
					var newPercentage = ((position.innerHeight()-20) / imageHeight);
					imageWidth = imageWidth * newPercentage;
					imageHeight = imageHeight * newPercentage;
				}
			}
			$('#lightboxLoading').css({"width": imageWidth + "px", "height": imageHeight  + "px"});

			$('#lightboxLoading').css("display","none");
					// Resize the white to fit the image!
		$('#lightbox').animate({
			width: imageWidth + "px",
			height: imageHeight + "px",
			paddingTop: '0px',
			top: position.scrollTop() + ((position.innerHeight() / 2) - (imageHeight / 2)) + "px",
			marginLeft: "-"+(imageWidth/2)+"px"
		}, 500, function() {
			$('#lightboxLoading').attr("src",newImage.src);
			$('#lightboxLoading').css("display","block");
					});

		} else {
			// @TODO What if it never finds the image? Need Max Revs here.. 
			setTimeout("imageLoaded()", 200);
		}
	}
	// Okay now start waiting for the image to load.. 
	setTimeout("imageLoaded()", 200);
	return false;
}

function loadThumbnailImage(addlDetail, largeImgURL){
	$("#media_a").attr("href", largeImgURL);
	$("#media_img").attr("src", addlDetail);
}

function loadVideoMedia(productId){
	$("#media").load("/si/catalog/gadgets/media-display.jsp?prodId="+productId);
}

function PopUP(URL) { 
	window.open(URL,'POPUP','width=600,height=500,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,maximized=no');
}

