$(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);
	}

}

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);
	}
}

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_IDVertical;
var maxHeight;
var maxHeightMenuTabs;

var intervals_IDVertical = new Array();
var arrMaxHeight = new Array();
var arrMaxHeightMenuTabs = new Array();

var intervals_ID = new Array();
var arrMaxWidth = new Array();
var arrMaxWidthMenuTabs = new Array();


function getWidth(id){
	if(id){
		if(document.getElementById("menuTabsTable"+id)){ arrMaxWidth[id] = document.getElementById("menuTabsTable"+id).offsetWidth;}
		if(document.getElementById("menuTabs"+id)) { arrMaxWidthMenuTabs[id] = document.getElementById("menuTabs"+id).style.width; arrMaxWidthMenuTabs[id] = parseInt(arrMaxWidthMenuTabs[id].replace("px", ""));	}
	}
	else{
		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(id){
	if(id){
		if(document.getElementById("menuTabsTableVertical"+id)){ arrMaxHeight[id] = document.getElementById("menuTabsTableVertical"+id).offsetHeight;}
		if(document.getElementById("menuTabsVertical"+id)) { arrMaxHeightMenuTabs[id] = document.getElementById("menuTabsVertical"+id).style.height; arrMaxHeightMenuTabs[id] = parseInt(arrMaxHeightMenuTabs[id].replace("px", ""));}
	}
	else{
		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 getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
} // getStyleObject



function startMenu(step, id)
{
	var divId = "menuTabs";
	if(id){
		divId = divId +id;
		intervals_ID[id] = setInterval('moveDiv("'+divId+'",'+step+',\''+id+'\');',15);
	}
	else{
		interval_ID = setInterval('moveDiv("'+divId+'",'+step+');',15);
	}
}

function startMenuVertical(step, id)
{
	var divId = "menuTabsVertical";
	if(id){
		divId = divId +id; 
		intervals_IDVertical[id] = setInterval('moveDivVertical("'+divId+'",'+step+',\''+id+'\');',15);
	}
	else{
		interval_IDVertical = setInterval('moveDivVertical("'+divId+'",'+step+');',15);
	}
}

function moveDiv(divID,step, id)
{
  // get the stylesheet
  //
	if(id){ getWidth(id);}
	else getWidth();
  var the_style = getStyleObject(divID);

  if (the_style)
  {
    // get the current coordinate and add 5
    //
    var current_left = parseInt(the_style.left);
		var new_left = current_left + step;

    // set the left property of the DIV, add px at the
    // end unless this is NN4
    //
		if(step > 0 && current_left < 0 ){
			
			if (document.layers)  
			{
				the_style.left = new_left;
			}
			else 
			{  
				the_style.left = new_left + "px";
			}
		}
		if(id){
			if(step < 0 && current_left >= (-arrMaxWidth[id] + arrMaxWidthMenuTabs[id])){
				if (document.layers) 
				{
					the_style.left = new_left;
				}
				else 
				{  
					the_style.left = new_left + "px";
				}
			}
		}
		else{
			 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, id)
{
  // get the stylesheet
  //
 	if(id){ getHeight(id);}
	else getHeight();
  var the_style = getStyleObject(divID);
	if (the_style)
  {
    // get the current coordinate and add 5
    //
		if(the_style.top != "")
	    var current_top = parseInt(the_style.top);
		else
			var current_top = 0;
		
	var new_top = current_top + step;

    // set the left property of the DIV, add px at the
    // end unless this is NN4
    //
		if(step > 0 && current_top < 0 ){ 
			if (document.layers) 
			{
				the_style.top = new_top;
			}
			else 
			{  
				the_style.top = new_top + "px";
			}
		} 
		if(id){
			if(step < 0 && current_top >= (-arrMaxHeight[id] + (arrMaxHeightMenuTabs[id]))){
				if (document.layers) 
				{
					the_style.top = new_top;
				}
				else 
				{  
					the_style.top = new_top + "px";
				}
			}
		}
		else{
			if(step < 0 && current_top >= (-maxHeight + (maxHeightMenuTabs))){
				if (document.layers) 
				{
					the_style.top = new_top;
				}
				else 
				{  
					the_style.top = new_top + "px";
				}
			}
		}
  }
}

function stopMenu(id){
	if(id) clearInterval(intervals_ID[id]);	
	else clearInterval(interval_ID);
}

function stopMenuVertical(id)
{
	if(id) clearInterval(intervals_IDVertical[id]);
	else clearInterval(interval_IDVertical);
}