String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function isEmail(s) {
	if(s=="") 
		return false;
	if(s.indexOf(" ")>0)
		return false;
	if(s.indexOf("@")==-1) 
		return false;
	var i=1; var sLength=s.length;
	if(s.indexOf(".")==-1) 
		return false;
	if(s.indexOf("..")!=-1) 
		return false;
	if(s.indexOf("@")!=s.lastIndexOf("@")) 
		return false;
	if(s.lastIndexOf(".")==s.length-1)
		return false;
	var str="abcdefghikjlmnopqrstuvwxyz-@._0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	for(var j=0;j<s.length;j++)
		if(str.indexOf(s.charAt(j))==-1)
			return false;
	return true;
}
		
function goPicture(step) {
	current_photo_id = current_photo_id + step;
	eval($('picture_id_' + current_photo_id).getProperties('onclick').onclick
			.replace('return', ''));

	check_btn_status();

	return false;
}

function check_btn_status() {
	if (current_photo_id == 1) {
		$('photo_back_btn').style.display = 'none';
	} else {
		$('photo_back_btn').style.display = '';
	}

	if (current_photo_id == max_photo) {
		$('photo_next_btn').style.display = 'none';
	} else {
		$('photo_next_btn').style.display = '';
	}
}

function photoDetailchange(link, w, h, title) {
	$('photo_detail_link').src = CLUSTER_HOST + 'public/images/loadinggrey.gif';
	if ($('photo_detail_desc'))
		$('photo_detail_desc').innerHTML = '';

	id = link.replace(CLUSTER_HOST + 'public/upload/album_picture/large/', '');
	id = link.replace('public/upload/album_picture/large/', '');

	id = id.replace('.jpg', '');

	margin_top = Math.round((450 - h) / 2);

	new Asset.image(link, {
		onload : function() {
			$('photo_detail_link').setStyle('margin-top', margin_top);
			$('photo_detail_link').src = link;
			$('photo_detail_desc').innerHTML = title;
			$('td_picture_id').innerHTML = '#&nbsp;' + id;
		}
	});

	if (current_photo_id == 1) {
		$('photo_back_btn').style.display = 'none';
	} else {
		$('photo_back_btn').style.display = '';
	}

	if (current_photo_id == max_photo) {
		$('photo_next_btn').style.display = 'none';
	} else {
		$('photo_next_btn').style.display = '';
	}

	if (window.getScrollTop() > 330) {
		scroll.toElement('photo_detail_wrapper');
	}

	return false;
}

function changePhotoDetail(link, title) {

	$('photo_detail_link').width = 300;
	$('photo_detail_link').height = 300;

	$('photo_detail_link').src = CLUSTER_HOST + 'public/images/loading.gif';

	new Asset.image(link, {
		onload : function() {
			$('photo_detail_link').width = 300;
			$('photo_detail_link').height = 300;
			$('photo_detail_link').src = link;
			if ($('photo_detail_title')) {
				$('photo_detail_title').innerHTML = title;
			}
		}
	});

	return false;
}

function drinkRefresh() {
	var food_served = $('food_served').checked ? 1 : 0;
	var dance_floor = $('dance_floor').checked ? 1 : 0;
	window.location.href = 'index.php?action=drink&food_served=' + food_served
			+ "&dance_floor=" + dance_floor;
	return false;
}

function commentSubmit() {

	if ($('comment_name').value == '') {
		alert(REQUIRE_COMMENT_NAME);
		$('comment_name').focus();
		return false;
	}

	if ($('comment_email').value == '') {
		alert(REQUIRE_COMMENT_EMAIL);
		$('comment_email').focus();
		return false;
	}

	var emailregexp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!emailregexp.test($('comment_email').value)) {
		alert(PLZ_ENTER_CORRECT_EMAIL);
		$('comment_email').focus();
		return false;
	}

	if ($('comment_text').value == '') {
		alert(PLZ_ENTER_COMMENT_TEXT);
		$('comment_text').focus();
		return false;
	}

	$('form_comment').submit();
	return false;

}

function eatRefresh() {
	var chkChinese = $('chkChinese').checked ? 1 : 0;
	var chkJapanese = $('chkJapanese').checked ? 1 : 0;
	var chkVietnamese = $('chkVietnamese').checked ? 1 : 0;
	var chkKorean = $('chkKorean').checked ? 1 : 0;
	var chkThai = $('chkThai').checked ? 1 : 0;
	var chkIndian = $('chkIndian').checked ? 1 : 0;
	var chkItalian = $('chkItalian').checked ? 1 : 0;
	var chkFrench = $('chkFrench').checked ? 1 : 0;
	var chkSpanish = $('chkSpanish').checked ? 1 : 0;
	var chkInternational = $('chkInternational').checked ? 1 : 0;

	var Budget = $('Budget').checked ? 1 : 0;
	var Mid_range = $('Mid-range').checked ? 1 : 0;
	var topend = $('Top-end').checked ? 1 : 0;

	window.location.href = 'index.php?action=eat&spanish=' + chkSpanish
			+ '&chinese=' + chkChinese + "&japanese=" + chkJapanese
			+ '&vietnamese=' + chkVietnamese + '&korean=' + chkKorean
			+ '&thai=' + chkThai + '&indian=' + chkIndian + '&italian='
			+ chkItalian + '&french=' + chkFrench + '&international='
			+ chkInternational + '&budget=' + Budget + '&midrange=' + Mid_range
			+ '&topend=' + topend;
	return false;
}

function checkShowAll(fromObj, selectAllObj) {
	if (!fromObj.checked) {
		$(selectAllObj).checked = false;
	}
}

function selectAll(cssClass, fromObj) {
	$$("." + cssClass).each( function(el) {
		el.checked = fromObj.checked;
	});
}

function selectAllBoolean(cssClass, bool) {
	$$("." + cssClass).each( function(el) {
		el.checked = bool;
	});
	return false;
}

var current_image_index = 0;
var widget = false;
function openImage(image_index) {

	current_image_index = image_index;
	image_link = CLUSTER_HOST + "public/upload/album_picture/"
			+ image_list[image_index]['picture_id'] + ".jpg";
	image_title = image_list[image_index]['picture_title'];

	if (!widget) {
		$('open_image_detail').width = 208;
		$('open_image_detail').height = 111;

		$('open_image_detail').src = CLUSTER_HOST + 'public/images/loading.gif';

		new Asset.image(image_link, {
			onload : function() {
				$('open_image_detail').width = 454;
				$('open_image_detail').height = 393;
				$('open_image_detail').src = image_link;
			}
		});

		$('open_image_title').innerHTML = image_title;
	} else {
		$('widget_photo_detail_title').innerHTML = image_title + "<br/>"
				+ image_list[image_index]['picture_addon'];
		$('widget_photo_detail_link').width = 208;
		$('widget_photo_detail_link').height = 111;

		$('widget_photo_detail_link').src = CLUSTER_HOST + 'public/images/loading.gif';
		new Asset.image(image_link, {
			onload : function() {
				$('widget_photo_detail_link').width = 454;
				$('widget_photo_detail_link').height = 393;
				$('widget_photo_detail_link').src = image_link;
			}
		});

	}

	$('detail_image').style.display = '';
	$('open_image').style.display = '';
	$('detail_image').setStyle('height', $('body').clientHeight);
	$('detail_image').setStyle('opacity', 0.8);

	return false;
}

function closeImage() {
	widget = false;
	$('detail_image').style.display = 'none';
	$('open_image').style.display = 'none';
	return false;
}

var image_list = new Array();

function openAlbum(album_id) {

	$('detail_image').style.display = '';
	$('ajax_loading').style.display = '';

	$('detail_image').setStyle('height', $('body').clientHeight);
	$('detail_image').setStyle('opacity', 0.8);
	$('open_image').setStyle('top', document.documentElement.scrollTop + 50);

	var json_remote = new Json.Remote(
			'index.php?c=admin&action=ajax_view_album&album_id=' + album_id,
			{
				onComplete : function(data) {
					image_list = new Array();

					for (x = 0; x < data.totalCount; x++) {
						image_list[x] = new Array();
						image_list[x]['picture_id'] = data.rows[x].picture_id;
						image_list[x]['picture_title'] = data.rows[x].picture_title;
						image_list[x]['picture_addon'] = data.rows[x].picture_addon;
					}

					html_paging = '';
					if (data.totalCount > 3) {
						for (page = 1; page < Math.ceil(); page++) {
							html_paging += '<a href="#" onclick="return album_ajax_change_page('
									+ page + ');">' + page + '</a> ';
						}
					}

					if (html_paging == '') {
						$('widget_paging_html').style.display = 'none';
					} else {
						$('widget_paging_html').style.display = '';
						$('album_detail_paging').innerHTML = html_paging;
					}
					$('ajax_loading').style.display = 'none';
					$('open_image').style.display = '';

					widget = true;
					openImage(0);
					album_ajax_change_page(1);
				}
			}).send();

	return false;
}

function album_ajax_change_page(page) {
	var _from = 3 * (page - 1);
	var _to = 3 * page;
	z = 1;
	for (x = _from; x < _to; x++) {
		if (image_list[x]) {
			$('td_album_img_' + z).style.display = '';
			$('album_img_' + z).innerHTML = '<a href="#" onclick="return openImage('
					+ x
					+ ',true);"><img src="public/upload/album_picture/160x160/'
					+ image_list[x]['picture_id']
					+ '.jpg" width="120" height="120" border="0" />';
		} else {
			$('td_album_img_' + z).style.display = 'none';
		}

		z++;
	}
}




var core = {
	validator : {
		email : function(email) {
			return (email.indexOf(".") > 2) && (email.indexOf("@") > 0);
		},
		zipcode : function() {
		},
		required : function() {
		}
	},
	form : {
		submit : function() {
			error = false;
			$('.error').each( function() {
				this.style.display = 'none';
			});

			$('.required').each( function() {
				if (this.value == '') {
					$('#' + this.id + '-required').css('display', '');
					error = true;
				}
			});

			$('.confirm').each( function() {
				el_id = this.id.substring('confirm'.length);
				if (this.value != $('#' + el_id).val()) {
					$('#' + el_id + '-confirm').css('display', '');
					error = true;
				}
			});
			
			$('.val-email').each(function(){
				email_error = !core.validator.email(this.value);
			
				el_id = this.id;
				if (email_error)
				{
					$('#' + el_id + '-val').css('display', '');
					error = true;
				}
				
			});
			
			return !error;
		},
		textarea: {
			limit: function(el,size)
			{
				if (el.value.length > size) 
					{
					alert('Limited size '+ size);
					return false;
					}
				return true;
			}
		}
	}
};


function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

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 "";
}



var anyarena = {
	sendtofriend: {
		send: function()
		{
			error = false;
			
			if ($('#email')[0] != null && $('#email')[0].value.trim() != '' && $('#email')[0].value.trim() != FRIEND_EMAIL) {
				if (!core.validator.email($('#email')[0].value)) {
					alert(PLZ_ENTER_CORRECT_EMAIL); //PLZ_ENTER_FRIEND_EMAIL);
					error = true;
				}
			}
			else {
				alert(PLZ_ENTER_FRIEND_EMAIL);
				error = true;
			}
			
			if ($('#yemail')[0] != null && $('#yemail')[0].value.trim() != '' && $('#yemail')[0].value != YOUR_EMAIL) {
				if (!core.validator.email($('#yemail')[0].value)) {
					alert(PLZ_ENTER_CORRECT_EMAIL);
					error = true;
				}
			}
			else {
				alert(PLZ_ENTER_YOUR_EMAIL);
				error = true;
			}
			
			return !error;
		}
	},
	cityguide: {
		eat: {
			showAll: function(nationlity,height)
			{
				if ($('.headline span.'+nationlity+' a img').attr('src') == '/public/images/showall_res_arrow_down.png')
				{
					
					$('.cotainer.nationality.alphabetical.'+nationlity).animate({height:'165px'},250,'linear',function(){
						$('.cotainer.nationality.random.'+nationlity).show();
						$('.cotainer.nationality.alphabetical.'+nationlity).hide();
						$('.headline span.'+nationlity+' a').removeClass('current');
						$('.headline span.'+nationlity+' a img').attr('src','/public/images/showall_res_arrow.png');
					});
				}
				else
				{
					$('.cotainer.nationality.random.'+nationlity).hide();
					$('.headline span.'+nationlity+' a').addClass('current');
					
					$('.cotainer.nationality.alphabetical.'+nationlity).show();
					$('.cotainer.nationality.alphabetical.'+nationlity).animate({height:height+'px'});
					$('.headline span.'+nationlity+' a img').attr('src','/public/images/showall_res_arrow_down.png');
				}
				return false;
			},
			search: function(selectbox)
			{
				if (selectbox)
				{
				
					$('#select_box').val(1);
				}
				
				$('#search_eat_form').submit();
				return false;
			}
		},
		shop:
		{
			filter: function(value)
			{
				window.location.href = geturl('/cityguide/shop/filter'+value);
			}
		}
	},
	user: {
		logged: false
	},
	form : {
		register : {
			submit : function() {
				error = core.form.submit();
				return error;
			}
		}
	},
	album : {
		display :false,
		seeAll : function() {
			this.display = true;
			$('#allphoto').animate( {
				marginTop :'0px'

			}, 500, 'linear', function() {
				$('#seeall_thumb').hide();
			});

		},
		hideAll : function() {
			this.display = false;
			$('#allphoto').animate( {
				marginTop :'-300px'

			}, 500, 'linear', function() {
				$('#seeall_thumb').show();
			});
		}
	},
	newsletter: {
		submit: function() {
			email = $('#newsletteremail')[0].value;
			if (email != '' && email != 'email@domain.com') {
				var result = isEmail(email);
				if(result == false) {
					alert(INCORRECT_EMAIL_FOR_NEWSLETTER);
				}
				else {
					$.get("/index.php", { c: "index", action: "postnewsletter", email: email},
						function(data){
							$('#newsletteremail')[0].value = 'email@domain.com';
							alert(THANKS_FOR_SUBSRIBLE);
						}
					);
				}

			} 
			else {
				alert(ENTER_EMAIL_FOR_NEWSLETTER);
			}
			return false;
		}
	},
	photo : {
		current_photo :false,
		list :false,
		album_name_url :'',
		current_page :false,
		max_page :false,
		current_margin :0,
		current_picture_in_page :0,
		move :false,
		pic_per_page :5,
		init : function() {
		
		cookie_page = Number(getCookie('current_page'));
	
		if (cookie_page != null && cookie_page != "")
		{
			this.current_page = cookie_page;
		}
		else
		{
			
		found_current_page = false;
		current_photo = this.current_photo + 1;
		current_page = 0;
		while(!found_current_page)
		{
			current_page++;
			if (current_page == 1)
			{
				if (current_photo < 8) found_current_page = true; 
			}
			else
			{
				if (current_photo <= (((current_page-1) * 5) + 7)) found_current_page = true; 
			}
		}
		
		this.current_page = current_page;
		
		albumurl = window.location.href;
		_x = albumurl.indexOf('.com/') +4;
		_y = albumurl.indexOf('/picture/');
	
		cuurrentalbumurl = albumurl.substring(_x,_y+1);
		
		setCookie('current_album_url',cuurrentalbumurl,365);
		setCookie('current_page',current_page,365);
		}
		
		//this.current_page = Math.ceil((this.current_photo + 1)
		///		/ (this.pic_per_page));
		
		this.current_picture_in_page = this.current_page;
		
		found_current_page = false;
		current_photo = this.list.length;
		current_page = 0;
		while(!found_current_page)
		{
			current_page++;
			if (current_page == 1)
			{
				if (current_photo < 8) found_current_page = true; 
			}
			else
			{
				if (current_photo <= (((current_page-1) * 5) + 7)) found_current_page = true; 
			}
		}
		
		this.max_page = current_page;
		
		if (this.current_photo == 0)
			$('#album_photo_detail .nav .left').hide();
		if ((this.current_photo + 1) == this.list.length)
			$('#album_photo_detail .nav .right').hide();

		current_photo_in_first_page = this.current_photo
				- ((this.current_picture_in_page - 1) * this.pic_per_page)
				+ 1;
		// if (current_photo_in_first_page) {up_narrow_margin_left = 60}
		up_narrow_margin_left = 20 + ((current_photo_in_first_page - 1) * 75) + 40;
		$('#photo_up_narrow').css('margin-left', up_narrow_margin_left);

		this.initPage(false);
		},
		view : function(photo_id) {
			this.move = true;
			window.location.href = this.urlSite + geturl('/photos/album/' + this.album_name_url + '/picture/' + photo_id + '#centerView');
			return false;
		},

		next : function() {
			if (this.move)
				return false;
			if ((this.current_photo + 1) < this.list.length)
				this.view(this.list[this.current_photo + 1])
			return false;
		},
		previous : function() {
			if (this.move)
				return false;
			this.view(this.list[this.current_photo - 1]);
			return false;

		},
		scrollNext : function() {
			this.current_page = this.current_page + 1;
			setCookie('current_page',this.current_page,365);
			this.initPage(true);
			return false;
		},
		scrollBack : function() {
			this.current_page = this.current_page - 1;
			setCookie('current_page',this.current_page,365);
			this.initPage(true);
			return false;
		},
		initPage : function(animate) {
			if (this.current_page == this.max_page) {
				$('#thumbnail_nav .right a').hide();
			} else {
				$('#thumbnail_nav .right a').show();
			}
			if (this.current_page == 1) {
				$('#thumbnail_nav .left a').hide();
			} else {
				$('#thumbnail_nav .left a').show();
			}
			
			if (this.current_picture_in_page != this.current_page)
				$('#photo_up_narrow').hide();
			else
				$('#photo_up_narrow').show();
			
			this.current_margin = -75
					* (((this.current_page - 1) * this.pic_per_page));
			if (animate) {
				$('#thumbnail_nav .photo_thumbnail .wrapper').animate( {
					marginLeft :this.current_margin
				}, 900);
			} else {
				$('#thumbnail_nav .photo_thumbnail .wrapper').css(
						'margin-left', this.current_margin);
			}

		}

	}
};




$(document)
		.ready(
				function() {
					$('#album_photo_detail')
							.hover(
									function() {

										if (anyarena.photo.current_photo > 0)
											$('#album_photo_detail .nav .left')
													.animate({marginLeft: 0});
										if ((anyarena.photo.current_photo + 1) < anyarena.photo.list.length && $('#main_col #album_photo_detail .right').css('marginRight') != '0px')
											$('#main_col #album_photo_detail .right').animate({marginRight: 0});
										
											
										$('#main_col #album_photo_detail .nav .show_album').show();
										$('#main_col #album_photo_detail .nav .show_album').animate( {
													marginTop :0
										});
										$('#album_photo_detail .detail')
												.animate( {
													marginTop :-38
												},400,'swing');

									},
									function() {
										$('#album_photo_detail .detail')
												.animate( {
													marginTop :0
												},400,'swing');
										

										$('#album_photo_detail .nav .left')
												.animate({marginLeft: -25});
										
										$('#album_photo_detail .nav .right')
												.animate({marginRight: -125});
										
										$('#main_col #album_photo_detail .nav .show_album')
												.animate( {
													marginTop :-38
												},400,'swing');
										
												
									});

					$('#album_photo_detail').click( function() {
						anyarena.photo.next();
					});

					$('.hover_color').hover( function() {
						// $(this).addClass('grey');
							$(this).animate( {
								backgroundColor :'#E0E3DD'
							}, 150);
						}, function() {
							$(this).animate( {
								backgroundColor :'#ffffff'
							}, 150);
						});

					$('.album_hover_color').hover( function() {
						$('#main_col #' + this.id + '.album').animate( {
							backgroundColor :'#E0E3DD'
						}, 150);
					}, function() {
						$('#main_col #' + this.id + '.album').animate( {
							backgroundColor :'#ffffff'
						}, 150);
					});
					
					$('.hover').hover( function() {
						$(this).addClass('grey');

					}, function() {
						$(this).removeClass('grey');
					});

					$('.frame.click').click( function() {
						id = this.id.replace('frame-', '');

						href = $('.' + id).attr('href');
						window.location.href = href;
					});

					$('#album_photo_detail .left a')
							.hover(
									function() {
										$('#album_photo_detail .left img')
												.attr('src',
														'/public/images/photo_view/left_nav_t.png');
									},
									function() {
										$('#album_photo_detail .left img')
												.attr('src',
														'/public/images/photo_view/left_nav.png');
									});

					$('#album_photo_detail .right a')
							.hover(
									function() {
										$('#album_photo_detail .right img')
												.attr('src',
														'/public/images/photo_view/right_nav_t.png');
									},
									function() {
										$('#album_photo_detail .right img')
												.attr('src',
														'/public/images/photo_view/right_nav.png');
									});

					$('.photo_thumbnail .thumb').hover( function() {
						if (this.className == 'thumb') {
							this.style.marginTop = '-3px';
						}
						
					}, function() {
						if (this.className == 'thumb') {
							this.style.marginTop = '0px';
						}
					});
				
				
				$('input.trap').focus(function(){
					if (this.value == KEY_WORD)//'keyword')
					{
						this.value = '';
						this.style.color = 'white';
					}
				});
				
				$('input.trap').blur(function(){
					if (this.value == '')
					{
						this.value = KEY_WORD; //'keyword';
						this.style.color = '#ACACAC';
					}
				});
				
				
				$('#email.strap').focus(function(){
					if (this.value == FRIEND_EMAIL)
					{
						this.value = '';
						this.style.color = '#424242';
					}
				}).blur(function(){
					if (this.value == '')
					{
						this.value = FRIEND_EMAIL;
						this.style.color = '#ACACAC';
					}
				});
				
				$('#yemail.strap').focus(function(){
					if (this.value == YOUR_EMAIL)
					{
						this.value = '';
						this.style.color = '#424242';
					}
				}).blur(function(){
					if (this.value == '')
					{
						this.value = YOUR_EMAIL;
						this.style.color = '#ACACAC';
					}
				});
				
				
				$('#newsletteremail.text').focus(function(){
					if (this.value == "email@domain.com")
					{
						this.value = '';
						this.style.color = '#424242';
					}
				}).blur(function(){
					if (this.value == '')
					{
						this.value = "email@domain.com";
						this.style.color = '#ACACAC';
					}
				});
				
			
				
				
				
				});

