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(){

	/* popup */

	
	if($('.popup').length) {

		//settings
		var s = {
			element: $('.popup div'),
			shownFor: 10000,
			shownIn: 2000,
			height: 305,
			width: 469,
			bottom: 0
		};

		//set initial state
		$('.popup').css({height:s.height});
		
		s.element.css({
			height: s.height,
			width: s.width,
			bottom: s.height-(s.height*2)
		}).parent().show();
		
		//animations
		var e = setTimeout(function(){
			s.element.animate({bottom: s.bottom});
				var hovered = false;
				s.element.hover(function(){
					hovered = true;
				},function(){
					hovered = false;
				});
			

			

			$('#popup-close').bind('click',function(){
				s.element.animate({bottom: '-'+s.height},{complete: function(){$('.popup').hide();}});
				return false;
			});
		}, s.shownIn);
		
	}



	// if($('#popup').length){
		
	// 	$(window).bind('resize load',function(){
	// 		$('#popup-content').css('top',($(window).height()-278)/2);
	// 	});
		
	// 	$('#popup-overlay, #popup-close').bind('click', function(){
	// 		$('#popup').fadeOut();
	// 		return false;
	// 	});

	// }
				   
						   
						   
	
	/* TIPSY */
	
	$("a,img,h1,h2,h3,input,span").each(function(){if($(this).attr("title").length>0){$(this).tipsy({opacity:1,html:true,gravity:"s"})}});

	
	$('.pgewrapper').hide();
	$('.pgemask').css({opacity: 0.8});


	

			
	/* OFFERS */
	
	
	///////////////////////
			
			
						   
		
	$('#book-temp').fancybox();	
	$('#book-table').fancybox();
	$('#book-room').fancybox();					   
	
	/*ie = (function() {
		// borrowed from Padolsey
	    var undef,
	        v = 3,
	        div = document.createElement('div'),
	        all = div.getElementsByTagName('i');
	    
	    while (
	        div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
	        all[0]
	    );
	    
	    return v > 4 ? v : undef;
	    
	}());*/
	
						   
	function lightb(ind){
		$('.pgecontainer>div').hide().eq(ind).show();							
									
		$('.pgewrapper').show();
		$('.pgecontainer').pgeConCen();	
	}
						   
	$('.pgewrapper').hide();
	$('.pgemask').css({opacity: 0.8});

	$('#becomeafriend').click(function(){lightb(0);});
	$('#book-table').click(function(){lightb(1);});
	$('#book-room').click(function(){lightb(2);});
	
				   
	$(window).bind("resize",function(){
		$('.pgecontainer').pgeConCen();					   
	});
	
	$('.pgemask').click(function(){
		$('.pgewrapper').hide();				
	});	
	
	
	
	
						  
$('#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 = 648;
	var s_minwidth = 30;



	//startup	
	nav_ul.fadeOut();
	//pagecontent.hide();
	//expand(section.eq(0));
	
	
//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));

            var load_content = function(file){
            	$('.current').children('.pagecontent')
				.load("media/content/" + file + " .ajax_content", function(){
					pageload_callback(section.eq(0).children('.nav').children('ul').children().children('a'));
				});
            }

			switch (window.location.hash){
				case "#gift-vouchers":
					load_content("home/vouchers.htm");
				break;

				case "#become-a-friend":
					load_content("home/becomeafriend.htm");
				break;

				case "#civil-ceremonies":
					load_content("news/offer-3.htm");
				break;

				case "#returns":
					load_content("home/returns.htm");
				break;

				case "#terms":
					load_content("home/terms.htm");
				break;

				case "#privacy":
					load_content("home/privacy.htm");
				break;

				default:
					load_content("home/default.htm");
				break;
			}


			
        } else {
            expand(ref.parent().parent().parent().parent());
			ref.parent().parent().parent().parent().children('.pagecontent')
				.load(checkhash + " .ajax_content", function(){
					//callback
					pageload_callback(ref);
				});
        }
		
    }
	
	
	
	//events
	section_name.click(function(){
		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: "top right"});
			
			$('.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 -300px"});
			
			if(element.children().children('ul').children('li').length<2){ 
				element.children().children('ul').hide();
			} else {
				element.children().children('ul').fadeIn();
			}
			

			pageload(element.children('.nav').children('.section_name'));
			
		}
	}
	
nav.children().children().children().click(function(){
	pageload($(this));
	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);
				});
			}
}






/*

	PAGE CALLBACK

*/

/* WORLD PAY */

	$('.rps_wp').submit(function(){
		wplink="https://select.worldpay.com/wcc/purchase?instId=273665&cartId=Box Tree%20Restaurant%20vouchers"; wplink=wplink+"&amount=120&currency=GBP&desc=Practical%20Solutions%20conference&testMode=0";
		window.open(wplink);
		wplink="";
	});
	

is_voucher = 0;

$('.intlink').live('click',function(){
    var loadurl = $(this).attr('href');

    if($(this).attr('id') === 'gift_vouchers')
    {
    	is_voucher = 1;
    }
			
    //load the content
    //loadcontent(button.parent().parent().parent().parent().children('.pagecontent'));

    $('.current').children('.pagecontent')
    .load(loadurl + " .ajax_content", function(){
        //callback
        pageload_callback($('.current .nav').eq(0).children('ul').children('li').children('a'));

    });
    return false;
});



cbi = 0;

function pageload_callback(button){


	var ajaxhook = button.parent().parent().parent().parent().children('.pagecontent').children('.ajax_content');

	
	if($('.fbox').length){
		$('.fbox').fancybox();
	}
	
	
	if($('#offer1').length){
		$('#offer1').submit(function(){
				
				email=$('#offer1_email').val();
				
				restaurant='Box Tree';
				
				$('#offer1_email').val('');
												
				$.ajax({
					type: "get",
					url: 'plugs/baf/offers_process.php',
					data: 'email=' + email + '&restaurant=' + restaurant,
					success: function(html){
						if(html=='success'){
							$('#offer_return1').html('Thank you, your e-mail voucher has been sent.');
						} else {
							$('#offer_return1').html('There was an issue with your email address');
						}
					}
				});	
				
				return false;
			});
	}
	
	//INPUT CLEAR

//clear form inputs (add & uses rel attributes for comparison)
$('.inputClear').focus(function(){
	if(!$(this).attr('rel')) {
		$(this).attr('rel',$(this).val()).val('');
	} else {
		if($(this).val()==$(this).attr('rel')) $(this).val('');
	}
}).blur(function(){
   if($(this).val()=="") $(this).val($(this).attr('rel'));
});



/*
	CHKEML
*/
(function(a){a.fn.checkEmail=function(){var b;this.each(function(){var c=a(this).val();b=c.indexOf("@")==-1||c.lastIndexOf(".")==-1?false:true});return b}})(jQuery);

	
	
	$('#baf').submit(function(){
				chkeml = $('#baf_eml').checkEmail();
				email=$('#baf_eml').val();
				name=$('#baf_name').val();
				restaurant='Box Tree';
				if(chkeml == true){
				$.ajax({
					type: "get",
					url: 'plugs/baf/process.php',
					data: 'email=' + email + '&name=' + name,
					success: function(html){
						if(html=='success'){
							$('#baf_return').html('Thank you, your e-mail has been added.');
						} else {
							$('#baf_return').html('Please check your email address and try again.');
						}
					}
				});	
				} else {
					$('#baf_return').html('Please check your email address and try again.');
				}
				$('#baf_return').animate({height: 20}, 500);
				$('#baf_return').delay(2500).animate({height: 0}, 500);
				return false;
			});
	
	//MENUS FBOX
	$('#menu1_fb').fancybox();	
	$('#menu2_fb').fancybox();	
	$('#menu3_fb').fancybox();
	$('#menu4_fb').fancybox();
	$('#menu5_fb').fancybox();
	$('#menu6_fb').fancybox();
	$('#menu7_fb').fancybox();
	
	//OFFERS FBOX
	$('#offer1_fb').fancybox();	
	$('#offer2_fb').fancybox();	
	$('#offer3_fb').fancybox();
	$('#offer4_fb').fancybox();
	
	//XMAS FBOX
	$('#xmas1_fb').fancybox();	
	$('#xmas2_fb').fancybox();	
	$('#xmas3_fb').fancybox();
	$('#xmas4_fb').fancybox();	
	$('#xmas5_fb').fancybox();	
	$('#xmas6_fb').fancybox();
	
	
	/* video */
	
	
	/*IE DETECTION*/
	var ie=function(){for(var a=3,b=document.createElement("div"),c=b.getElementsByTagName("i");b.innerHTML="<!--[if gt IE "+ ++a+"]><i></i><![endif]--\>",c[0];);return a>4?a:void 0}();
	
	
	vsettings = {
		elem: $('#show-video'),
		container: $('#video1'),
		video_dir: 'media/video/',
		flash_dir: 'media/flash/',
		video_name: 'Kings Arms',
		site_root: 'http://www.wheelerskingsarms.com/',
		width: 768,
		height: 432
	};
	
	
	if(vsettings.elem.length){
		
		if(ie<8){
			//Internet Explorer 6/7 Href FAILS!
			vsettings.elem.attr('href', "#" + vsettings.elem.attr('href').split('#')[1]);
		}
		
		vsettings.elem.fancybox({
            overlayOpacity:0.8,
            overlayColor:'#000',
            padding:0,
            autoDimensions:false,
            height:vsettings.height,
            width:vsettings.width,
			scrolling: 'no',
			onComplete: function(){			
				vsettings.container.html("<video controls width=" + vsettings.width + " height="+ vsettings.height +" preload autoplay>\
					<source src=\"" + vsettings.video_dir + vsettings.video_name + ".mp4\" type=\"video/mp4\">\
					<source src=\"" + vsettings.video_dir + vsettings.video_name + ".ogg.php\" type=\"video/ogg\">\
				</video>");
				
				if(ie<9) {
					vsettings.container.flashembed({
						src: vsettings.flash_dir + 'flv-player.swf',
						width: 768,
						height: 432,
						wmode: 'transparent'
					},{
						flv: vsettings.site_root + vsettings.video_dir + vsettings.video_name + '.mp4',
						configxml: vsettings.flash_dir + 'config.xml'
					});
					
					
				}
				
			}
		});
	}
	
	
	/* end Video */
	
	
	/* BAF FANCYBOX */
	/*if($('#becomeafriend').length){
		$('#becomeafriend').fancyZoom();
	}*/

	$('.pgecontainer #baf').live('submit',function(){
		
		$('#baf input[type="submit"]').css({display:'none'});
		
		expVals="";
		$('#baf input[type="text"]').each(function(){expVals=expVals+$(this).val()+"{spl}";});		
		$.ajax({ 
		type: "POST", 
		url: "media/php/baf/voucher.php", 
		data: "func=voucherSubmit&vals="+expVals, 
		success: function(data){
			$('#baf #feedback').html(data);
			$('#baf input[type="submit"]').css({display:'block'});
		}
		});
		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).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;}
				

				if($('.pdResSel').length){
					restaurant_id=$('.pdResSel').val();
					restaurant=restaurants_arr[$('.pdResSel').val()];
					dd=1;
				} else {
					restaurant=$('#restaurant_name').val();
					restaurant_id=$('#restaurant_id').val();
					dd=0;
				}
				
				//submit form vals
				if(postAjax){
					 $.ajax({ 
					   type: "POST", 
					   url: "media/php/process.php", 
					   data: "func=cFrmReq&frmIndx="+fncName+"&frmVals="+cVals+"&restaurant_id=" + restaurant_id + "&restaurant_name=" + restaurant + "&dd=" + dd, 
					   success: function(data){
						   if(data=="success"){$('.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;
		});
	}
	
	/***/
	


	if($('.mar').length>0) {
		$('.mar').click(function(){
			$('.pgewrapper').show();
			$('.pgecontainer').pgeConCen();	
		});
	}
	
	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;
		});
	}

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 ($('#hk_switch').length>0){
				$('#hk_switch').click(function(){
				var loadurl = $('#hk_switch').attr('href');
					
				//load the content
				//loadcontent(button.parent().parent().parent().parent().children('.pagecontent'));
				$('#hk_switch').parent().parent().parent().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;
		});
		

	}
	
	/* REVIEWS PAGE FUNCTIONS */
	if($('.revLst').length>0){
		
		$('.revC').css({top:500,display:'block'}).eq(0).css({top:0});
		$('.revMask').animate({opacity:.8},{duration:0,queue:false});
		revSel=0;
		
		$('.revLst').css({zIndex:60}).children('li').children('a').click(function(){
			thsId=$('.revLst li a').index(this);
			secCount=$('.revC').size()-1;
			if(revSel!=thsId){
				aniIn=thsId
				$('.revC').each(function(){
					if($(this).css('top')=="0px"){
						thsIndx=$('.revC').index(this);
						aniO=thsIndx;
					}
				});
				revSel=thsId;
				$('.revC').eq(aniIn).css({zIndex:5}).animate({top:0},{duration:500,queue:false});
				$('.revC').eq(aniO).css({zIndex:0}).animate({top:-500},500,function(){$(this).css({top:500})});

			}
		});
		
	}
	

	if(ajaxhook.children('.icontent').children('a.showhide').length > 0){
					//img controls
					
					ics = {
						offstage: {
							top: 0,
							left: 0,
							height: 80,
							width: 650
						},
						onstage: {
							top: 20,
							left: 20,
							height: 450,
							width: 500
						},
						ext: {
							queue: false,
							duration: 500,
							easing: 'swing'
						}
					};
					
					cs = {
						offstage: {
							height: 36
						},
						onstage: {
							height: 394
						}
					}

					if(is_voucher === 1)
					{
						ics.onstage.width = 610;
					}

						$('a.showhide').toggle(function() {


								$(this).parent('.icontent').animate({
									top: ics.onstage.top,
									left: ics.onstage.left,
									height: ics.onstage.height,
									width: ics.onstage.width
								},{queue: ics.ext.queue,duration: ics.ext.duration,easing: ics.ext.easing})
								.end()
								.siblings('.icontent_position')
								.animate({height: cs.onstage.height });
								
								$(this).html('hide text');
	
							}, function(){
							
								$(this).parent('.icontent').animate({								
									top: ics.offstage.top,
									left: ics.offstage.left,
									height: ics.offstage.height,
									width: ics.offstage.width								
								},{queue: ics.ext.queue,duration: ics.ext.duration,easing: ics.ext.easing})
								.end()
								.siblings('.icontent_position')
								.animate({height: cs.offstage.height });
								
								$(this).html('show text');

							return false;
						});
					
					
				}//end if a.showhide

				
				
				if (ajaxhook.children('.images').length > 0) {
					ajaxhook.children('.images').cycle();
				}//end if #images



				//time delay the text appearing
				
				setTimeout(function(){
					if($('.icontent').height() == 450 || $('.icontent').height() == 80) {
						$('a.showhide').trigger('click');
					}
				},750);
				
	}

function CheckEmail(inputemail) {
	AtPos = inputemail.indexOf("@");
	StopPos = inputemail.lastIndexOf(".");

	if (AtPos == -1 || StopPos == -1)
	{
		return false;
	}
	else
	{
		return true;	
	}

}

$.fn.pgeConCen = function(){
		Pheight=$(window).height()/2;
		Thisheight=$(this).height()/2;
		Cheight=Pheight-Thisheight;
		$(this).animate({
			"top":Cheight+"px"			
		},{ duration: 200, queue: false });
};
function hashTohref(){
    hash = window.location.hash;
		
    if (hash) {
        hash=hash.substr(1);
        hash=hash.split(",");
		
        if (!hash[1]){
            hrefend=hash[0]+".htm";
        } else {
            hrefend=hash[0]+"/"+hash[1]+".htm";
        }
        var href="media/content/" + hrefend;
        return href;
    } else {
        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);

/***/
