var emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/;
var emailReGlobal = /\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))/;
var phoneRe = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/;
var phoneReGlobal = /\d{2,3}(.+|)\d{7}/;
var urlRe =  /^(((ht|f){1}(tp:[/][/]){1})|((www.){1}))[-a-zA-Z0-9@:%_\+.~#?&//=]+$/;





function isShort(str){
	return (str.length<1);
}
function isLong(str){
	return (str.length>20);
}
function isPhone(str){
	return phoneRe.test(str);
}
function containPhone(str){
	return phoneReGlobal.test(str);
}
function containEmail(str){
	return emailReGlobal.test(str);
}
function isEmail(str){
	return emailRe.test(str);
}

function errHandle(msg,obj,doScroll){
	//$('#register-loader').hide();
	if(obj==null){
		alert(msg)
	} else {
		$('#'+obj).hide().html(msg).fadeIn('slow');
		var target=$('#'+obj).offset();
		//alert(target.top);
		num=140;
		if(doScroll){
			$(window)._scrollable().scrollTo( target.top - num, 800 );
		}
	}
}


$(document).ready(function() {
	
	
	// gallery
	$('#gallery').cycle({
		fx:      'fade',
		timeout:  0,
		next:   '#prev', 
		prev:   '#next',
		after: onAfter,
		before: onBefore,
		pager:  '#thumbs',
		pagerEvent: 'click',
		slideExpr: 'img',
		pagerAnchorBuilder: function(idx, slide) { 
			var theLink = slide.src
			thumbLink = theLink.replace('regular','thumb');
			return '<a href="#"><img src="' + thumbLink + '" /></a>'; 
		} 
	});
	function onBefore(curr, next, opts) {
		$('#thumb-description').hide().css('top',this.height+20+'px').show();
		$(document).bind('keyup', function(event){ 
			if ((event.keyCode && event.keyCode == 39) || (event.which && event.which == 39) ) {
				$('#gallery').cycle('next');
			} 
			else if ((event.keyCode && event.keyCode == 37) || (event.which && event.which == 37)) {
				$('#gallery').cycle('prev');
			} 
		});
	}
	function onAfter() { 
		$('#thumb-description').css('top',this.height+20+'px').html(this.alt); 
	}
	/*$(document).bind('keypress', function(e) {
		alert(e.charCode);
		if (e.keyCode == 37)
			$('#gallery').cycle('prev');
		else if (e.keyCode == 39)
			$('#gallery').cycle('next');
	});*/
	
	
	$('#pause').toggle(
		function() { 
			$(this).attr('src','images/btn-play.jpg').hover(
				function(){$(this).attr('src','images/btn-play-hover.jpg')},
				function(){$(this).attr('src','images/btn-play.jpg')}
			);
			$('#gallery').cycle('pause');
		},
		function() { 
			$(this).attr('src','images/btn-pause.jpg').hover(
				function(){$(this).attr('src','images/btn-pause-hover.jpg')},
				function(){$(this).attr('src','images/btn-pause.jpg')}
			);
			$('#gallery').cycle('resume')
		}
	);
		
	$('#pause').hover(
		function() {$(this).attr('src','images/btn-pause-hover.jpg');},
		function() {$(this).attr('src','images/btn-pause.jpg');}
	);
	$('#close').hover(
		function() {$(this).attr('src','images/close-hover.gif');},
		function() {$(this).attr('src','images/close.gif');}
	);
	$('#prev').hover(
		function() {$(this).attr('src','images/right-hover.gif');},
		function() {$(this).attr('src','images/right.gif');}
	);
	$('#next').hover(
		function() {$(this).attr('src','images/left-hover.gif');},
		function() {$(this).attr('src','images/left.gif');}
	);
	$('#send').hover(
		function() {$(this).attr('src','images/send-hover.gif');},
		function() {$(this).attr('src','images/send.gif');}
	);
	$('#print').hover(
		function() {$(this).attr('src','images/print-hover.gif');},
		function() {$(this).attr('src','images/print.gif');}
	);
	
	/*$('#lightbox-nav-btnNext').hover(
		function() {$(this).css('background-image','../images/left-hover.gif');},
		function() {$(this).css('background-image','../images/left.gif');}
	);
	$('#lightbox-nav-btnPrev').hover(
		function() {$(this).css('background-image','../images/right-hover.gif');},
		function() {$(this).css('background-image','../images/right.gif');}
	);*/
	
	// scroll to top
	$('#scrollTo').click(function(){
		$.scrollTo( '#scrollTarget', 800 );
	});
	
	
	// menu toggle
	$('.hasSub').click(
		function(){
			$('.sub').slideUp('slow');
			$('.hasSub').removeClass('selected');
			if($(this).next().css('display')=='none'){
				$(this).next().slideDown('fast');
				$(this).addClass('selected');
			}
		}
	);
	
	// thumbs 
	$('#thumbs').click(function(){
		$('#gallery-content').hide();
		$('#gallery-wrap').show();
	});
	$('#close').click(function(){
		$('#gallery-wrap').hide();
		$('#gallery-content').show();
	});
	
	// lightbox
	$('#gallery a').lightBox({
		fixedNavigation:true,
		overlayBgColor:'#fff',
		imageBtnClose: 'images/close.gif',
		imageBtnPrev: 'images/left.gif',
		imageBtnNext: 'images/right.gif'
	});
	// send friend modal
	$('#send').click(function(){
		$('#sendFriend-popup').modal();
	});
	
	// submit hover 
	$('.submit').hover(
		function(){
			$(this).addClass('submit-hover');
		},
		function(){
			$(this).removeClass('submit-hover');
		}
	);
	
});
/////////////////// start regular function ///////////////////

// print page
function printPage(pageID){
	win = window.open(baseUrl+"page-print.php?pageID="+pageID+"","name",""); 
}
// toggle exhibition thumbs
function toggleThumbs(ID){
	$('#thumb_' + ID).slideToggle();
	$('#thumb_' + ID + ' a').lightBox({
		fixedNavigation:true,
		overlayBgColor:'#fff',
		imageBtnClose: 'images/close.gif',
		imageBtnPrev: 'images/left.gif',
		imageBtnNext: 'images/right.gif'
	});
}
// validate contact form
function validate(){
	$('.required').hide();
	
	var comments = $('#comments').val();
	var phone = $('#phone').val();
	
	var fullname = $('#fullname').val();
	if(isShort(fullname)){
		errHandle('Please fill in full name','fullnameErr',false);
		return false;
	}
		
	var email = $('#email').val()
	if(isShort(email)){
		errHandle('Please fill in E-mail','emailErr',false);
		return false;
	}
	if(!isEmail(email)){
		errHandle('E-mail is invalid','emailErr',false);
		return false;
	}
	
	$.post("sendmail.php", { 
		subject: 'vardi kahana website - contact form', 
		f_fullname: fullname,	
		f_phone: phone,	
		f_email: email,	
		f_comments: comments }, 
		function(data){
			$('#success-row').show();
			document.contact.reset();
		});
	return false;
}

function validate_sendFriend(){
	$('.required').hide();
	
	var yourName = $('#yourName').val();
	if(isShort(yourName)){
		errHandle('Please fill in your name','yourNameErr',false);
		return false;
	}
	var yourEmail = $('#yourEmail').val()
	if(isShort(yourEmail)){
		errHandle('Please fill in your Email','yourEmailErr',false);
		return false;
	}
	if(!isEmail(yourEmail)){
		errHandle('Your Email is invalid','yourEmailErr',false);
		return false;
	}
	var friendName = $('#friendName').val();
	if(isShort(friendName)){
		errHandle('Please fill in your friend\'s name','friendNameErr',false);
		return false;
	}
	var friendEmail = $('#friendEmail').val()
	if(isShort(yourEmail)){
		errHandle('Please fill in your friend\'s Email','friendEmailErr',false);
		return false;
	}
	if(!isEmail(friendEmail)){
		errHandle('Your friend\'s Email is invalid','friendEmailErr',false);
		return false;
	}
	currLink = $('#link').val()
	
	$.post("sendFriend.php", { 
		subject: 'Email from ' + yourName, 
		link: currLink, 
		f_yourName: yourName,	
		f_yourEmail: yourEmail,	
		f_friendName: friendName,	
		f_friendEmail: friendEmail }, 
		function(data){
			$('#sendFriendForm').hide();
			$('#success').show();
			if($.modal){
				var t=setTimeout(function(){$.modal.close()},3000)
			}
		});
	return false;
}


