var c_min=0;	
var c_max=600;	//max width
var c_lft=0;	
var m_lft=360; //subject width
var news_size = "";
		
var selecteasing="easeInOutQuad";
var animspeed=800;

$(function(){


	//partners
	
	var partners = {
		height: $('#partners').height(),
		queue: false,
		duration: 200
	};
	
	var imgtop = [];
	
	$('#partners a').tipsy({
		gravity: 's',
		opacity: 1,
		fade: true
		
	});
	
	$('#partners a')
	.each(function(i){
		imgtop[i]=(($(this).height()-$(this).children('img').height())/2)
		$(this).children('img').css({top: imgtop[i]});
	})
	.bind("mouseover focus", function(){
		ti=$('#partners a').index(this);
		$(this).children('img').animate({
			top: imgtop[ti]-5
		},{queue:partners.queue, duration:partners.duration})
	})
	.bind("mouseout blur",function(){
		ti=$('#partners a').index(this);
		$(this).children('img').animate({
			top: imgtop[ti]
		},{queue:partners.queue, duration:partners.duration})
	});
						   
	$('div.pgewrapper, div.pgecontainer').hide();
	$('div.pgemask').css({opacity: 0.8});
						   
    muse_arr = new Array();
    muse_arr[0] = "media/flash/m001.mp3";
    muse_arr[1] = "media/flash/m002.mp3";
    muse_arr[2] = "media/flash/m003.mp3";
						   
flashembed("flmusic", "media/flash/music.swf?muse="+ muse_arr[Math.floor(Math.random()*muse_arr.length)]);
						  
    $('#join').submit(function(){
        joinemail = $('#joinemail').val(); 
	
        if(CheckEmail(joinemail))
        {
            $.post("media/php/baf/baf2.php",{
                email: joinemail
            }, function(data){
                $('#baf_return').text(data).css({
                    color: "#fff"
                });
                $('#joinemail').val("");
            });	
        }
        else
        {
            $('#baf_return').html("Please enter a valid email address.")
            .css({
                color: "#fff"
            });	
        }
        return false;
    });
	
	
    //variables
    var contain = $('#sections');
    var section = contain.children();
    var nav = section.children('.nav');
    var nav_ul = nav.children('ul');
    var section_name = $('a.section_name');
    var section_name = $('a.section_name');
    var pagecontent = $('.pagecontent');
	
    var s_maxwidth = 678;
    var s_minwidth = 30;



    //startup	
    nav_ul.fadeOut();
    //pagecontent.hide();
	
	
    //hash
    checkhash = hashTohref();
    if (!checkhash) {
        expand(section.eq(0));
    } else {
        ref = $('div.nav ul li a').filter(function(){
            return $(this).attr('href') == checkhash;
        });
        if(ref.length==0){	
            expand(section.eq(0));
			
			if(window.location.hash=="#ZW1sYms="){
				$('.current').children('.pagecontent')
				.load("media/content/emlbook/index.htm" + " .ajax_content", function(){});
			}
			
			if(window.location.hash=="#MjAxMC0wMi0yMi1mbG9nYXM="){
				$('.current').children('.pagecontent')
				.load("media/content/lifestyle/news/2010-02-22-flogas.htm" + " .ajax_content", function(){});
			}
			
			if(window.location.hash=="#YmVybmFyZG1hdHRoZXdz"){
				$('.current').children('.pagecontent')
				.load("media/content/lifestyle/news/2010-03-26-bernard.htm" + " .ajax_content", function(){});
			}
			
			if(window.location.hash=="#d2luZXRhc3Rpbmc="){
				$('.current').children('.pagecontent')
				.load("media/content/lifestyle/news/2010-03-23-winetasting.htm" + " .ajax_content", function(){});
			}
			
			if(window.location.hash=="#a2l0Y2hlbmJ1cm5vdXQ="){
				$('.current').children('.pagecontent')
				.load("media/content/lifestyle/burnout-menus.htm" + " .ajax_content", function(){ 
					pageload_callback($('div.nav ul li a').eq(0));
				});
				
			}
			//fathers day
			if(window.location.hash=="#ZmF0aGVyc2RheQ=="){
				$('.current').children('.pagecontent')
				.load("media/content/restaurants/fathersday.htm" + " .ajax_content", function(){ 
					pageload_callback($('div.nav ul li a').eq(0));
				});		
			}
			//bar default
			if(window.location.hash=="#YmFy"){
				$('.current').children('.pagecontent')
				.load("media/content/bars/default.htm" + " .ajax_content", function(){ 
					pageload_callback($('div.nav ul li a').eq(0));
				});		
			}
			//recruitment
			if(window.location.hash=="#cmVjcnVpdG1lbnQ="){
				$('.current').children('.pagecontent')
				.load("media/content/forms/recruitment.html" + " .ajax_content", function(){ 
					pageload_callback($('div.nav ul li a').eq(0));
				});		
			}
			
        } else {
            expand(ref.parent().parent().parent().parent());
			ref.parent().parent().parent().parent().children('.pagecontent')
				.load(checkhash + " .ajax_content", function(){
					//callback
					pageload_callback(ref);
				});
        }
		
    }

	
    //show first section 
	
    //events
    section_name.click(function(){
        window.location.hash="none";

        expand($(this).parent().parent());

        return false;
    });
	
    $('input#joinemail').example(function() {
        return $(this).attr('title');
    });

	
	
    //expand section elements
    function expand(element){
        if(!element.hasClass('current')){
			
            //out
            $('.current')
            .animate({
                width: s_minwidth
            },{
                queue: false
            });
            $('.current').children().children('ul').fadeOut();
			
			
            $('.current').children('.nav').animate({
                width: 9
            }).children('a.section_name').animate({
                width: 9
            }).css({
                backgroundPosition: "-116px 0px"
            });
			
            $('.current').children('.pagecontent').hide();
            $('.current').removeClass('current');
			

			
            //in
            element.children('.pagecontent').show();
			
            element
            .animate({
                width: s_maxwidth	 
            },{
                queue: false
            })
            .addClass('current');
			
            element.children('.nav').animate({
                width: 115
            }).children('a.section_name').animate({
                width: 115
            }).css({
                backgroundPosition: "0px 0px"
            });
			
            element.children().children('ul').fadeIn();

            pageload(element.children('.nav').children('.section_name'));
			
        }
    }
	
    nav.children().children().children().click(function(){
	
        if($(this).attr('href').substr(0,4)=="http"){
            
        } else {
            pageload($(this));
			return false;
        }
        	
    });


});


//jquery extended
(function($){
    $.fn.extend({
        formatPartnersLst:function(){
            centObj=Math.round($('.partnersLi li').size()/2-1);
            pSlideCHalf=$('.pSliderC').width()/2;
            
            $('.partnersLi li').animate({opacity:0},0,function(){
                $(this).css({display:'block'});
                $(this).animate({opacity:.5},{duration:0,queue:false});
                $('.partnersLi li').eq(centObj).animate({opacity:1},{duration:500,queue:false});
            });

            iC=0;
            icWid=0;
            while(iC<=centObj-1){icWid=icWid+$('.partnersLi li').eq(iC).width();iC++;}
            $('.partnersLi').css({left:((-icWid+pSlideCHalf)-($('.partnersLi li').eq(centObj).width()/2))});
            //$('.partnersLi').animate({left:((-icWid+pSlideCHalf)-($('.partnersLi li').eq(centObj).width()/2))},{duration:300,queue:false});
        },
		returnFrmVals:function(){
			thsElm=$(this);
			expVal="";
			thsElm.children('input[type="text"]').each(function(){expVal=expVal+$(this).val()+"{spl}";});
			thsElm.children('textarea').each(function(){expVal=expVal+$(this).val()+"{spl}";});
			thsElm.children('select').each(function(){expVal=expVal+$(this).val()+"{spl}";});
			
			thsElm.children('input[type="checkbox"]').each(function(){
				thsChVl=$(this).val();
				if($(this).is(":checked")){exChVl=1;}else{exChVl=0;}
				expVal=expVal+exChVl+"{spl}";
			});
			
			thsElm.children('input[type="radio"]:checked').each(function(){
				thsRaVl=$(this).val();
				expVal=expVal+thsRaVl+"{spl}";
			});
			
			
			return expVal;
		}
    });
})(jQuery);



//javascript functions

function IntLnk(){
			$('.intlink').click(function(){
				var loadurl = $(this).attr('href');
						
				//load the content
				//loadcontent(button.parent().parent().parent().parent().children('.pagecontent'));
				$('.current').children('.pagecontent')
				.load(loadurl + " .ajax_content", function(){
					
					//callback
					pageload_callback(button);
				});
				return false;
			});
}

function loadcontent(element) {
    element.fadeOut();
    element.bind("load", function(){
        element.fadeIn();			  
    });
}

function pageload(button){
	
    $('.pagecontent').html('');
		
    if (button.hasClass("section_name")) {
        //setup variables
        var loadurl = button.parent().children('ul').children().children('a').eq(0).attr('href');
					
        //load the content
        //loadcontent(button.parent().parent().children('.pagecontent'));
        button.parent().parent().children('.pagecontent')
        .load(loadurl + " .ajax_content", function(){
				
            //callback
            pageload_callback(button.parent().children('ul').children().children('a').eq(0));
        });
				
    } else {
			
        //setup variables
        var loadurl = button.attr('href');
					
        //load the content
        //loadcontent(button.parent().parent().parent().parent().children('.pagecontent'));
        button.parent().parent().parent().parent().children('.pagecontent')
        .load(loadurl + " .ajax_content", function(){
				
            //callback
            pageload_callback(button);
        });
    }
}

function pageload_callback(button){
    var ajaxhook = button.parent().parent().parent().parent().children('.pagecontent').children('.ajax_content');
	
	
	if($('a.bookint')) {
			$('a.bookint').click(function(){
				$('div.pgecontainer').hide();
				var rel=$(this).attr('rel');
				var lb=$('div.pgewrapper .pgecontainer').eq(rel);
					
				$('.pgewrapper').show();
				lb.show().pgeConCen();
									   
				$(window).bind("resize",function(){
					$('.pgecontainer').pgeConCen();					   
				});
				
				$('.pgemask').click(function(){
					$('div.pgewrapper').hide();		
				});	
				return false;
			});
			
			
	}
	

    if($('.lightBox').length>0){
        $('.lightBox').lightBox();
    }
	
	
    if($('a.hkback').length>0){
        $('a.hkback').click(function(){
            var loadurl = $(this).attr('href');
					
            //load the content
            //loadcontent(button.parent().parent().parent().parent().children('.pagecontent'));
            $(this).parent().parent().parent().parent().children('.pagecontent')
            .load(loadurl + " .ajax_content", function(){
				
                //callback
                pageload_callback(button);
            });
            return false;
        });
    }
	
	//private dining
	
	if($('.pd_link').length){
		$('.pd_link').bind('click',function(){
			var loadurl = "media/content/forms/privatedining.php?restaurant_id=" + $(this).attr('rel');
			$(this).parent().parent().parent().parent().parent().parent().parent().parent().children('.pagecontent')
            .load(loadurl + " .ajax_content", function(){
				
                //callback
                pageload_callback(button);
            });
            return false;							
		});
	}
	
	
	
	//contact forms
	if($('.cFrm').length>0){
		$('.cFrm p').formValidate();
		
		//reset form
		$('.cFrm p').children('input[type="reset"]').click(function(){$(this).parent().frmReset();});
		
		$('.cFrm').submit(function(){
			thsObj=$(this).children('p');
			pCount=thsObj.size();
			valSize=0;
			cVals="";
			
			//validate form
			if($('.privatediningform').length>0){
				pCount=0;
				$(this).children('.pdSec').each(function(){
					thsObj=$(this).children('p');								 
					pCount=pCount+thsObj.size();
					thsObj.each(function(){if($(this).frmSubmit()){valSize++;cVals=cVals+$(this).returnFrmVals();}});
					
				});
			}
			else{thsObj.each(function(){if($(this).frmSubmit()){valSize++;cVals=cVals+$(this).returnFrmVals();}});}
			
			//is form valid
			if(valSize==pCount){
				$('.cFrmReturn').text("Processing your request...")
				
				//form names
				if($('.privatediningform').length>0){fncName="1",postAjax=true;}
				else if($('.recruitmentform').length>0){fncName="2",postAjax=false;}
				else if($('.contactMarco').length>0){fncName="3",postAjax=false;}
				else if($('.competitionform').length>0){fncName="4",postAjax=true;}
				else if($('.competitionform2').length>0){fncName="5",postAjax=true;}
				
				
				//submit form vals
				if(postAjax){
					 $.ajax({ 
					   type: "POST", 
					   url: "media/php/contactforms/process.php", 
					   data: "func=cFrmReq&frmIndx="+fncName+"&frmVals="+cVals+"&restaurant_id=" + $('#restaurant_id').val() + "&restaurant_name=" + $('#restaurant_name').val(), 
					   success: function(data){
						   if(data){$('.cFrmReturn').text("Thank you for your enquiry.");}
						   else{$('.cFrmReturn').text("There was an issue with your enquiry");}
						   $('.cFrm textarea, .cFrm input[type="text"]').val("");
					   } 
					 });
				 }
				 else{
					return true; 
				 }
			}
			else{$('.cFrmReturn').text("Please ensure you have filled in the mandatory fields marked with *correctly")}
			return false;
		});
		
		/*
		$('.contactMarco').submit(function(){
			return false;
		});
		
		$('.competitionform').submit(function(){
			alert("comps");
			return false;
		});
		
		$('.recruitmentform').submit(function(){
			alert("recruitment");
			return false;
		});
		
		$('.privatediningform').submit(function(){
			thsForm=$(this).children('p');
			if(thsForm.frmSubmit()){
				alert("Form is valid")	
			}
			else{
				alert("Form is not valid");
			}
			return false;
		});
		*/
		
		
	}
    
    //Partners Slide
    if($('.pSliderC').length==0){
        $('#partners').css({display:'block'});
    }
    
    if($('.pSliderC').length>0){
        $('#partners').css({display:'none'});
        
        glbWi=0;
        $('.partnersLi li').each(function(){glbWi=glbWi+$(this).width();});
        $('.partnersLi').css({width:glbWi});
		
        $().formatPartnersLst();
		
        $('.parLftAr').click(function(){
            lstCount=$('.partnersLi li').size()-1;

            lstClassNme=$('.partnersLi li').eq(lstCount).attr('class');
            httpAddr=$('.partnersLi li').eq(lstCount).children('a').attr('href');

            $('.partnersLi li').eq(lstCount).remove();
            $('.partnersLi').prepend('<li class="'+lstClassNme+'"><a href="'+httpAddr+'" class="intlink">test</a></li>');
			IntLnk();
			$().formatPartnersLst();
			
            return false;							
        });
		
        $('.parRteAr').click(function(){
            fstClassNme=$('.partnersLi li').eq(0).attr('class');
            httpAddr=$('.partnersLi li').eq(0).children('a').attr('href');

            $('.partnersLi li').eq(0).remove();
            $('.partnersLi').append('<li class="'+fstClassNme+'"><a href="'+httpAddr+'" class="intlink">test</a></li>')
			.css({left:0});
			IntLnk();
            $().formatPartnersLst();

            return false;							
        });
		
    }

    if($('a.summary').length>0){
        $('a.summary').click(function(){
            var loadurl = $(this).attr('href');
					
            //load the content
            //loadcontent(button.parent().parent().parent().parent().children('.pagecontent'));
            $(this).parent().parent().parent().parent().parent().parent().parent().children('.pagecontent')
            .load(loadurl + " .ajax_content", function(){
				
                //callback
                pageload_callback(button);
            });
            return false;
        });
    }
    if($('#video').length > 0){	
        var flashpath = "media/video/" + $('#video').attr('rel');
        flashembed("video", {
            src: flashpath, 
            wmode: 'transparent'
        });
    }
	
    if ($('.intlink').length>0){
        
		$('.intlink').click(function(){
            var loadurl = $(this).attr('href');
					
            //load the content
            //loadcontent(button.parent().parent().parent().parent().children('.pagecontent'));
            $('.current').children('.pagecontent')
            .load(loadurl + " .ajax_content", function(){
				
                //callback
                pageload_callback(button);
            });
            return false;
        });
    }
	
    if($('#hk_news').length > 0){
			
        news_size = $('#innercon').children('div').size();
        m_lft = $('#innercon div').width();
        c_max = m_lft * news_size;
        //alert(c_max);
		
        //animate arrows
        $('#arrowright').click(function(){
            $('#innercon').IcRight(selecteasing, animspeed);		
            return false;	
        });
		
        $('#arrowleft').click(function(){
            $('#innercon').IcLeft(selecteasing, animspeed);		
            return false;
        });
		

    }
	
    if($('#hk_cs').length > 0){
			
        cs_size = $('#innercon').children('div').size();
        m_lft = $('#innercon div').width();
        c_max = m_lft * cs_size - $('#hk_cs').width() + 100;
		
        //animate arrows
        $('#arrowright').click(function(){
            $('#innercon').IcRight(selecteasing, animspeed);		
            return false;	
        });
		
        $('#arrowleft').click(function(){
            $('#innercon').IcLeft(selecteasing, animspeed);		
            return false;
        });
		

    }
	
    if(ajaxhook.children('.icontent').children('a.showhide').length > 0){
        //img controls
        dura = 500;
        moveMax = "-375px";
        movelmax = "-210px";
        movelmin = 20;
        moveMin = 20;
					
        var tvisable = true;
        $('a.showhide').click(function() {
            if(tvisable == true) {
                //show text	
                $(this).parent('.icontent').animate({
									
                    top: moveMax,
                    left: movelmax
									
                },{
                    duration: dura, 
                    queue: false
                });
                $(this).html('show text');
                //$(this).parent().parent().parent().children(".imgcontrol").fadeOut();
								
                tvisable = false;
						
            } else {
                //hide text	
                $(this).parent('.icontent').animate({
																
                    top: moveMin,
                    left: movelmin
									
                },{
                    duration: dura, 
                    queue: false
                });
                $(this).html('hide text');
                //$(this).parent().parent().parent().children(".imgcontrol").fadeIn();
								
                tvisable = true;
					
            }
            return false;
        });
					
					
    }//end if a.showhide
				
    if (ajaxhook.children('.images').length > 0) {
        ajaxhook.children('.images').cycle();
    }//end if #images
}

function CheckEmail(inputemail) {
    AtPos = inputemail.indexOf("@");
    StopPos = inputemail.lastIndexOf(".");

    if (AtPos == -1 || StopPos == -1)
    {
        return false;
    }
    else
    {
        return true;	
    }
}
function hashTohref(){
    hash = window.location.hash;
		
    if (hash) {
        hash=hash.substr(1);
        hash=hash.split(",");
		
        if (!hash[1]){
            hrefend=hash[0]+".htm";
        } else {
			if(!hash[2]){
				hrefend=hash[0]+"/"+hash[1]+".htm";
			}else{
				hrefend=hash[0]+"/"+hash[1]+"/"+hash[2]+".htm";
			}
        }
        var href="media/content/" + hrefend;
        return href;
    } else {
        return false;
    }
	
}


$.fn.pgeConCen = function(){
		Pheight=$(window).height()/2;
		Thisheight=$(this).height()/2;
		Cheight=Pheight-Thisheight;
		$(this).animate({
			"top":Cheight+"px"			
		},{ duration: 200, queue: false });
};
