var relloc = '/';

switch_on = function(theelement) {
	theelement.style.backgroundImage="url(images/white-grad.jpg)";
}

switch_off = function(theelement) {
	theelement.style.background='transparent';
}

highlight_form = function(theelement) {
	new Effect.Highlight(theelement, { startcolor:'#e0d1e8',endcolor:'#ffffff' }); 
	return false;
}

eraseSearchBox = function(id,defaultvalue) {
	if($(id)) {
		if($(id).value == "") {
			$(id).value = defaultvalue;
		} else if ($(id).value == defaultvalue) {
			$(id).value = "";
		}
	}
}

// Header navigation controls

var boolCollectionCategoryFadeInProgress = false;

collectioncategorytabon = function(categoryid) {
	$('category' + categoryid + 'tab').removeClassName('top-nav-off');
	$('category' + categoryid + 'tab').addClassName('top-nav-on');
}

collectioncategorytaboff = function(categoryid) {
	$('category' + categoryid + 'tab').removeClassName('top-nav-on');
	$('category' + categoryid + 'tab').addClassName('top-nav-off');
}

collectioncategory = function(categoryid) {

	if(!boolCollectionCategoryFadeInProgress) {
		boolCollectionCategoryFadeInProgress = true;
		if(collectioncategorycurrent!=0) {
			collectioncategorytaboff(collectioncategorycurrent);
			collectioncategorytabon(categoryid);
			if($('category' + collectioncategorycurrent)) {			
				new Effect.Fade($('category' + collectioncategorycurrent), {
					duration: 0.5,
					afterFinish: function() {				
						new Effect.Appear($('category' + categoryid), {
							duration: 0.5,
							afterFinish: function() {
								boolCollectionCategoryFadeInProgress = false;
							}
						});				
					}			
				}); 
			}	
			else if($('category' + categoryid)) {		
				new Effect.Appear($('category' + categoryid), {
					duration: 0.5,
					afterFinish: function() {
						boolCollectionCategoryFadeInProgress = false;
					}
				});				
			}			
		} else {
			collectioncategorytabon(categoryid);
			if($('category' + categoryid)) {
				new Effect.Appear($('category' + categoryid), {
					duration: 0.5,
					afterFinish: function() {
						boolCollectionCategoryFadeInProgress = false;
					}
				});		
			}
		}	
		
		collectioncategorycurrent = categoryid;
	}

}

searchlinkon = function(id) {
	for(i=1;i<6;i++) {
		if($('pagelink' + i)) {
			$('pagelink' + i).removeClassName('category-link-on');
		}
	}
	$('pagelink' + id).addClassName('category-link-on');
}

// Floating DIV functions

centerPopup = function(divid) {

	var eltDims = $(divid).getDimensions();
	var browserDims = $(document.body).getDimensions(); 

	//var y  = (browserDims.height - eltDims.height) / 2;
	var y = 20;
	var x = (browserDims.width - eltDims.width) / 2;

	var styles = { position : 'absolute',
	top      : y + 'px',
	left     : x + 'px' };

	$(divid).setStyle(styles);
	$('parentcontainer').scrollTo();

}

unfadeBackground = function() {
	new Effect.Opacity($('parentcontainer'), { 
		from: 0.3, 
		to: 1.0, 
		duration: 0.5 
	});
} 

fadeBackground = function() {
	new Effect.Opacity($('parentcontainer'), { 
		from: 1.0, 
		to: 0.3, 
		duration: 0.5 
	});
}

triggerPopup = function(divid) {
	fadeBackground();
	centerPopup(divid);
	new Effect.Appear($(divid), {
		afterFinish: function() {
			//$(divid).scrollTo();
		}
	});
	return false;
}

closePopup = function(divid) {
	new Effect.Fade($(divid));
	unfadeBackground();
	return false;
}

// Add/Remove favourites
collectionStringSingular = function(collection_id) {
	var strtype = 'Wedding Dress';
	if(collection_id == 7 || collection_id == 8 || collection_id == 9) {
		strtype = "Dress";
	} else if(collection_id == 13) {	
		strtype = "Evening Wear Dress";		
	} else if(collection_id == 10 || collection_id == 11 || collection_id == 12) {	
		strtype = "Bridal Wear Dress";		
	} else if(collection_id == 16) {
		strtype = "Prom Dress";		
	} 
	return strtype;
}

collectionStringPlural = function(collection_id) {
	var strtype = 'Wedding Dresses';
	if(collection_id == 7 || collection_id == 8 || collection_id == 9) {
		strtype = "Dresses";
	} else if(collection_id == 13) {	
		strtype = "Evening Wear Dresses";		
	} else if(collection_id == 10 || collection_id == 11 || collection_id == 12) {	
		strtype = "Bridal Wear Dresses";		
	} else if(collection_id == 16) {
		strtype = "Prom Dresses";		
	} 
	return strtype;
}

addFave = function(dress_id,dress,collection_id) {
	var strtype = collectionStringPlural(collection_id).toLowerCase();
	new Ajax.Request(relloc + '_ajax/ajax_faves_add.php?dress_id=' + dress_id, {
		onSuccess: function() {
			var id = 'fave' + dress_id;
			$(id).innerHTML = '<img onclick="remFave(\'' + dress_id + '\',\'' + dress + '\',\'' + collection_id + '\');" src="' + relloc + 'images/dress-unfavourite.png" width="20" height="18" alt="Remove ' + dress + ' from your favourite ' + strtype + '" title="Remove ' + dress + ' from your favourite ' + strtype + '" \/>';
		}		
	});
}

remFave = function(dress_id,dress,collection_id) {
	var strtype = collectionStringPlural(collection_id).toLowerCase();
	new Ajax.Request(relloc + '_ajax/ajax_faves_remove.php?dress_id=' + dress_id, {
		onSuccess: function() {
			var id = 'fave' + dress_id;	
			$(id).innerHTML = '<img onclick="addFave(\'' + dress_id + '\',\'' + dress + '\',\'' + collection_id + '\');" src="' + relloc + 'images/dress-favourite.png" width="20" height="18" alt="Add ' + dress + ' to your favourite ' + strtype + '" title="Add ' + dress + ' to your favourite ' + strtype + '" \/>';
		}		
	});
}

addFaveDress = function(dress_id,dress,collection_id) {
	//var strtype = collectionStringPlural(collection_id);
	new Ajax.Request(relloc + '_ajax/ajax_faves_add.php?dress_id=' + dress_id, {
		onSuccess: function() {
			var id = 'fave' + dress_id;
			$(id).innerHTML = '<div class="dress-unfavourite"><p><a href="#" onclick="return remFaveDress(\'' + dress_id + '\',\'' + dress + '\',\'' + collection_id + '\');">Remove From Favourites<\/a><\/p><\/div>';
		}		
	});
	return false;
}

remFaveDress = function(dress_id,dress,collection_id) {
	//var strtype = collectionStringPlural(collection_id);
	new Ajax.Request(relloc + '_ajax/ajax_faves_remove.php?dress_id=' + dress_id, {
		onSuccess: function() {
			var id = 'fave' + dress_id;		
			$(id).innerHTML = '<div class="dress-favourite"><p><a href="#" onclick="return addFaveDress(\'' + dress_id + '\',\'' + dress + '\',\'' + collection_id + '\');">Add to Favourite Dresses<\/a><\/p><\/div>';
		}		
	});
	return false;
}

// Trigger Colours popup div
coloursPopup = function(dress_id) {
	new Ajax.Updater($('popup-container-content'), relloc + '_ajax/ajax_colours.php?dress_id=' + dress_id, {
		evalScripts: true,
		onComplete: function() {
			triggerPopup('popup-container');
		}
	});
	return false;
}

// Trigger Contact popup div
contactPopup = function(dress_id) {
	new Ajax.Updater($('popup-container-content'), relloc + '_ajax/ajax_send_a_dress.php?dress_id=' + dress_id, {
		evalScripts: true,
		onComplete: function() {
			triggerPopup('popup-container');
		}
	});
	return false;
}

// Validate a given email address
validateEmail = function(email) {
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(email.match(emailRegEx)){
		return true;
	} else {
		return false;
	}
}

