// JavaScript Document

// switching between product details cards
function productDetailsCard_ShowHide(c) {
	
	if (c<10) {var c_num="0"+c;} else {var c_num=c;}
	
	$(".card-desc").each(function(i){
		if (i+1<10) {var i_num="0"+(i+1);} else {var i_num=(i+1);}

		if (i+1==c) {
			$("#card-"+c_num+"-hd").removeClass("card-off");
			$("#card-"+c_num+"-hd").addClass("card-on");
			$("#card-"+c_num+"-desc").removeClass("hidden");
		} else {
			$("#card-"+i_num+"-hd").removeClass("card-on");
			$("#card-"+i_num+"-hd").addClass("card-off");
			$("#card-"+i_num+"-desc").addClass("hidden");			
		}
		
	});

}


// opening & closing cart window
function productAddToCart(id,qt) {
	
	// id - product id
	// qt - product quantity
	
	productPromoZoneClose()
	productColorPaletteClose();
	
	$("#detail_popin").show();
	var posX = mouse_x;
	var posY = mouse_y;
	if(posX > 480) {posX = 480;}
	var posY_Window = window.outerHeight;
	var high = (posY - posY_Window);
	$("#detail_popin").css({top:(posY-200)+"px",left:(posX-100)+"px"});
	$("#detail_popin_qt").html(qt);

}

function productCartWindowClose() {

	if ($("#detail_popin")) {
		$("#detail_popin").hide();
	}

}

function product_qt(id) {
	return $("#"+id).val();
}


// opening & closing color palette window
function productColorPalette(id) {

	// id - product id
	
	productPromoZoneClose()
	productCartWindowClose();
	
	$("#detail_color_popin").show();
	var posX = mouse_x;
	var posY = mouse_y;
	if(posX > 480) {posX = 480;}
	var posY_Window = window.outerHeight;
	var high = (posY - posY_Window);
	$("#detail_color_popin").css({top:(posY-200)+"px",left:(posX-100)+"px"});
	
}

function productColorPaletteClose() {

	if ($("#detail_color_popin")) {
		$("#detail_color_popin").hide();
	}

}


// opening & closing image zoom window
function productZoom(id) {

	// id - product id
	
	$("#zoom").show();
	var posX = mouse_x;
	var posY = mouse_y;
	if(posX > 480) {posX = 480;}
	var posY_Window = window.outerHeight;
	var high = (posY - posY_Window);
	$("#zoom").css({top:(posY-200)+"px",left:(posX-100)+"px"});
	
}

function productZoomClose() {
	
	if ($("#zoom")) {
		$("#zoom").hide();
	}

}


// opening & closing product promo window
function productPromoZone(id,disableShopping,disableSelector) {
    if (disableShopping!=undefined) var add = '/d/'+disableShopping; else var add = '';
    if (disableSelector!=undefined) var add2 = '/s/'+disableSelector; else var add2 = '';
    visibleSelects = jQuery("select:visible");
    visibleSelects.hide();
    $("#PromoZone").load('/catalogproduct/getpopin/sku/'+id+add+add2,null,function(responseText){
//        if (responseText.success) {
            productColorPaletteClose();
            productCartWindowClose();

            var arrayPageSize = getPageSize();

            Overlay = true;
            var positionCss = "fixed";
            if (jQuery.browser.msie) positionCss = "absolute";
            jQuery('#' + "popin-product").prependTo("body");
            var zindex = jQuery('#' + "popin-product").css('z-index');
            var overLayZIndex = zindex - 1;
            jQuery('#' + "popin-product").show();
            overLay = jQuery('<div id="storeLocatorOverlay"></div>').css({'background-color':'#888888', height:arrayPageSize[1]+'px',width:'100%',position: positionCss,left:0,top:0, 'z-index':overLayZIndex,opacity:0.5,display:'block'});
            jQuery('body').append(overLay);

            document.getElementById("popin-product").style.display = "block";
//            var posY = mouse_y;
            var h = $.browser.opera && $.browser.version > "9.5" && $.fn.jquery <= "1.2.6" ? document.documentElement["clientHeight"] : $(window).height();
            var popin = $("#popin-product");
            var posY = (h - popin.height())/2 + $(window).scrollTop()  + 'px';
//            var posY = clickY;
//            document.getElementById("popin-product").style.top = (posY - 220) + "px";
            document.getElementById("popin-product").style.top = posY;

            LoadScrollV();

            if (disableShopping!=undefined) {
                $('#popin_art_no').val($('#cart_art_no_'+disableShopping).val());
                $('#popin_rowid').val($('#cart_rowid_'+disableShopping).val());
                $('#popin_sztuk').val($('#cart_sztuk_'+disableShopping).val());
            }
//        }
    });
}

function productPromoZoneClose() {
	
	if (jQuery("select:hidden")) {
		visibleSelects = jQuery("select:hidden");
		visibleSelects.show();
	}
	if ($("#popin-product")) {
		$("#popin-product").hide();
	}
	if ($("#storeLocatorOverlay")) {
		$("#storeLocatorOverlay").remove();
	}
	
}


//
var mouse_x = 0;
var mouse_y = 0;
var scroll_x = 0;
var scroll_y = 0;
function mousePosition(evt){
    if( !evt ) evt = window.event;
    if (evt.pageX || evt.pageY){
		mouse_x = evt.pageX;
		mouse_y = evt.pageY;
	}
	else if (evt.clientX || evt.clientY) 	{
	    if(document.documentElement) {
	    	scroll_x = document.documentElement.scrollLeft;
	    	scroll_y = document.documentElement.scrollTop;
	    }else if(document.body) {
			scroll_x = document.body.scrollLeft;
			scroll_y = document.body.scrollTop;
	    }
	    mouse_x = evt.clientX + scroll_x;
	    mouse_y = evt.clientY + scroll_y;
    } 
}
document.onmousemove = mousePosition;


//
function getElementsByClassName (oElm, strTagName, oClassNames) {
	var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var arrRegExpClassNames = new Array();
	if(typeof oClassNames == "object") {
		for(var i=0; i<oClassNames.length; i++) {
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
		}
	}
	else {
		arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
	}
	var oElement;
	var bMatchesAll;
	for(var j=0; j<arrElements.length; j++) {
		oElement = arrElements[j];
		bMatchesAll = true;
		for(var k=0; k<arrRegExpClassNames.length; k++) {
			if(!arrRegExpClassNames[k].test(oElement.className)) {
				bMatchesAll = false;
				break;
			}
		}
		if(bMatchesAll) {
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function LoadScrollV() {
	if (document.getElementById("stretcherContainer")) {
  		TabDivs = getElementsByClassName(document.getElementById("stretcherContainer"), "div", "stretcher");  	
	  	var myScroll;
	  	myScroll = new ScrollObj(6,40,130,"track1","up1","down1","drag1","contentMaskTab1","contentTab1");
	}
}


// Scroller
var dom = document.getElementById;
var iex = document.all;
var ns4 = document.layers;

function addEventScroll(event,method){
	this[event] = method;
	if(ns4) this.captureEvents(Event[event.substr(2,event.length).toUpperCase()]);
}
function removeEventScroll(event){
	this[event] = null;
	if(ns4) this.releaseEvents(Event[event.substr(2,event.length).toUpperCase()]);
}
function getElementScroll(name,nest){
	nest = nest ? "document."+nest+"." : "";
	var el = dom ? document.getElementById(name) : iex ? document.all[name] : ns4 ? eval(nest+"document."+name) : false;
	el.css = ns4 ? el : el.style;
	el.getTop = function(){return parseInt(el.css.top) || 0};
	el.setTop = function(y){el.css.top = ns4 ? y: y+"px"};
	el.getHeight = function(){return ns4 ? el.document.height : el.offsetHeight};
	el.getClipHeight = function(){return ns4 ? el.clip.height : el.offsetHeight};
	el.hideVis = function(){el.css.visibility="hidden"};
	el.addEventScroll = addEventScroll;
	el.removeEventScroll = removeEventScroll;
	return el;
}
function getYMouse(e){
	return iex ? event.clientY : e.pageY;
}

document.addEventScroll = addEventScroll;
document.removeEventScroll = removeEventScroll;

ScrollObj = function(speed, dragHeight, trackHeight, trackObj, upObj, downObj, dragObj, contentMaskObj, contentObj){
	this.speed = speed;
	this.dragHeight = dragHeight;
	this.trackHeight = trackHeight;
	this.trackObj = getElementScroll(trackObj);
	this.upObj = getElementScroll(upObj);
	this.downObj = getElementScroll(downObj);
	this.dragObj = getElementScroll(dragObj);
	this.contentMaskObj = getElementScroll(contentMaskObj);
	this.contentObj = getElementScroll(contentObj,contentMaskObj);
	this.obj = contentObj+"Object";
	eval(this.obj+"=this");
	
	this.trackTop = this.dragObj.getTop();
	this.trackLength = this.trackHeight-this.dragHeight;
	this.trackBottom = this.trackTop+this.trackLength;
	this.contentMaskHeight = this.contentMaskObj.getClipHeight();
	this.contentHeight = this.contentObj.getHeight();
	this.contentLength = this.contentHeight-this.contentMaskHeight;
	this.scrollLength = this.trackLength/this.contentLength;
	this.scrollTimer = null;
	
	if(this.contentHeight <= this.contentMaskHeight){
		this.dragObj.hideVis();
		this.upObj.hideVis();
		this.downObj.hideVis();
		this.trackObj.hideVis();
	}else{
		var self = this;
		this.trackObj.addEventScroll("onmousedown", function(e){self.scrollJump(e);return false});
		this.upObj.addEventScroll("onmousedown", function(){self.scroll(self.speed);return false});
		this.upObj.addEventScroll("onmouseup", function(){self.stopScroll()});
		this.upObj.addEventScroll("onmouseout", function(){self.stopScroll()});
		this.downObj.addEventScroll("onmousedown", function(){self.scroll(-self.speed);return false});
		this.downObj.addEventScroll("onmouseup", function(){self.stopScroll()});
		this.downObj.addEventScroll("onmouseout", function(){self.stopScroll()});
		this.dragObj.addEventScroll("onmousedown", function(e){self.startDrag(e);return false});
		if(iex) this.dragObj.addEventScroll("ondragstart", function(){return false});
	}
}
ScrollObj.prototype.startDrag = function(e){
	this.dragStartMouse = getYMouse(e);
	this.dragStartOffset = this.dragObj.getTop();
	var self = this;
	document.addEventScroll("onmousemove", function(e){self.drag(e)});
	document.addEventScroll("onmouseup", function(){self.stopDrag()});
}
ScrollObj.prototype.stopDrag = function(){
	document.removeEventScroll("onmousemove");
	document.removeEventScroll("onmouseup");
}
ScrollObj.prototype.drag = function(e){
	var currentMouse = getYMouse(e);
	var mouseDifference = currentMouse-this.dragStartMouse;
	var dragDistance = this.dragStartOffset+mouseDifference;
	var dragMovement = (dragDistance<this.trackTop) ? this.trackTop : (dragDistance>this.trackBottom) ? this.trackBottom : dragDistance;
	this.dragObj.setTop(dragMovement);
	var contentMovement = -(dragMovement-this.trackTop)*(1/this.scrollLength);
	this.contentObj.setTop(contentMovement);
}
ScrollObj.prototype.scroll = function(speed){
	var contentMovement = this.contentObj.getTop()+speed;
	var dragMovement = this.trackTop-Math.round(this.contentObj.getTop()*(this.trackLength/this.contentLength));
	if(contentMovement > 0){
		contentMovement = 0;
	}else if(contentMovement < -this.contentLength){
		contentMovement = -this.contentLength;
	}
	if(dragMovement < this.trackTop){
		dragMovement = this.trackTop;
	}else if(dragMovement > this.trackBottom){
		dragMovement = this.trackBottom;
	}
	this.contentObj.setTop(contentMovement);
	this.dragObj.setTop(dragMovement);
	this.scrollTimer = window.setTimeout(this.obj+".scroll("+speed+")",25);
}
ScrollObj.prototype.stopScroll = function(){
	if(this.scrollTimer){
		window.clearTimeout(this.scrollTimer);
		this.scrollTimer = null;
	}
}
ScrollObj.prototype.scrollJump = function(e){
	var currentMouse = getYMouse(e);
	var dragDistance = currentMouse-(this.dragHeight/2);
	var dragMovement = (dragDistance<this.trackTop) ? this.trackTop : (dragDistance>this.trackBottom) ? this.trackBottom : dragDistance;
	this.dragObj.setTop(dragMovement);
	var contentMovement = -(dragMovement-this.trackTop)*(1/this.scrollLength);
	this.contentObj.setTop(contentMovement);
}


//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}