jQuery.noConflict();

function flash_load(){}
function DoNav(){} 
function flash_stop(){
	jQuery('#overlay').fadeOut('1000', function(){
		jQuery('#overlay').remove();
	});
}
function callback(e)
{
	if(!e.success)
	{
		jQuery('#splash').click(function(){				
			jQuery('#overlay').fadeOut('1000', function(){
				jQuery('#overlay').remove();
			});
		});
	}
}

function loadFlash() {		
	var flashvars = {};
	flashvars.movieName = "/wp-content/themes/ottoristorante/images/Otto_Intro.swf";
	var params = {};
	params.wmode = "Transparent"
	params.allowscriptaccess = "always";
	var attributes = {};
	swfobject.embedSWF("/wp-content/themes/ottoristorante/images/externalpreloader.swf", "splash", "1200", "612", "9.0.0", false, flashvars, params, attributes, callback);
}

function checkCookie() {
	var cookie = jQuery.cookie("splash");
	if(cookie == null) {
		jQuery.cookie("splash", 'true', {path:'/'});
		loadFlash();
	} else {
		jQuery('#overlay').remove();
	}
}

function updatePanel() {
	updateLinks("#nav");
	updateLinks("#section-content");
	jQuery('#pane1').jScrollPane();
	//temp.scroll(
	//scrollPane.getViewport().setViewPosition(new Point(0,0));
	
	//call submit form
	jQuery("#contactfrm").submit(function(e){
		
		if(validateContactForm(this)){
			submitForm("#contactfrm");			
		}// else alert('invalid post');
		return false;
	});	
	
	//call submit reservation form
	//validateReservationsForm
	jQuery("#reservationsfrm").submit(function(e){
		
		if(validateReservationsForm(this)){
			submitForm("#reservationsfrm");			
		}
		return false;
	});
	
	
	
}

function getPath(href) {
	var parts = href.split('/');
	if(parts.length > 1 && parts[1].length==0 && parts[0].length>0 && parts[0][parts[0].length-1]==':') {
		if(parts[0] != 'http:'){
			return null;
		}
		parts = parts.slice(2);
		parts[0] = '';
	} else if(href.indexOf('mailto:')== 0) {
		return null;
	}
	return parts.join('/'); 
}

function updateLinks(selector) {
	jQuery(selector + ' a[rel*=lightbox]').lightBox();
	jQuery(selector + ' a[href]').click(function(e) {
		if(jQuery(this).hasClass('popup')){
			PopupWindow(this.href);
			e.preventDefault();
		} else if(jQuery(this).hasClass('modal')){
			
			popupMovie(this);
			e.preventDefault();
		} else if(jQuery(this).attr('rel') == 'lightbox'){
			/* Fall through to lightbox handler */
			e.preventDefault();
		} else if(typeof(this.target) != "string" || this.target.length == 0) {
			var path = getPath(this.href);
			if(path){
				SWFAddress.setValue(path);
				e.preventDefault();
			}
		}
	});
}

function popupMovie(movie){
	//Cancel the link behavior
	//e.preventDefault();
	
	//Get the A tag
	var id = jQuery(movie).attr('href');
	
	//var box = jQuery('#QuickTime');
	var box_width = 458;
	var box_height = 422;
	
	
	
	if(file_get_ext(id)=='flv')
	{
		var flash = '<embed src ="/wp-content/plugins/otto-gallery/flvplayer.swf?file='+id+'" width="458" height="394" allowfullscreen="true"/>';
			
		jQuery("#video").html(flash);
	} else if(file_get_ext(id)=='mov') {
		var play_mov = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="458" width="394">';
		play_mov += '<param name="src" value="'+id+'"/>';
		play_mov += '<param name="controller" value="true" />';
		play_mov += '<param name="autoplay" value="true" />';
		play_mov += '<param name="scale" value="tofit" />';
		play_mov += '<embed type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/" scale="tofit" height="394" width="458" src="'+id+'" controller="true" autoplay="true" /></object>';
		
		jQuery("#video").html(play_mov);
	} else {		
		jQuery("#video").html('<object width="458" height="394" type="application/x-shockwave-flash" data="http://www.youtube.com/v/'+id+'"><param name="movie" value="http://www.youtube.com/v/'+id+'" />This video was embedded using the YouTuber plugin by <a href="http://www.roytanck.com">Roy Tanck</a>. Adobe Flash Player is required to view the video.</object>');
	}
	
	
	//Get the screen height and width
	var maskHeight = jQuery(document).height()-5;
	var maskWidth = jQuery(window).width();

	//Set heigth and width to mask to fill up the whole screen
	jQuery('#mask').css({'width':maskWidth,'height':maskHeight});
	
	//transition effect		
	//jQuery('#mask').fadeIn(1000);	
	jQuery('#mask').fadeTo("slow",0.8);	
	//jQuery('#mask').animation('');
	
	//jQuery('#mask').animate({'opacity':0.75}, 1000);
	
	//Get the window height and width
	var winH = jQuery(window).height();
	var winW = jQuery(window).width();
       
	//Set the popup window to center
	jQuery('#QuickTime').css('top',  winH/2-box_height/2);
	jQuery('#QuickTime').css('left', winW/2-box_width/2);

	//transition effect
	jQuery('#QuickTime').fadeIn(); 
}



function updateContent(data)
{
	var beginTag = '<!-- CONTENT START -->';
	var endTag = '<!-- CONTENT END -->'
	if(typeof(data) == 'string')
	{
		var begin = data.indexOf(beginTag,0);
		if(begin >0)
		{
			begin += beginTag.length;
			end = data.indexOf(endTag, begin);
			if(end > 0){
				jQuery("#nav").remove();
				jQuery("#section-content").remove();
				jQuery("#content").append(data.substr(begin, end-begin));
				updatePanel();
				
				fadeContentIn();
			}
		}
	}
}
function fadeContentOut()
{
	jQuery("#nav").fadeOut(500);
	jQuery("#section-content").fadeOut(500);
}
function fadeContentIn()
{
	jQuery("#content").fadeIn(1000);
}
function updateTitle(data)
{
	
	var beginTag = '<title>';
	var endTag = '</title>'
	if(typeof(data) == 'string')
	{
		var begin = data.indexOf(beginTag,0);
		if(begin >0)
		{
			begin += beginTag.length;
			end = data.indexOf(endTag, begin);
			if(end > 0){
				SWFAddress.setTitle(data.substr(begin, end-begin).replace('&amp;', '&'));
			}
		}
	}
}

function updateLeftImage(data)
{
	var beginTag = '<div id="image-left">';
	var endTag = '</div>';
	if(typeof(data) == 'string')
	{
		var begin = data.indexOf(beginTag,0);
		if(begin >0)
		{
			begin += beginTag.length;
			end = data.indexOf(endTag, begin);
			if(end > 0){
				jQuery("#image-left").prepend(data.substr(begin, end-begin));
				if(jQuery("#image-left img").length>1){ 
					jQuery("#image-left img:last").fadeOut(1000, function(){
						jQuery(this).remove();	
					});
				}
								
			}
		}
	}
	
}

function handleExternalChange(event)
{
	var path = event.path;
}
 
function handleChange(event)
{
	var path = event.path;
	if(window['ajaxpath'] != path)
	{
		window['ajaxpath'] = path;
	}
	loadPath(path);
}



function submitForm(selector) {
	fadeContentOut();
	jQuery.post(window['ajaxpath'],
		jQuery(selector).serialize(),
		function(data) {		
			updateContent(data);			
		});
}
function updateNewsletterMessage(data) {
	
	
	var beginTag = '<div class="subscribe">';
	
	var endTag = '</div>';
	if(typeof(data) == 'string')
	{
		var begin = data.indexOf(beginTag,0);
		if(begin >0)
		{
			begin += beginTag.length;
			end = data.indexOf(endTag, begin);
			if(end > 0){
				//jQuery(".subscribe").append(data.substr(begin, end-begin));
				var contentParagraph = data.substr(begin, end-begin);
				var beginParagraphTag = '<p>';
				var endParagraphTag = '</p>';
				var beginP = contentParagraph.indexOf(beginParagraphTag,0);
				
				
				beginP += beginParagraphTag.length;
				endP = contentParagraph.indexOf(endParagraphTag, beginP);
				jQuery(".subscribe").html(contentParagraph.substr(beginP, endP-beginP));
				
				
			}
		}
	}
	
	
	
}

function submitFormNewsletter(selector) {	
	jQuery.post(window['ajaxpath'],
		jQuery(selector).serialize(),
		function(data) {
			
			updateNewsletterMessage(data);
		});
}

function loadPath(path) {
	fadeContentOut();
	jQuery("#loading").show();
	jQuery.ajax({
		url: path,		
		success: function(data) {
			updateContent(data);
			updateTitle(data);
			updateLeftImage(data);
			jQuery("#loading").hide();
		}
	});
	
	
}



function checkBroswer()
{

	
	if(jQuery.browser.mozilla && parseFloat(jQuery.browser.version) >= parseFloat("1.9"))
	{	
		return true;
	}
	
	if(jQuery.browser.msie && parseFloat(jQuery.browser.version) >= parseFloat("8"))
	{
		return true;
	}
	if(jQuery.browser.webkit && parseFloat(jQuery.browser.version) >= parseFloat("533.17"))
	{
		return true;
	}
	
	
	
	return false;
}


jQuery(document).ready(function(){

	//alert(jQuery.browser.version);
	
	if(!checkBroswer()) {
		alert('Ottoristorante.com.au has been built for the following browsers:\n'+
				'Internet Explorer 8 and above\n'+
				'Firefox 3.6 and above\n'+				
				'Safari 4.0.5 and above\n'+
				'Other browsers may provide a user with a reduced user experience');
	}
	


	if(jQuery('#overlay').length==1){
		window['ajaxpath'] = '/';
		checkCookie();
		updateLinks('#header');
		updatePanel();
		updateLinks('#footer');
		
		
		jQuery(document).ajaxError(function() {
  			fadeContentIn();
		});
		//call submit Newsletter form	
		jQuery("#frmsubscribe").submit(function(e){
			
			if(validateNewsletter(this)){
				submitFormNewsletter("#frmsubscribe");			
			}
			return false;
		});
	
//		SWFAddress.setTracker(_gaq._trackPageview);


		SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
		SWFAddress.addEventListener(SWFAddressEvent.EXTERNAL_CHANGE, handleExternalChange);
	}
	else
	{
		window.location.replace('/#' + window.location.pathname);
	}
	
});



/////////////
/* Rotator */
/////////////
var imageRotatorImage = 0;
var valuesArrayImage = [];
//Display this function when json finish
function initialImage(){
	if(jQuery("#image-right").length == 1){
		var cookie = jQuery.cookie("indexCookie");
		imageRotatorImage = 0;		
		if(cookie != null){//when the first load or cookie expire
			var imageRotatorImage = parseInt(cookie);
			if(isNaN(imageRotatorImage)) {
				imageRotatorImage = 0;
			}
			else
			{
				imageRotatorImage = (imageRotatorImage+1)%(valuesArrayImage.length);
				jQuery.cookie("indexCookie", imageRotatorImage, {path:'/'});
			}
		}
		//Display 2 Images in front end			
		jQuery('#image-right').prepend('<img src="'+valuesArrayImage[(imageRotatorImage+1)%valuesArrayImage.length]+'" alt=""/>');
		$intervalid = setInterval( "rotateImage()", 15000/2 );
	}
}

function rotateImage(){
	var images = jQuery();
	
	jQuery('#image-right img:last').fadeOut(8000/2, function(){
		jQuery(this).remove();
	});

	imageRotatorImage = (imageRotatorImage+1)%(valuesArrayImage.length);
	jQuery.cookie("indexCookie", imageRotatorImage, {path:'/'});
	jQuery('#image-right').prepend('<img src="'+valuesArrayImage[(imageRotatorImage+1)%(valuesArrayImage.length)]+'" alt=""/>');
}

jQuery(document).ready(function(){
	jQuery.getJSON(
		"/json-requestimage.php",
		function(data){
			jQuery.each(data.userdata, function(i,user){
				valuesArrayImage[i] = user.rotated_image;					
			});
			//call initialImage function to display in front end from javascript	
			initialImage();

		}
	);
});

//End Rotated

jQuery(document).ready(function(){
	var flashvars = {};				
	var flashvars = {};

	var params = {'allowscriptaccess':'always','wmode':'transparent'};
	params.wmode = "Transparent";

	var attributes = {'id':'logo-image'};
	swfobject.embedSWF("/wp-content/themes/ottoristorante/images/Otto-Symbols.swf", "logo-image", "83", "147", "9.0.0", null, flashvars, params, attributes);
});




	
jQuery(document).ready(function(){
	
	
	//check phone
	jQuery("#ec_phone").keypress(function(e){			
		if (e.which > 31 && (e.which < 48 || e.which > 57)){
			if (e.which != 40 && e.which != 41 && e.which != 43 && e.which != 44 && e.which != 45 && e.which != 32 && e.which != 37 && e.which != 39 && e.which != 46 && e.which != 35 && e.which != 36)
				e.preventDefault();
		}			
	});
	// check verify code
	jQuery("#ec_code").keypress(function(e){			
		if (e.which > 31 && (e.which < 48 || e.which > 57)){
			if (e.which != 40 && e.which != 41 && e.which != 43 && e.which != 44 && e.which != 45 && e.which != 32 && e.which != 37 && e.which != 39 && e.which != 46 && e.which != 35 && e.which != 36)
				e.preventDefault();
		}			
	});
	
	
	
	//newsletter

});


//////////////////////////	
/* Validation Functions */
//////////////////////////

function PopupWindow(URL) {
h=500;
w=500;
window.open(URL,'Title','toolbar=0,location=0,status=0,menubar=0,scrollbars=0,top='+Math.floor(screen.height/2-h/2)+',left='+Math.floor(screen.width/2-w/2)+',resizable=0,width='+w+',height='+h);
}
function IsValidEmail(e)
	{
    ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
    re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
    re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    
    for(i=0; i < e.length ;i++)
    {
        if(ok.indexOf(e.charAt(i))<0)
        {    
            return false;
        }	
    } 
        
    if (!e.match(re) && e.match(re_two))
    {
        return true;		
    }
    else
    {
        return false;
    }    
}




function checkField(id, value, error) {
	if(value == "")	{
		jQuery('#' +id).addClass("error");
		jQuery('#' +id).html(error);		
		return false;
	} else {
		jQuery('#' +id).removeClass("error");
		jQuery('#' +id).html("");
	}	
	return true;
}
function checkEmailField(id, value, error) {
	if(value != "" && !IsValidEmail(value)) {
		jQuery('#' +id).addClass("error");
		jQuery('#' +id).html(error);
		return false;
	} else {
		jQuery('#' +id).removeClass("error");
		jQuery('#' +id).html("");
	}
	return true;
}

function validateNewsletter(form){
	var isValid = true;
	
	
	isValid = isValid && checkField('lbl_email',form.wpnewsletter_email.value ,"Please enter your email address");
	isValid = isValid && checkEmailField('lbl_email',form.wpnewsletter_email.value ,"Invalid email address");
	
	return isValid;
}

function validateContactForm(form){
	
	var isValid = true;
	isValid = isValid && checkField('lbl_firstname',form.ec_firstname.value ,"Please enter your first name");
	isValid = isValid && checkField('lbl_lastname',form.ec_lastname.value ,"Please enter your last name");
	isValid = isValid && checkField('lbl_phone',form.ec_phone.value ,"Please enter your phone");
	isValid = isValid && checkField('lbl_phone',form.ec_phone.value ,"Please enter your phone");	
	isValid = isValid && checkField('lbl_ec_email',form.ec_email.value ,"Please enter your email");
	isValid = isValid && checkEmailField('lbl_ec_email',form.ec_email.value ,"Invalid email address");
	isValid = isValid && checkField('lbl_enquiry',form.ec_message.value ,"Please enter your enquiry");
	isValid = isValid && checkField('lbl_code',form.ec_code.value ,"Please enter the code shown below");	
	return isValid;	
}

function validateReservationsForm(form)
{
	var isValid = true;
	
	isValid = isValid && checkField('lbl_firstname',form.rc_firstname.value ,"Please enter your first name");
	isValid = isValid && checkField('lbl_lastname',form.rc_lastname.value ,"Please enter your last name");
	isValid = isValid && checkField('lbl_phone',form.rc_phone.value ,"Please enter your phone");
	isValid = isValid && checkField('lbl_rc_email',form.rc_email.value ,"Please enter your email");
	isValid = isValid && checkEmailField('lbl_rc_email',form.rc_email.value ,"Invalid email address");
	isValid = isValid && checkField('lbl_rc_people',form.rc_people.value ,"Please enter the No. of People");
	isValid = isValid && checkField('lbl_rc_date',form.rc_date.value ,"Please enter the Date");
	isValid = isValid && checkField('lbl_rc_time',form.rc_time.value ,"Please enter the Time");
	isValid = isValid && checkField('lbl_code',form.rc_code.value ,"Please enter the code shown below");
	
	
	return isValid;
}





///////////////
/* Popup Box */
///////////////
function file_get_ext(filename)
{
    return typeof filename != "undefined" ? filename.substring(filename.lastIndexOf(".")+1, filename.length).toLowerCase() : false;
}


jQuery(document).ready(function() {	

	jQuery('#stimuli_bottomNavClose').click(function(e){
		e.preventDefault();
	});


	jQuery('a[id=close]').click(function(e) {		
		jQuery('.window').hide();
		jQuery('#mask').hide();
		jQuery('#video *').remove();
		e.preventDefault();
	});
	
	//if close button is clicked
	jQuery('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		jQuery('#mask').hide();
		jQuery('.window').hide();
	});		
	
	//if mask is clicked
	jQuery('#mask').click(function () {		
		jQuery(this).hide();
		jQuery('.window').hide();		
	});			
	
});
jQuery(window).resize(function(e) {
	//select all the a tag with name equal to modal
	
		//Cancel the link behavior
		e.preventDefault();
		
		var id = jQuery('a[name=modal]');
	
		//Get the screen height and width
		var maskHeight = jQuery(document).height()-5;
		var maskWidth = jQuery(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		jQuery('#mask').css({'width':maskWidth,'height':maskHeight});
		
		
	
		//Get the window height and width
		var winH = document.documentElement.clientHeight;
		var winW = jQuery(window).width();
              
		//Set the popup window to center
		jQuery('#QuickTime').css('top',  winH/2-jQuery('#QuickTime').height()/2);
		jQuery('#QuickTime').css('left', winW/2-jQuery('#QuickTime').width()/2);
	
	
});
