var itensComparacao = new Array();
var canDrop = false;
var clientHeight;
var topWindowScroll;
var lastObj, cloneObj;
var intervalo, seconds;

jsRoger.Include("Element.SetDrag");
jsRoger.Include("Number.round");
jsRoger.Include("String.toInteger");

function SetOpacity(elem, opacityAsInt) {
	var opacityAsDecimal = opacityAsInt;

	if (opacityAsInt > 100)
		opacityAsInt = opacityAsDecimal = 100; 
	else if (opacityAsInt < 0)
		opacityAsInt = opacityAsDecimal = 0; 

	opacityAsDecimal /= 100;
	if (opacityAsInt < 1)
		opacityAsInt = 1;

	elem.style.opacity = (opacityAsDecimal);
	elem.style.filter  = "alpha(opacity=" + opacityAsInt + ")";
}

function removeProdutoComparacao(obj) {
	var codigoProduto = jQuery(obj).parent().attr("title");
	jQuery("#compare-aqui-cart div[title='" + codigoProduto + "']").remove();
	itensComparacao.splice(jQuery.inArray(codigoProduto, itensComparacao), 1);
}

function compararItens() {
	window.location.href = '/comparativo/'+itensComparacao;
};

jsRoger.Window.DOMEvents.push(function(){
	jsRoger.Element.myDrag = jsRoger.Element.SetDrag;

	jsRoger.Element.myDrag.MouseUp = function(event) {
		var obj		= this.obj;
		if ( obj ) {
			var main	= obj.drag_main;
			if ( main ) {
				if ( main.onDragEnd ) {
					main.onDragEnd( this.feedBackX, this.feedBackY);
				}
			} else {
				if ( obj.onDragEnd ) {
					obj.onDragEnd( this.feedBackX, this.feedBackY);
				}
			}
			this.obj		= null;
			this.style.display	= "none";
			this.style.visibility	= "hidden";
			//obj.style.zIndex	= 0;
		}
		var glassPane	= jsRoger.Element.myDrag.glassPane;
		document.onmousemove	= glassPane.__originalMouseMove;
		document.onmouseup	= glassPane.__originalMouseUp;

		jQuery(".block-cart", glassPane).remove();
		cloneObj = undefined;

		if ( glassPane.parentNode == document.body ) {
			document.body.removeChild( glassPane);
		}
	};


	jsRoger.Element.myDrag.MouseDown = function(obj, e) {
		if (!cloneObj) { // && e.button == 0
			lastObj = obj;
			obj = jQuery(obj).clone();
			obj[0].style.position = "absolute";
			cloneObj = obj = obj[0];

			seconds = 0;
			intervalo = setInterval(function() {
                ++seconds;
            }, 190);

			cloneObj.onmouseup = function(e) {
				if (seconds <= 1 && !canDrop)
					window.location.href = jQuery("a", this)[0].href;
				//e.preventPropagate()
			};
			jsRoger.Element.myDrag(cloneObj);

			jQuery("a", obj).removeClass().addClass("single-column");
	
			var main	= obj.drag_main || obj;
	
			if ( jsRoger.Element.myDrag._isFirstTime ) {
				//glassPane
				var glassPane                 = jsRoger(document.createElement("DIV"));
				glassPane.style.position      = "absolute";
				glassPane.style.zIndex        = 100001;
				glassPane.style.display       = 
					glassPane.style.MozUserFocus  = 
						glassPane.style.MozUserSelect = "none";
				jQuery(glassPane).addClass("cards");
	
				glassPane.onselectstart = 
					glassPane.ondragstart   = 
						glassPane.ondrag        = function () { return false; };
	
						glassPane.onMouseDown = jsRoger.Element.myDrag.MouseMove;
						glassPane.onMouseMove = jsRoger.Element.myDrag.MouseMove;
						glassPane.onMouseUp   = jsRoger.Element.myDrag.MouseUp;
	
						jsRoger.Element.myDrag.glassPane = glassPane;
	
						glassPane.style.backgroundImage	= "url("+ jsRoger.root +"Image/util/blank.gif)";
	
						jsRoger.Element.myDrag._isFirstTime = false;
			}
	
			var glassPane = jsRoger.Element.myDrag.glassPane;
			glassPane.appendChild(obj);
			if ( !obj ) {
				return null;
			}
	
			if ( !obj._dragInitialized ) {
				jsRoger.Element.myDrag( obj);
			}
	
			glassPane.obj		= jsRoger(obj);
			glassPane.dragScale	= obj.dragScale || 100;
			glassPane.maxL		= main.maxL!=null? main.maxL: obj.maxL;
			glassPane.maxR		= main.maxR!=null? main.maxR: obj.maxR;
			glassPane.maxT		= main.maxT!=null? main.maxT: obj.maxT;
			glassPane.maxB		= main.maxB!=null? main.maxB: obj.maxB;
	
			obj.bDragX		= obj.bDragX==null? true: obj.bDragX;
			obj.bDragY		= obj.bDragY==null? true: obj.bDragY;
			
			glassPane.style.display		= "";
			glassPane.style.visibility	= "visible";
			glassPane.overflow = "hidden";
	
			glassPane.style.left		= window.getScrollLeft() +"px";
			glassPane.style.top		= window.getScrollTop() +"px";
			glassPane.style.width		= (window.getScrollLeft() + window.getWidth()  - 20 ) +"px";
			glassPane.style.height		= (window.getScrollTop()  + window.getHeight() - 20 ) +"px";
	
			glassPane.objZIndex		= obj.style.zIndex || "";
			obj.style.zIndex		= 9999;
	
			glassPane.__originalMouseMove	= document.onmousemove;
			glassPane.__originalMouseUp	= document.onmouseup;
	
			glassPane.onmousemove	= function () { glassPane.onMouseMove; return false; };
			glassPane.onmouseup	    = function () { glassPane.onMouseUp; return false; };
	
			document.onmousemove	= function(e) { glassPane.onMouseMove(e); return false; };
			document.onmouseup	= function(e) { glassPane.onMouseUp(e); return false; };
	
			obj.style.zIndex++;
			if (e && e.preventDefault) {
				e.preventDefault();
			} else if (window.event) {
				window.event.returnValue = false;
			}
	
			if ( main.onDragStart ) {
				main.onDragStart( obj.feedBackX, obj.feedBackY);
			}
	
		}
		return false;
	};

	jsRoger.Element.myDrag.MouseMove = function( e) {
		if (jsRoger.Element.myDrag.glassPane.parentNode != document.body) {
			document.body.appendChild( jsRoger.Element.myDrag.glassPane);
		}
		
		e = window.event || e;

		if (this.obj) {
			var obj			= this.obj;
			var screenX		= e.clientX;
			var screenY		= e.clientY;
			
			var posX		= screenX - 91;
			var posY		= screenY - 150;

			var maxL		= this.maxL;
			var maxT		= this.maxT;
			var maxR		= this.maxR;
			var maxB		= this.maxB;

			obj._mouseX	= posX;
			obj._mouseY	= posY;

			if (obj.bDragX) {
				obj.style.left		= posX +"px";
				this.feedBackX	= posX;
			}

			if (obj.bDragY) {
				if ( maxT != null ) {
					posY	= Math.max( maxT, Math.min(posY, maxB));
				}
				obj.style.top		= posY +"px";

				if ( maxT != null ) {
					this.feedBackY	= (this.dragScale/(maxB-maxT)) * (obj.offsetTop-maxT-this.offsetIntTop);
				} else {
					this.feedBackY	= 0;
				}
			}

			jsRoger(cloneObj).onDrag = function(x, y) {
				if (document.getElementById && document.all) { this.onmousedown(); }
				lastObj = this;
				SetOpacity(cloneObj, 50);

				topWindowScroll = jQuery("#compare-aqui").offset().top + 10;

				if (jsRoger("compare-aqui").className == "ca-cart" || jsRoger("compare-aqui").className == "ca-cart-hover") {
					canDrop = false;
					jsRoger("compare-aqui").className = "ca-cart-active";
					jQuery("#compare-texto").removeClass().addClass("ca-texto2");
					jQuery(".bt-remover-item").removeClass("bt-remover-item-dark").addClass("bt-remover-item-light");
				}

				if (jQuery(this).offset().top + 300 >= topWindowScroll) {
					canDrop = true;
					jsRoger("compare-aqui").className = "ca-cart-hover";
					jQuery("#compare-texto").removeClass().addClass("ca-texto2");
					jQuery(".bt-remover-item").removeClass("bt-remover-item-dark").addClass("bt-remover-item-light");
				}

			};
			jsRoger(cloneObj).onDragEnd = function() {
				clearTimeout(intervalo);
				if (document.getElementById && document.all) {
					if (this.style.left == "0px" && this.style.top  == "0px")
						window.location.href = this.getElementsByTagName("a")[0].href;
				}

				this.style.left = "0px";
				this.style.top  = "0px";
				jQuery("#compare-texto").removeClass().addClass("ca-texto1");

				jQuery(".bt-remover-item").removeClass("bt-remover-item-light").addClass("bt-remover-item-dark");

				jsRoger("compare-aqui").className = "ca-cart";
				if (canDrop) {
					var codigoProduto = jQuery(this).find("span.code").attr("title");
					if (itensComparacao.length >= 4 || jQuery.inArray(codigoProduto, itensComparacao) >= 0) return false;

					var imagemProduto = jQuery(this).find("span.img img").clone().removeAttr("style");

					itensComparacao.push(codigoProduto);

					var botaoRemover = document.createElement("div");
					botaoRemover.setAttribute("title", "Remover");
					botaoRemover.setAttribute("class", "bt-remover-item bt-remover-item-dark");
					botaoRemover.onclick = function() { removeProdutoComparacao(this); };

					var imagemRemover = document.createElement("img");
					imagemRemover.setAttribute("src", jQuery(imagemProduto).attr("src"));
					imagemRemover.setAttribute("title", jQuery(imagemProduto).attr("title"));
					imagemRemover.setAttribute("style", jQuery(imagemProduto).attr("style"));

					var spanRemover = document.createElement("div");
					spanRemover.setAttribute("class", "ca-cart-produtos");
					spanRemover.setAttribute("title", codigoProduto);

					/**
					 * Styles para IE7
					 */
					if (jsRoger.browser.msie && jsRoger.browser.version <= 7) {
						imagemRemover.style.display = "";
						imagemRemover.style.position = "relative";

						botaoRemover.style.display = "";
						botaoRemover.style.position = "relative";
						botaoRemover.style.width   = "13px";
						botaoRemover.style.height  = "16px";
						botaoRemover.style.cursor  = "pointer";
						botaoRemover.style.background = "transparent url(\"/imgs/categories/remover-do-comparativo.png\") no-repeat top left";

						jQuery(spanRemover).css({float: "left"});
						spanRemover.style.display = "inline";
						spanRemover.style.width = "85px";
						spanRemover.style.height = "61px";
						/*
						 * ie6
						if (jsRoger.browser.msie && jsRoger.browser.version < 7)
							spanRemover.style.margin = "30px 0 0 0";
						*/
						document.getElementById("compare-aqui-cart").style.float = "none";
						document.getElementById("compare-aqui-cart").style.display = "block";
						document.getElementById("compare-aqui-cart").style.position = "relative";
						document.getElementById("compare-aqui-cart").style.width = "345px";
						document.getElementById("compare-aqui-cart").style.height = "50px";
						document.getElementById("compare-aqui-cart").style.textAlign = "left";
					}

					spanRemover.appendChild(imagemRemover);
					spanRemover.appendChild(botaoRemover);

					document.getElementById("compare-aqui-cart").appendChild(spanRemover);
				}
				canDrop = false;
			};

			if ( cloneObj.onDrag ) {
				cloneObj.onDrag( cloneObj.feedBackX, cloneObj.feedBackY);
			} else if ( obj.onDrag ) {
				obj.onDrag( obj.feedBackX, obj.feedBackY);
			}
		}
	};

	if (!jsRoger.browser.msie || jsRoger.browser.msie && jsRoger.browser.version >= 7) {
		jQuery("label.compare").remove();
		jsRoger("::div.block-cart").each(function() {
			jQuery(this).find("a")
			.append(jQuery("<span/>")
					.addClass("arraste")
					.html("Arraste para comparar"));
			this.style.zIndex = 0;
			jsRoger.Element.myDrag(this);
		});
	}
});
