var galleryEasing = {
	init : function (display ,images ,all ,bigSuffix ,smallSuffix) {
		this.stack   = jQ(images).hover(this.hoverEvent,function(){}).click(this.hrefClick);
		this.display = jQ(display);
		this.bigSuffix   = bigSuffix;
		this.smallSuffix = smallSuffix;
		this.startTimer();
		that = this;
		this.autoChange();
	} ,
	
	current : 0, 
	display : {},
	stack : {} ,
	bigsizesuffix : '' ,
	
	timer 	: null,
	currentTimeout : null,
	haltTimer : function () {
		clearInterval(this.timer);
		this.timer = null
		if(this.currentTimeout != null) {
			clearTimeout(this.currentTimeout);
			this.currentTimeout = null
		}
		this.currentTimeout = setTimeout(function () {
			galleryEasing.startTimer.call(that);
		},30000);
	},
	
	startTimer : function () {
		if(this.timer != null) {
			clearInterval(this.timer);
			this.timer = null
		}
		
		this.timer = setInterval(function(){
			galleryEasing.autoChange.call(that);
		},5000);
	},
	
	hoverEvent : function () {
		galleryEasing.haltTimer();
		galleryEasing.changeDisplay(this);
	},
	/*clickEvent : function () {
		galleryEasing.haltTimer();
		galleryEasing.changeDisplay(this);
		jQ(this).unbind('click').bind('click' ,galleryEasing.hrefClick);
	} ,*/
	
	autoChange : function () {
		if(galleryEasing.current >= (galleryEasing.stack.length-1)) {
			galleryEasing.current = 0;
		} else  {
		 	galleryEasing.current++;
		}

		galleryEasing.changeDisplay(galleryEasing.stack[galleryEasing.current]);

	} ,
	
	changeDisplay : function (currentImage) {
		currentImage = jQ(currentImage);
		var head1 ,head2 ,head3 ,text ,src ,href;
		var raw = jQ(currentImage).attr('alt').split(',');
		head1 = raw[0];
		head2 = raw[1];
		head3 = raw[2];
		text  = raw[3];
		src   = this.thumbToBig(currentImage.attr('src'));
		href  = currentImage.parent().attr('href');
		this.display.css('background-image' ,'url("'+src+'")');
		this.display.find('.head1').html(head1);
		this.display.find('.head2').html(head2);
		this.display.find('.head3').html(head3);
		this.display.find('#text').html(text);
		this.display.unbind('click').bind('click' ,function () {
			window.location = href;
		});
	},
	
	hrefClick : function () {
		window.location = jQ(this).parent().attr('href');
	},
	
	thumbToBig : function (src) {
		var tmpStr = src.split(this.smallSuffix);	
		tmpStr     = tmpStr[0]+this.bigSuffix+tmpStr[1];
		return tmpStr.concat();
	} ,
	
	bigToThumb : function (src) {
		var tmpStr = src.split(this.bigSuffix);	
		tmpStr     = tmpStr[0]+this.bigSuffix+tmpStr[1];
		return tmpStr.concat();
	}
}


/** send to friend **/

var sendMailParams = null;
function initSendToFriendDialog(memberId, memberName, section, param1, param2) {
	sendMailParams = new Array();
	sendMailParams = {
			'memberId': memberId,
			'memberName' : memberName,
			'section' : section,
			'param1' : param1,
			'param2' : param2
		};
}

function openSendToFriendDialog() {
	var sendMailHtml = "<table><tr><td align='left'>מאת:</td><td align='left'>"+
"<input type='text' id='senderName' style='width:300px' value=\""+sendMailParams['memberName']+"\" /></td></tr>"+
"<tr><td align='left'>נמען<br />(דוא&quot;ל):</td><td align='left'>"+
"<input type='text' id='targetMail' style='width:300px' /></td></tr>"+
"<tr><td valign='top' align='left'>הודעה קצרה:</td><td align='left'>"+
"<textarea id='mailMessage' style='width:300px;height:60px;overflow:hidden;'></textarea></td></tr>"+
"</table><div id='sendMailErrorBox' align='center' style='color:red'></div>";
	MessageBox.confirm(sendMailHtml, sendToFriend, sendMailParams);
	// reset the target value before opening
	// (more comfortable if user wants to send to more than one friend)
	document.getElementById('targetMail').value = '';
	document.getElementById('sendMailErrorBox').innerHTML = '';	
}

function openSendToPhone(imgPath)
{
	imgPath = "http://www.shin1.co.il"+imgPath;
    // path to your config file
    var configUri = "http://www.shin1.co.il/mobilin/config.xml";
    var iCountry = "48";
	var sUrl = "http://www.moblin.com/CellPic/Default.aspx?config="+configUri+"&image="+ escape(imgPath) +"&country="+iCountry
    window.open(sUrl,"cellpic","menubar=0,width=571,height=320");
}


function sendToFriend() {
	var senderName = trim(document.getElementById('senderName').value);
	if (senderName.length == 0)
	{
		document.getElementById('sendMailErrorBox').innerHTML = MISSING_NAME;
		return false;
	}
	var targetMail = trim(document.getElementById('targetMail').value);
	if (targetMail.length == 0)
	{
		document.getElementById('sendMailErrorBox').innerHTML = MISSING_TARGET_EMAIL;
		return false;
	}
	var message = trim(document.getElementById('mailMessage').value);
	var POSTInfo = 'act=sendtofriend&member_id='+sendMailParams['memberId']+"&section="+sendMailParams['section']+"&param2="+sendMailParams['param2']
		+'&param1='+sendMailParams['param1']+'&target_mail='+targetMail+"&sender_name="+senderName+"&message="+message;
	var senderMail = document.getElementById('senderMail');
	if (senderMail != null) POSTInfo += "&sender_mail="+senderMail.value;
	if (typeof xhr != 'object') xhr = new XHR();
	xhr.Post("/srv/events/common.php",POSTInfo,sendToFriendResult);
}

function sendToFriendResult(responseText) {
	MessageBox.Alert(responseText);
}

function delete_pic() {
	MessageBox.confirm("האם את/ה בטוח/ה שברצונך למחוק את התמונה?", delete_pic_process, sendMailParams);
}

function delete_pic_process() {
	var POSTInfo = 'act=delete_pic&album_id='+sendMailParams['param1']+'&pic_id='+sendMailParams['param2'];
	if (!xhr) xhr = new XHR();
	xhr.Post("/srv/events/common.php",POSTInfo,delete_pic_result);
}

function delete_pic_result(responseText) {
	if (responseText=='success')
		top.location.href='/events.php';//?eid='+sendMailParams['param1']+'a='
	else
		MessageBox.Alert(responseText);
}


function WeekdaysEvents() {
	this.request = new XHR(3000);
	this.serverScript = '/srv/events/weekevents.php';
	this.listCache = new Array(7); //cache weekday's events
	this.listContainer = $('weekeventslist');
	
	// used as __constructor
	this.init = function() {
		this.initListCache();
	}
	
	/** initialize caching **/
	this.initListCache = function() {
		for (var i in this.listCache)
			this.listCache[i] = null;
	}
	
	/** show result in page **/
	this.implementResult = function(obj, val) {
		obj.innerHTML = val;
	}
	
	/** list weekday events **/
	this.list = function(weekday) {
		if (this.listCache[weekday]!=null) {
			this.implementResult(this.listContainer, this.listCache[weekday]);
			return;
		}
		
		var self = this; //scope issues
		var postInfo = 'get=weekeventslist&weekday='+weekday;
		this.request.Post(this.serverScript, postInfo, 
			function(responseText, weekday) { // needed for scope issues
				self.onListResult.call(self, responseText, weekday); 
			}, 
			weekday);
	}
	
	this.onListResult = function(responseText, weekday) {
		this.implementResult(this.listContainer, responseText)
		
		//cache
		this.listCache[weekday] = responseText;
	}
} 
