$(window).addEvent('domready', function(){initUserControlPanel();});

function initUserControlPanel(){
	if($('rememberPassword')) initFormFields('rememberPassword');
	initWishlistTable();
}

function loadUserContent(nameFile, nameDiv, callBack, ajaxLoading) {
	if(checkSession() == false){window.location.href = shop.url + '/ecommerce/users/user.cfm'; return;}
	else loadContent(nameFile, nameDiv, callBack, ajaxLoading);
}

function checkSessionInUserOrder(){
	if(checkSession() == false){window.location.href = shop.url + '/ecommerce/users/userControlPanel.cfm'; Shadowbox.close(); return false;}
	return true;
}

function checkSession()
{
	var xmlObj = createXMLRequest();
	
	xmlObj.open('GET', shop.url + '/ajaxTemplates/users/userControlPanel/checkSession.cfm?'+getUniqueId(), false);
	xmlObj.send(null);
	if(xmlObj.responseText.indexOf('0') != -1)return false;
	return true;
}

function showMessageDeleteAccount(formAction){
	if(checkSession() == false){window.location.href = shop.url + '/ecommerce/users/user.cfm'; return false;}
	shop.cuteQuestion.addMessage('', languageSheet.lblDeleteAccount, languageSheet.lblYes, languageSheet.lblNo, 1, "yes", formAction, 0,0);
}

/*FUNCTIONS IN WISHLIST USER CONTROL PANEL*/
function initWishlistTable(){
	if($('listWishlistDetailsId')) $('listWishlistDetailsId').value = "";
	if($('listProductsIdToEmail')) $('listProductsIdToEmail').value = "";
	if($('listProductsId')) $('listProductsId').value = "";
	$$('.checkWishlistDetail').each(function(el){el.checked = false;});
	
}

function initWishlistToEmail(obj){
	$('informationBox').style.visibility = 'visible';
	initFormFields("wishlistToEmail");
}

function findInArray(array, stringToSearch){
	for (var i = 0; i < array.length; i++){
		if(array[i] == stringToSearch) return i;
	}
	return -1;
}

function generateListProductsId(productId){
	var content = $('listProductsId').value;

	if(content.length > 0)var arrContent = content.split(',');
	else var arrContent = new Array();
	
	var pos = findInArray(arrContent, productId);
	if(pos >= 0)arrContent.splice(pos, 1);
	else arrContent.push(productId);
	
	content = arrContent.join(",");
	$('listProductsId').value = content;
	$('listProductsIdToEmail').value = content;

}

function emptyListListProductsId(){
	$('listProductsId').value = "";
	$('listProductsIdToEmail').value = "";

}

function generateListWishlistDetailsId(wishlistDetailId){
	var content = $('listWishlistDetailsId').value;

	if(content.length > 0)var arrContent = content.split(',');
	else var arrContent = new Array();
	
	var pos = findInArray(arrContent, wishlistDetailId);
	if(pos >= 0)arrContent.splice(pos, 1);
	else arrContent.push(wishlistDetailId);
	
	content = arrContent.join(",");
	$('listWishlistDetailsId').value = content;
}

function emptyListWishlistDetailsId(){
	$('listWishlistDetailsId').value = "";
}


function addProducts(form){
	$('productList').value = $('listProductsId').value;
//	loadContent(shop.url + '/ajaxTemplates/addProducts.cfm?listProductId='+content, 'wishlistTable', 'reloadMiniBaskets()','');
	return Spry.Utils.submitForm(form, reloadMiniBaskets );
}


function deleteWishList(){
	if(checkSession() == false){window.location.href = shop.url + '/ecommerce/users/user.cfm'; return;}
	var content = $('listWishlistDetailsId').value;
	
	var arrContent = content.split(',');
	content = arrContent.join(',');
	if(content.length>0){
		loadContent(shop.url + '/ajaxTemplates/users/userControlPanel/wishlistDelete.cfm?listWishlistDetailsId='+content, 'wishlistTable', "initWishlistTable()",'');
	}
	else{
		shop.cuteAlert.addMessage(languageSheet.lblError, languageSheet.lblSelectProducts,0,0,350,130);
	}

}

function generateEmail(){
	if(checkSession() == false){window.location.href = shop.url + '/ecommerce/users/user.cfm'; return;}
	var content = $('listProductsIdToEmail').value;
	
	/*Shadowbox.open({
			player:     'ajax',
			content:    shop.url + '/ajaxTemplates/wishlistToEmail.cfm?listProductsId='+content,
			width:			470,
			height:			375
	}, {onFinish: function() {initWishlistToEmail();}}
	);	*/
	var arrContent = content.split(',');
	content = arrContent.join(',');
	if(content.length>0){
		loadContent(shop.url + '/ajaxTemplates/users/userControlPanel/wishlistToEmail.cfm?listProductsId='+content, 'informationBox', 'initWishlistToEmail();', '');
	}
	else{
			shop.cuteAlert.addMessage(languageSheet.lblError, languageSheet.lblSelectProducts,0,0,350,130);
	}
}

function selectAllProductsWishlist(){
	$$('.checkWishlistDetail').each(
																	function(el){
																					 var arrId = el.id.split("_");
																					 if(el.checked == true){
																						 	el.checked = false;
																						 	emptyListWishlistDetailsId();
																							emptyListListProductsId();
																					 }
																					 else{
																						 generateListWishlistDetailsId(arrId[0]); 
																						 generateListProductsId(arrId[1]); 
																						 el.checked = true;
																					 }
																					 changeClass($('divWishlistDetail'+arrId[0]), 'selectRow', 'selectedRow');
																	});
}

/*END FUNCTIONS IN WISHLIST USER CONTROL PANEL*/

var interval_ID;
var maxWidth;
var maxWidthMenuTabs;

var interval_ID;
var maxHeight;
var maxHeightMenuTabs;


function getWidth(){
	if(document.getElementById("menuTabsTable")){ maxWidth = document.getElementById("menuTabsTable").offsetWidth;}
	if(document.getElementById("menuTabs")) { maxWidthMenuTabs = document.getElementById("menuTabs").style.width;	maxWidthMenuTabs = parseInt(maxWidthMenuTabs.replace("px", ""));}
}

function getHeight(){
	if(document.getElementById("menuTabsTableVertical")){ maxHeight = document.getElementById("menuTabsTableVertical").offsetHeight;}
	if(document.getElementById("menuTabsVertical")) { maxHeightMenuTabs = document.getElementById("menuTabsVertical").style.height; maxHeightMenuTabs = parseInt(maxHeightMenuTabs.replace("px", ""));}
}


function startMenu(step)
{
	getWidth();
	var divId = "menuTabs";
	interval_ID = setInterval('moveDiv("'+divId+'",'+step+');',15);
}

function startMenuVertical(step)
{
	getHeight();
  var divId = "menuTabsVertical";
	interval_IDVertical = setInterval('moveDivVertical("'+divId+'",'+step+');',15);
}

function moveDiv(divID,step)
{
 	var the_style = getStyleObject(divID);
	if (the_style)
  {
    var current_left = parseInt(the_style.left);
		var new_left = current_left + step;

  	if(step > 0 && current_left < 0 ){
			if (document.layers) the_style.left = new_left;
			else the_style.left = new_left + "px";
		}
		if(step < 0 && current_left >= (-maxWidth + maxWidthMenuTabs)){
			if (document.layers) the_style.left = new_left;
			else the_style.left = new_left + "px";
		}
  }
}

function moveDivVertical(divID, step)
{
  var the_style = getStyleObject(divID);
	if (the_style)
  {
  	if(the_style.top != "") var current_top = parseInt(the_style.top);
		else var current_top = 0;

		var new_top = current_top + step;
    if(step > 0 && current_top < 0 ){ 
			if (document.layers) the_style.top = new_top;
			else the_style.top = new_top + "px";
		} 
		if(step < 0 && current_top >= (-maxHeight + (maxHeightMenuTabs))){
			if (document.layers) the_style.top = new_top;
			else the_style.top = new_top + "px";
		}
  }
}

function stopMenu(){
	clearInterval(interval_ID);
}

function stopMenuVertical()
{
	clearInterval(interval_IDVertical);
}
