// ----------------------------------------------------------------------------
// 

var RecommendByEmailController = Class.create();

RecommendByEmailController.prototype = {
	
	initialize: function() {
	},
	
	recommendPicture: function(senderName, toEmail, message, pictureNumber) {
		return this._ajaxSendRecommendation(senderName, toEmail, message, 'picture=' + escape(pictureNumber));
	},
	
	recommendArtist: function(senderName, toEmail, message, artistPrefix) {
		return this._ajaxSendRecommendation(senderName, toEmail, message, 'artist=' + escape(artistPrefix));
	},
	
	_ajaxSendRecommendation: function(senderName, toEmail, message, options) {
		var queryStr = ('senderName=' + escape(senderName) + '&toEmail=' + escape(toEmail) + '&message=' + escape(message));
		if (options != null) {
			queryStr = options + '&' + queryStr;
		}
		queryStr = 'cmd=ajaxRecommend&' + queryStr;
		
		var req = new Ajax.Request("/" + globalLPLanguage + "/shop/RecommendByEmail.php", { 
			asynchronous: false,
			parameters: queryStr
		});
		
		return req.success();
	}
	
};

// one global instance
var recommendByEmailController = new RecommendByEmailController();


// ----------------------------------------------------------------------------
// 

var ShoppingListController = Class.create();

ShoppingListController.prototype = {
	
	initialize: function() {
	},
	
	addPicture: function(pictureNumber) {
		return this._ajaxAddToShoppingList(pictureNumber);
	},
	
	addPictureWithNotice: function(pictureNumber, noticeDiv) {
		if (this.addPicture(pictureNumber)) {
			new Effect.BlindDown(noticeDiv, { duration: 0.2, afterFinish: function() {
				new Effect.Highlight(noticeDiv, { duration: 3.0, afterFinish: function() {
					new Effect.BlindUp(noticeDiv, { duration: 0.2 })
			} }) } });
		}
	},
	
	_ajaxAddToShoppingList: function(pictureNumber, options) {
		var queryStr = ('picture=' + escape(pictureNumber));
		if (options != null) {
			queryStr = options + '&' + queryStr;
		}
		queryStr = 'cmd=ajaxAddToShoppingList&' + queryStr;
		
		var req = new Ajax.Request("/" + globalLPLanguage + "/shop/ManageShoppingList.php", { 
			asynchronous: false,
			parameters: queryStr
		});
		
		return req.success();
	}
	
};

// one global instance
var shoppingListController = new ShoppingListController();


// ----------------------------------------------------------------------------
// 

function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return null;
}


// ----------------------------------------------------------------------------
// 

function fillSelectFromArray(selectCtrl, itemArray, goodPrompt, badPrompt, defaultItem) {
	var i, j;
	var prompt;
	
	// empty existing items
	for (i = selectCtrl.options.length; i >= 0; i--) {
		selectCtrl.options[i] = null; 
	}
	prompt = (itemArray != null) ? goodPrompt : badPrompt;
	if (prompt == null) {
		j = 0;
	} else {
		selectCtrl.options[0] = new Option(prompt);
		j = 1;
	}
	if (itemArray != null) {
		// add new items
		for (i = 0; i < itemArray.length; i++) {
			selectCtrl.options[j] = new Option(itemArray[i][0]);
			if (itemArray[i][1] != null) {
				selectCtrl.options[j].value = itemArray[i][1]; 
			}
			j++;
		}
		
		// select first item (prompt) for sub list
		selectCtrl.options[0].selected = true;
	}
}


// ----------------------------------------------------------------------------
// 

function focusPictureAfterLoad() {
	// if there's a fragment in the url, use it to start the lightbox
	var pictureNumber = location.hash;
	if (pictureNumber != null && pictureNumber.length > 0) {
		pictureNumber = pictureNumber.substring(1);
		var link = $('picture_link_' + pictureNumber);
		if (link != null) {
			link.onclick();
		}
	}
}
Event.observe(window, 'load', function() { focusPictureAfterLoad() });


