/**
 * @author Egloo
 */
var zwsUrl = "/PhotoBook/"

function zwsPostJSON(servletName, params, callback)
{	
	$.post(zwsUrl + servletName,params,function(resp){
		var re = /^[^\*]*\*(.*)\*[^\*]*$/;;
		var data = resp.replace(re, "$1");
		try {
			data = eval("(" + data + ")");
			if(callback){
				if(data.func) {
					callback(data.func, data.data);
				} else {
					callback(data);
				}	
			}			
		}
		catch(err){
			throw err;
			//alert(err);	
		}
	});
}

function zwsGetJSON(servletName, params, callback, path) {		
	$.get((path || zwsUrl) + servletName, params, function(resp) {
		var re = /^[^\*]*\*(.*)\*[^\*]*$/;;
		var data = resp.replace(re, "$1");
		try {
			data = eval("(" + data + ")");
			if(callback){
				if(data.func) {
					callback(data.func, data.data);
				} else {
					callback(data);
				}
			}						
		}
		catch(err){
			throw err;
			//alert(err);	
		}		
	});
}

function findPos(obj) {
	var curleft = 0;
	var curtop = 0;
	
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}  while((obj = obj.offsetParent));
	}
	
	if($.browser.msie) {
		curtop;
	}
	
	if($.browser.safari) {
		curtop;
		--curleft;
	}
	
	return [curleft, curtop];
}

function getReadableDate2(day, month, year, showDaysDiff) {
	showDaysDiff = showDaysDiff || false;
	
	var strMonth = "";
	switch(month) {
		case 1: strMonth = "января"; break;
		case 2: strMonth = "февраля"; break;
		case 3: strMonth = "марта"; break;
		case 4: strMonth = "апреля"; break;
		case 5: strMonth = "мая"; break;
		case 6: strMonth = "июня"; break;
		case 7: strMonth = "июля"; break;
		case 8: strMonth = "августа"; break;
		case 9: strMonth = "сентября"; break;
		case 10: strMonth = "октября"; break;
		case 11: strMonth = "ноября"; break;
		case 12: strMonth = "декабря";
	}
	
	var result = day + " " + strMonth + " " + year;
	if(showDaysDiff) {
		var daysDiff = getDaysDiff(day, month, year);
		var strDaysDiff = " (сегодня)";
		
		if(daysDiff == 1) {
			strDaysDiff = " (вчера)";
		} else if(daysDiff == 2) {
			strDaysDiff = " (позавчера)";
		} else if(daysDiff > 2) {
			strDaysDiff = " (" + daysDiff + " дней назад)"
		}
		result += strDaysDiff; 
	}
	
	return result;
}

function getReadableDate(gmtDate, showDaysDiff) {
	if(gmtDate) {
		
		var re = /^([0-9]{4})-([0-9]{2})-([0-9]{2})T.*$/;
		var year = gmtDate.replace(re, "$1");
		var month = gmtDate.replace(re, "$2");
		var day = gmtDate.replace(re, "$3");
		
		showDaysDiff = showDaysDiff || false;
		
		var strMonth = "";
		switch(month) {
			case "01": strMonth = "января"; break;
			case "02": strMonth = "февраля"; break;
			case "03": strMonth = "марта"; break;
			case "04": strMonth = "апреля"; break;
			case "05": strMonth = "мая"; break;
			case "06": strMonth = "июня"; break;
			case "07": strMonth = "июля"; break;
			case "08": strMonth = "августа"; break;
			case "09": strMonth = "сентября"; break;
			case "10": strMonth = "октября"; break;
			case "11": strMonth = "ноября"; break;
			case "12": strMonth = "декабря";
		}
		
		var result = day + " " + strMonth + " " + year;
		if(showDaysDiff) {
			var daysDiff = getDaysDiff(day, month, year);
			var strDaysDiff = " (сегодня)";
			
			if(daysDiff == 1) {
				strDaysDiff = " (вчера)";
			} else if(daysDiff == 2) {
				strDaysDiff = " (позавчера)";
			} else if(daysDiff > 2) {
				strDaysDiff = " (" + daysDiff + " дней назад)"
			}
			result += strDaysDiff; 
		}
	} else {
		result = gmtDate;
	}
	
	return result;
}

function getDaysDiff(day, month, year) {
	var today = new Date();
	var itemDate = new Date(year, month - 1, day);
	
	// One day in mills
	var one_day = 1000*60*60*24;
	//Calculate difference btw the two dates, convert to days and return
	return Math.floor( (today.getTime() - itemDate.getTime()) / one_day );
}

function buildJsonpRequest(pars, isDontCallback) {
	isDontCallback = isDontCallback || false;
	
	var result = "";
	for(key in pars) {
        result += key + "=" + pars[key] + "&";
    }
	if(!isDontCallback) result += "callback=?"
	result = result.replace(/&$/, "");
	
	return result;
}

function ascCommentsSortHandle(a, b){
	var x = a;
	var y = b;
	return ((String(x.commentId) < String(y.commentId)) ? -1 : ((String(x.commentId) > String(y.commentId)) ? 1 : 0));
}

function descCommentsSortHandle(a, b){
	var x = a;
	var y = b;
	return ((String(x.commentId) > String(y.commentId)) ? -1 : ((String(x.commentId) < String(y.commentId)) ? 1 : 0));
}

function ascOrdersSortHandle(a, b){
	var x = a;
	var y = b;
	return ((String(x.orderId) < String(y.orderId)) ? -1 : ((String(x.orderId) > String(y.orderId)) ? 1 : 0));
}

function descOrdersSortHandle(a, b){
	var x = a;
	var y = b;
	return ((String(x.orderId) > String(y.orderId)) ? -1 : ((String(x.orderId) < String(y.orderId)) ? 1 : 0));
}

function ascPhotosSortHandle(a, b){
	var x = a;
	var y = b;
	return ((String(x.photoId) < String(y.photoId)) ? -1 : ((String(x.photoId) > String(y.photoId)) ? 1 : 0));
}
