var slideProcesses = new Array();
var slideTo = getScrollPosition();
var isSliding = false;
var currSlidingPosition = 0;

function showProductInfo(url, productId, productTypeName, productName) {
	if(typeof(productList) != 'undefined' && typeof(productListIndex) != 'undefined') {
		currentCounter = productListIndex[productId];
		previousCounter = currentCounter-1;
		if(previousCounter >= 0) url += '&previous=' + productList[previousCounter];
		nextCounter = currentCounter+1;
		if(typeof(productList[nextCounter]) != 'undefined') url += '&next=' + productList[nextCounter];
	}
	showModalPopup(url,600,400);
	
}

//function to execute when the customer changes the variant product
function updateProductInfo(product_url, productId) {
	if(typeof(productList) != 'undefined' && typeof(productListIndex) != 'undefined') {
		currentCounter = productListIndex[productId];
		previousCounter = currentCounter-1;
		if(previousCounter >= 0) product_url += '&previous=' + productList[previousCounter];
		nextCounter = currentCounter+1;
		if(typeof(productList[nextCounter]) != 'undefined') product_url += '&next=' + productList[nextCounter];
	}
	updateModalPopup(product_url,600,400);
	try {
        
	} catch(err) {
		alert(err);
		//logError('http://www.tailorstore.fr/js_error_logger', err, 'updateProductInfo');
	}
}

function addToCart(thekey, createBox, showGoToCartLink) {
	try {
		var cmd = 'http://www.tailorstore.fr/panier/addnew/'+thekey+'?isAjax=1';

	    var xmlhttp=false;
	    try {
	        xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
	        } catch (e) {
	        try {
	             xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	        } catch (E) {
	             xmlhttp = false;
	        }
	     }
	     if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	         xmlhttp = new XMLHttpRequest();
	     }
		if(document.getElementById('product_info_module_div')) document.getElementById('product_info_module_div').innerHTML = '';
	    xmlhttp.open('GET', cmd, false);

	    xmlhttp.send(null);
		var content = xmlhttp.responseText;
		showCartSummary();
		var cartContents = document.getElementById('cart_contents');
		if(typeof(cartContents) != 'undefined' && cartContents != null) {
			cartContents.innerHTML = content;
		}

		var showGoToCartAdd = '';
		if(showGoToCartLink) showGoToCartAdd = '?show_go_to_cart=1';

		if(createBox) {
			showModalPopup('http://www.tailorstore.fr/cart_message/null/' + thekey + showGoToCartAdd,360,200);
		} else {
            updateModalPopup('http://www.tailorstore.fr/cart_message/null/' + thekey + showGoToCartAdd,360,180);
		}
		

	} catch(err) {
		logError('http://www.tailorstore.fr/js_error_logger', err, 'addToCart');
	}
}

function getClientWindowHeight() {
	if (self.innerHeight) // all except Explorer
	{
		return self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		return document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		return document.body.clientHeight;
	}
}

function getScrollPosition() {
	var scrollPos;
	if (typeof window.pageYOffset != 'undefined') {
	   scrollPos = window.pageYOffset;
	}
	else if (typeof document.compatMode != 'undefined' &&
	     document.compatMode != 'BackCompat') {
	   scrollPos = document.documentElement.scrollTop;
	}
	else if (typeof document.body != 'undefined') {
	   scrollPos = document.body.scrollTop;
	}
	return scrollPos;
}

function moveRightDiv() {
	if(isSliding) { // if it's already sliding stop the current sliding processes
		for(var i = 0; i < slideProcesses.length; i++) {
			window.clearTimeout(slideProcesses[i]);
		}
		slideProcesses = new Array();
		isSliding = false;
	}
	// get the height of the right div
	if(!document.getElementById('right_div')) return;
	var elementHeight = document.getElementById('right_div').clientHeight;
	var windowHeight = getClientWindowHeight();
	var contentHeight = document.getElementById('contentframe').clientHeight; // get the height of the contentframe and make sure that the right div won't scroll over that

	oldScroll = slideTo;
	slideTo = getScrollPosition();
	// element is smaller than the window (should be kept at the beginning of the page)
	if(elementHeight <= windowHeight && slideTo > 180) {
		isSliding = true;
		if(currSlidingPosition < slideTo) {
			// sliding down
			for(var newSlide = currSlidingPosition; newSlide <= slideTo; newSlide++) {
				var delay = (newSlide-currSlidingPosition)*1;
				slideProcesses[slideProcesses.length] = window.setTimeout('doDivMove(' + newSlide + ', ' + slideTo + ', ' + (newSlide-180) + ')' , delay);
			}
		} else {
			// sliding down
			for(var newSlide = currSlidingPosition; newSlide >= slideTo; newSlide--) {
				var delay = (currSlidingPosition-newSlide)*1;
				slideProcesses[slideProcesses.length] = window.setTimeout('doDivMove(' + newSlide + ', ' + slideTo + ', ' + (newSlide-180) + ')' , delay);
			}
		}
		//document.getElementById('right_div').style.top=slideTo-180 + 'px';
	}
	// element is bigger than window (should be kept at the beginning if srolling up and at the end if srolling down)
	else if(elementHeight > windowHeight) {
		if(slideTo > elementHeight-windowHeight+180 && currSlidingPosition < slideTo && slideTo+windowHeight < contentHeight+180) {
			// sliding down
			isSliding = true;
			for(var newSlide = currSlidingPosition; newSlide <= slideTo; newSlide++) {
				var delay = (newSlide-currSlidingPosition)*1;
				slideProcesses[slideProcesses.length] = window.setTimeout('doDivMove(' + newSlide + ', ' + slideTo + ', ' + (newSlide-(elementHeight-windowHeight)-180) + ')' , delay);
			}
		} else if(slideTo < slideTo+windowHeight && slideTo > 180 && currSlidingPosition > slideTo && slideTo+windowHeight < contentHeight) {
			// sliding up
			isSliding = true;
			for(var newSlide = currSlidingPosition; newSlide >= slideTo; newSlide--) {
				var delay = (currSlidingPosition-newSlide)*1;
				slideProcesses[slideProcesses.length] = window.setTimeout('doDivMove(' + newSlide + ', ' + slideTo + ', ' + (newSlide-180) + ')' , delay);
			}
			//document.getElementById('right_div').style.top=slideTo-180 + 'px';
		}
	}
	if(slideTo <= 180) document.getElementById('right_div').style.top='0px'; // set to initial position
}

function doDivMove(newSlide, endSlide, topPosition) {
	if(isSliding) {
		document.getElementById('right_div').style.top=topPosition + 'px';
	}
	currSlidingPosition = newSlide;
	if(newSlide == endSlide) isSliding = false; // the end of sliding
}
