function initShop()
{
	initBuyForms();
	initAsyncBasket();
	initAlert();
	initQuestion();
	initIndex();
	if($('smallSearchForm'))initFormFields("smallSearchForm");
	if($('smallLoginForm'))initFormFields("smallLoginForm"); 
	if($('smallNewsletterForm'))initFormFields('smallNewsletterForm');
}
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();

var ajaxCategories = new Array();

function showSubCategories(parentId, level){
	var divElement = $('categoriesParent' + parentId);
	//if(divElement.className == 'categoriesParentClose'){
		var imageLoading = "<img src='" + shop.templatePath + "/images/smallLoading.gif'>";
		document.getElementById('categoriesTreeLoadingParent'+parentId).className = "categoriesTreeLoadingParentOpen";
		document.getElementById('categoriesTreeLoadingParent'+parentId).innerHTML = imageLoading;
		// alert(document.getElementById('categoriesTreeLoadingParent'+parentId).innerHTML)
		hideSubCategories();
		cancelAjaxCategories();
		//clearTimeout(timeoutCategories);

		//timeoutCategories[parentId] = setTimeout(function(){ajaxLoad(shop.url + '/ajaxTemplates/common/categoriesContent.cfm?categoryId='+parentId+'&deepLevel=1&level='+level, divElement, 'actionShowSubCategories('+parentId+');hideLoadingParent('+parentId+')', null)}, 0);
		
		ajaxCategories[ajaxCategories.length+1] = ajaxLoad(shop.url + '/ajaxTemplates/common/categoriesContent.cfm?categoryId='+parentId+'&deepLevel=1&level='+level, divElement, 'actionShowSubCategories('+parentId+');hideLoadingParent('+parentId+')', null);
		
//	}
	//else{
//		hideSubCategoriesByParentId(parentId);
//	}
}

function cancelAjaxCategories(){
	for(var i=0; i<ajaxCategories.length;i++){
		if(ajaxCategories[i] != undefined) ajaxCancel(ajaxCategories[i]);
		delete ajaxCategories[i];
	}
	
}

function actionShowSubCategories(parentId){
	var divElement = $('categoriesParent' + parentId);
	divElement.className = 'categoriesParentOpen';
	topPosition = $('containerLeft').offsetTop - $('categoriesParent' + parentId).offsetTop;
	if($('subCategoriesTreeMain'+parentId)) $('subCategoriesTreeMain'+parentId).style.top = (topPosition + 10) + 'px';
	divElement.style.display = '';
}

function hideSubCategoriesByParentId(parentId){
	var divElement = document.getElementById('categoriesParent' + parentId);
	divElement.innerHTML = "";
	divElement.className = "categoriesParentClose";
}

function hideLoadingParent(parentId){
	document.getElementById('categoriesTreeLoadingParent'+parentId).innerHTML = "";
	document.getElementById('categoriesTreeLoadingParent'+parentId).className = "categoriesTreeLoadingParent";
}

function hideSubCategories(){
	//$$('.categoriesTreeLoadingParentOpen').each(function(el){el.className = 'categoriesTreeLoadingParent'; el.innerHTML = "";});
	$$('.categoriesParentOpen').each(function(el){el.className = 'categoriesParentClose'; el.innerHTML = "";});
}


function hideSubCategoriesAll(){
	$$('.categoriesParentOpen').each(function(el){el.className = 'categoriesParentClose'; el.innerHTML = "";});
}

/*function initIndex(){
	if($('bannersIndex')){
		var V2 = new viewer($$('#bannersIndexBox img)'),{
			mode: 'alpha',
			interval: 4000
		});
		V2.play(true);
		//$(window).addEvent('domready', V2.play.bind(V2,[false]));
	}
}
*/

function initIndex(){
	if($('productsIndex')){
		var V2 = new viewer($$('#productsIndexBox div.productIndex)'),{
			mode: 'alpha',
			interval: 4000
		});
		V2.play(true);
		$(window).addEvent('domready', V2.play.bind(V2,[false]));
	}
}



function validateAddProduct(req){
		validateAddProductShowMessage(req);
}

function showPriceByOption(form, option){
	showPriceByOptionWithImage(form, option);
}

function showArrowOption(objId){
	var obj = $(objId);
	$$('.productOptionValueTextSelected').each(function(el){el.removeClass("productOptionValueTextSelected");
		el.addClass("productOptionValueText");
	});
	
	obj.removeClass("productOptionValueText");
	obj.addClass("productOptionValueTextSelected");
}

function showArrowOptionGroup(objId){
	var obj = $(objId);

	if(obj.hasClass("productOptionValueText")){
		obj.removeClass("productOptionValueText");
		obj.addClass("productOptionValueTextSelected");
	}else{
		obj.removeClass("productOptionValueTextSelected");
		obj.addClass("productOptionValueText");
	}
}

function initProduct(){
	if(document.getElementById('productInfoBuyForm')){
		initFormFields('productInfoBuyForm');
	}
}

function checkUserType(type){
	if(type == 1){
		userFieldsRequired = 1;
		userFieldsDisplay = '';
		companyFieldsRequired = 0;
		companyFieldsDisplay = 'none';
	}
	else if(type == 2){
		userFieldsRequired = 0;
		userFieldsDisplay = 'none';
		companyFieldsRequired = 1;
		companyFieldsDisplay = '';
	}
	else if(type == 3){
		userFieldsRequired = 1;
		userFieldsDisplay = '';
		companyFieldsRequired = 1;
		companyFieldsDisplay = '';
	}
	
	document.getElementById('userInformationFirstName').style.display = userFieldsDisplay;
	if(document.getElementById('userInformationLastName'))document.getElementById('userInformationLastName').style.display = userFieldsDisplay;
	if(document.getElementById('userInformationLastName1'))document.getElementById('userInformationLastName1').style.display = userFieldsDisplay;
	if(document.getElementById('userInformationLastName2'))document.getElementById('userInformationLastName2').style.display = userFieldsDisplay;
	document.getElementById('userInformationNif').style.display = userFieldsDisplay;
	document.getElementById('userInformationCompany').style.display = companyFieldsDisplay;
	document.getElementById('userInformationVat').style.display = companyFieldsDisplay;
	
	document.getElementById('userInformationFirstNameField').setAttribute('requiredField', userFieldsRequired);
	if(document.getElementById('userInformationLastNameField'))document.getElementById('userInformationLastNameField').setAttribute('requiredField', userFieldsRequired);
	if(document.getElementById('userInformationLastName1Field'))document.getElementById('userInformationLastName1Field').setAttribute('requiredField', userFieldsRequired);
	if(document.getElementById('userInformationLastName2Field'))document.getElementById('userInformationLastName2Field').setAttribute('requiredField', userFieldsRequired);
	document.getElementById('userInformationNifField').setAttribute('requiredField', userFieldsRequired);
	document.getElementById('userInformationCompanyField').setAttribute('requiredField', companyFieldsRequired);
	document.getElementById('userInformationVatField').setAttribute('requiredField', companyFieldsRequired);
	
}


function requireSignIn(value){	
	document.getElementById('userInformationPasswordField').setAttribute('requiredField', value);	
	document.getElementById('userInformationConfirmPasswordField').setAttribute('requiredField', value);	
	//document.getElementById('userCheckoutType').value = value;		
	if(value == 0){
		document.getElementById('userInformationSubscribed').style.display = 'none';
		document.getElementById('userInformationPasswords').style.display = 'none';
	}	
	if(value == 1){		
		document.getElementById('userInformationSubscribed').style.display = '';		
		document.getElementById('userInformationPasswords').style.display = '';
	}	
}

function requireShipTo(value){	

	if(document.getElementById('userInformationFirstNameShippingField'))document.getElementById('userInformationFirstNameShippingField').setAttribute('requiredField', value);	
	if(document.getElementById('userInformationLastName1ShippingField'))document.getElementById('userInformationLastName1ShippingField').setAttribute('requiredField', value);	
	if(document.getElementById('userInformationLastName2ShippingField'))document.getElementById('userInformationLastName2ShippingField').setAttribute('requiredField', value);	
	if(document.getElementById('userInformationLastNameShippingField'))document.getElementById('userInformationLastNameShippingField').setAttribute('requiredField', value);	
	if(document.getElementById('userInformationNifShippingField'))document.getElementById('userInformationNifShippingField').setAttribute('requiredField', value);	
	if(document.getElementById('userInformationAddressShippingField'))document.getElementById('userInformationAddressShippingField').setAttribute('requiredField', value);
	if(document.getElementById('userInformationAddress2ShippingField'))document.getElementById('userInformationAddress2ShippingField').setAttribute('requiredField', value);
	if(document.getElementById('userInformationNumberShippingField'))document.getElementById('userInformationNumberShippingField').setAttribute('requiredField', value);
	if(document.getElementById('userInformationCityShippingField'))document.getElementById('userInformationCityShippingField').setAttribute('requiredField', value);
	if(document.getElementById('userInformationStateShippingField'))document.getElementById('userInformationStateShippingField').setAttribute('requiredField', value);
	if(document.getElementById('userInformationZipShippingField'))document.getElementById('userInformationZipShippingField').setAttribute('requiredField', value);
	if(document.getElementById('userInformationCountryShippingField'))document.getElementById('userInformationCountryShippingField').setAttribute('requiredField', value);


	if(value == 1){
		display = '';
	}
	else{
		display = 'none';
	}
	
	if(document.getElementById('userInformationShipTo'))document.getElementById('userInformationShipTo').style.display = display;
	else if(document.getElementById('userInformationControlPanelShipTo'))document.getElementById('userInformationControlPanelShipTo').style.display = display;
	
	//reloadCountries(value);
	
}

function initWishlistTable(){$('informationBox').style.visibility = 'visible';	initFormFields("formWishlistToEmail");}
function initWishlistToEmail(obj){	$('informationBox').style.visibility = 'visible';	initFormFields("formWishlistToEmail");}

function selectAllItemsWishlist(){
	var value = document.getElementById('optionSelect').value;
	if(value == '1') newValue = '2';
	else newValue = '1';
	
	document.getElementById('optionSelect').value =  newValue;
	$$('.userControlPanelWishlistField').each(
																						function(el){
																							if(newValue == '1') el.checked = true;
																							else el.checked = false;
																						})
}

function reloadWishlist(){
	loadAjaxFile(shop.url +'/ajaxTemplates/users/wishlist.cfm?getTemplates=0', 'userControlPanelInfo', 'initWishlistTable()', null);
}

function reloadBasket(){
	loadAjaxFile(shop.url+'/ajaxTemplates/basket/basket.cfm','basketResume', null,null);
}

function initFormsUser(){
	if(document.getElementById('formLogin'))initFormFields('formLogin');
	if(document.getElementById('formUserInformation')){
		initFormFields('formUserInformation');
		onlybuyCheck = $('userInformationCheckoutTypeField');
		checkLogin(onlybuyCheck);
	}
	restartShadowbox();
}

function hideZipResult(shipTo){
	if(shipTo == 1) divId = "userInformationZipShipping_Result";
	else divId = "userInformationZip_Result";

	document.getElementById(divId).style.display='none';
}

function valueZipResult(zip, shipTo){
	if(shipTo == 1) divId = "userInformationZipShipping_Result";
	else divId = "userInformationZip_Result";
	
	document.getElementById(divId).innerHTML = "";
	if(zip == "") document.getElementById(divId).style.display='none';
	else document.getElementById(divId).style.display='';
	
	ajaxLoad(shop.url+'/ajaxTemplates/users/postalCodes.cfm?zip='+zip+'&country='+countryId+'&shipTo='+shipTo,$(divId),null);

}

function valuesStateAndCity(postalCodeId, valueState, valueCity, shipTo){
	if(shipTo == 1) divId = "userInformationPostalCodeIdShippingField";
	else divId = "userInformationPostalCodeIdField";	
	document.getElementById(divId).value = postalCodeId;

	if(shipTo == 1) divId = "userInformationStateShippingField";
	else divId = "userInformationStateField";	
	document.getElementById(divId).value = valueState;
	
	if(shipTo == 1) divId = "userInformationCityShippingField";
	else divId = "userInformationCityField";	
	document.getElementById(divId).value = valueCity;	
}

function valueZip(valueZip, shipTo){
	if(shipTo == 1) divId = "userInformationZipShippingField";
	else divId = "userInformationZipField";	

	document.getElementById(divId).value = valueZip;
}

function showMiniBasket(){
	hideAsyncBasket();
	if(!$('basket')){
		if(shop.asyncBasketBox && (shop.asyncBasketBox.style.visibility == "hidden" || shop.asyncBasketBox.style.visibility == "")){
			shop.asyncBasketBox.transition.start('opacity',0,1);
			basketTime = setTimeout("hideAsyncBasketInShow()", 2000);
		}
	}
}


function reloadMiniBasket(){
	var loadNext = "ajaxLoad('"+shop.url+"/ajaxTemplates/basket/miniBasket.cfm', $('miniBasket'), 'initAsyncBasket();showMiniBasket();');";
	ajaxLoad(shop.url+ '/ajaxTemplates/basket/asyncBasket.cfm', $('asyncBasketBox'), loadNext);
}

function submitUserInformationStepBasket(){
	submitUserInformationForm('formUserInformation');
}

function reloadSmallLogin(){
	ajaxLoad(shop.url+ '/ajaxTemplates/common/smallLogin.cfm', $('smallLogin'));
}

function loadProductRelatedPageInfo(obj, objInfo){
	var clickClass = obj.getAttribute('clickClass');
	var obj = $(obj.id);
	alternateClass(obj, clickClass);
	
	var children = obj.getElementsByTagName('div');
	var objId = children[0].id;
	var obj = $(objId);
	var clickClass = obj.getAttribute('clickClass');
	alternateClass(obj, clickClass);

	var obj = $(objInfo);
	var clickClass = obj.getAttribute('clickClass');
	alternateClass(obj, clickClass);
}

function alternateClass(obj, nameClass){
	$$('.'+nameClass).each(function(el){el.removeClass(nameClass);});
	if(obj.hasClass(nameClass)) obj.removeClass(nameClass);
	else obj.addClass(nameClass);
}

function reloadStock(stock){}

function restartUserFormInformation(){
	initFormFields('formUserInformation');
}

$(window).addEvent('domready', function(){initShop(); initProduct();});
//$(window).addEvent('click', function(){hideSubCategories()});



function getWidthScroll(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 getHeightScroll(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 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
  //
 //alert(-maxHeight + maxHeightMenuTabs);
	if(id){ getHeightScroll(id);}
	else getHeightScroll();
  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);
}
