/* ========================================================
 * bootstrap-tabs.js v1.4.0
 * http://twitter.github.com/bootstrap/javascript.html#tabs
 * ========================================================
 * Copyright 2011 Twitter, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ======================================================== */


!function( $ ){

  "use strict"

  function activate ( element, container ) {
    container
      .find('> .active')
      .removeClass('active')
      .find('> .dropdown-menu > .active')
      .removeClass('active')

    element.addClass('active')

    if ( element.parent('.dropdown-menu') ) {
      element.closest('li.dropdown').addClass('active')
    }
  }

  function tab( e ) {
    var $this = $(this)
      , $ul = $this.closest('ul:not(.dropdown-menu)')
      , href = $this.attr('href')
      , previous
      , $href

    if ( /^#\w+/.test(href) ) {
      e.preventDefault()

      if ( $this.parent('li').hasClass('active') ) {
        return
      }

      previous = $ul.find('.active a').last()[0]
      $href = $(href)

      activate($this.parent('li'), $ul)
      activate($href, $href.parent())

      $this.trigger({
        type: 'change'
      , relatedTarget: previous
      })
    }
  }


 /* TABS/PILLS PLUGIN DEFINITION
  * ============================ */

  $.fn.tabs = $.fn.pills = function ( selector ) {
    return this.each(function () {
      $(this).delegate(selector || '.tabs li > a, .pills > li > a', 'click', tab)
    })
  }

  $(document).ready(function () {
    $('body').tabs('ul[data-tabs] li > a, ul[data-pills] > li > a')
  })

}( window.jQuery || window.ender );

$(document).ready(function(){


		

		$("#gal").click(function() {
 $.fancybox([
             'media/pictures/project/project-1.jpg',
             'media/pictures/project/project-2.jpg',
             'media/pictures/project/project-3.jpg',
             'media/pictures/project/project-4.jpg',
             
             ], {
             'padding' : 0,
             'transitionIn' : 'none',
             'transitionOut' : 'none',
             'type' : 'image',
             'changeFade' : 0
        });
    }); 
		
		/*$("#special").hover(function(){
		
		$("#baf").addClass("pulse");
		
		},function(){$("#baf").removeClass("pulse");} 
		
		);*/
		
		$("#various3, #various4, #various5").fancybox({
			'width'				: 400,
			'height'			: 400,
			'autoScale'			: false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'margin'			: 0,
			'padding'			: 0,
			'type'				: 'iframe'
		});


		var theInt = null;
		var $crosslink, $navthumb;
		var curclicked = 0;
		
		theInterval = function(cur){
			clearInterval(theInt);
			
			if( typeof cur != 'undefined' )
				curclicked = cur;
			
			$crosslink.removeClass("active-thumb");
			$navthumb.eq(curclicked).parent().addClass("active-thumb");
				$(".stripNav ul li a").eq(curclicked).trigger('click');
			
			theInt = setInterval(function(){
				$crosslink.removeClass("active-thumb");
				$navthumb.eq(curclicked).parent().addClass("active-thumb");
				$(".stripNav ul li a").eq(curclicked).trigger('click');
				curclicked++;
				if( 6 == curclicked )
					curclicked = 0;
				
			}, 3000);
		};
		
		$(function(){
			
			$("#main-photo-slider").codaSlider();
			
			$navthumb = $(".nav-thumb");
			$crosslink = $(".cross-link");
			
			$navthumb
			.click(function() {
				var $this = $(this);
				theInterval($this.parent().attr('href').slice(1) - 1);
				return false;
			});
			
			theInterval();
		});

	$(".wrapper a").fancybox();



	$('blockquote').prepend(" <span class='bqstart'>&#8220;</span> ");
	$('blockquote').append(" <span>&nbsp;</span><span class='bqend'>&#8221;</span> ");
	  
	//INPUT CLEAR
	
	$(window).bind("load", function(){
			//clear form inputs (add & uses rel attributes for comparison)
			$('.inputClear').live('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($){$.fn.checkEmail=function(){var b;this.each(function(){var c=$(this).val();b=c.indexOf("@")==-1||c.lastIndexOf(".")==-1?false:true});return b}})(jQuery);
	
	//NEWSLETTER SIGNUP
		

	$('#sign_up_form').submit(function(){
		chkeml = $('#sign_up_email input').checkEmail();
		fname = $('#sign_up_fname input').val();
		sname = $('#sign_up_sname input').val();
		emlval = $('#sign_up_email input').val();
		var message='';
		if(chkeml)		
		{
			$.ajax({
				type: "GET",
				url:"media/plugs/baf/process.php",
				data: "emailadd=" + emlval + "&fname=" + fname + "&sname=" + sname,
				success: function(data){
					switch(data){
						case "failed":
							message="Error adding to the database";
							$('#newsletter_return').html(message).slideDown();
							$('#newsletter_return').delay(1900).slideUp();
						break;
						case "success":
							message="Thank you. Your email has been added to our newsletter database and your voucher has been sent to the provided email address.";
							$('#newsletter_return').html(message).slideDown();
							$('#newsletter_return').delay(1900).slideUp();
							$('#sign_up_fname input, #sign_up_sname input, #sign_up_email input').val("");
							
							
						break;
						case "email":
							message="Your email address is already registered";
							$('#newsletter_return').html(message).slideDown();
							$('#newsletter_return').delay(1900).slideUp();
						break;
					}
				}
			});
			

			/*
			$.ajax({
				type: "POST",
				url: "ajax.php",
				data: "i="+$('#sign_up_email input').val(),
				success: function(data){
					$('#voucher').children('a').attr('href','ext_voucher.php?i='+encodeURIComponent(data)).end().show();
				}
			});*/
			

		}
		else
		{
			message="Please supply a valid email address.";
			$('#newsletter_return').html(message).slideDown();
			$('#newsletter_return').delay(1900).slideUp();
		}
		return false;
	});	

	$('#recommend_add a').click(function(){
		 $('#recommend_elements .recommend_form_email').each(function(i){
			amm = i;
		 });
		 if(amm < 4){
			storeval = $('#recommend_elements .recommend_form_email:first-child input').val();
			$('#recommend_elements .recommend_form_email:first-child input').val("email address");
			clone = $('#recommend_elements .recommend_form_email:first-child').clone();
			$('#recommend_elements').append(clone);
			if(amm == 3){
				$('#recommend_add a').css({'color': 'grey', 'cursor': 'default'});
			}
			$('#recommend_elements .recommend_form_email:first-child input').val(storeval);
		}
		if(amm > 0){
			$('.remove_form_email').show();
		} else {
			$('.remove_form_email').hide();
		}
		
		$('.remove_form_email').remove();
		
		$('#recommend_elements .recommend_form_email').each(function(i){
			$(this).append('<div class="remove_form_email"><a href="#">x</a></div>');
		 });
		 
		 $('#recommend_elements .recommend_form_email:first-child .remove_form_email').remove();
		 
		return false;
	});
	
	$('.remove_form_email a').live('click', function(){
		$('#recommend_add a').css({'color': 'blue', 'cursor': 'pointer'});
		$(this).parent().parent().remove();
		return false;
	});
	
	$('#recommend_form').submit(function(){
		var emladdrs='';
		$('.recommend_form_email').each(function(){
			gogo = 0;
			chkemail = $(this).children('input').checkEmail();
			if(chkemail){
				gogo = 1;
			}
			emladdrs = emladdrs + $(this).children('input').val() + '{SEP}'
		});
		if(gogo){
				$.ajax({
				type: "GET",
				url:"media/plugs/raf/process.php",
				data: "emailaddrs=" + emladdrs,
				success: function(data){
					switch(data){
						case "failed":
							message="Error adding to the database";
							$('#recommend_return').html(message).slideDown();
							$('#recommend_return').delay(1900).slideUp();
						break;
						case "success":
							message="Thank you. You have successfuly recommended your friend(s).";
							$('#recommend_return').html(message).slideDown();
							$('#recommend_return').delay(1900).slideUp();
							$('.recommend_form_email').val("");
						break;
						case "email":
							message="One or more of your friends has already been recommended.";
							$('#recommend_return').html(message).slideDown();
							$('#recommend_return').delay(1900).slideUp();
						break;
					}
				}
			});
		} else {
			message="Please ensure that all entries are a valid email address.";
			$('#recommend_return').html(message).slideDown();
			$('#recommend_return').delay(1900).slideUp();
		}
		return false;
	});
	
	//EVENTS 
	$('#e_items').rotary({
		auto: {interval:5000},
		changer: 'div',
		onstage: {left:0},
		offstage: {left:-290},
		ext: {
			queue:false,
			duration:500,
			easing:'swing'
		},
		control: {
			ind: {
				element: $('#events_control li a'),
				binding: 'click',
				onclass: 'cur'
			}
		}
	});
	
	//EVENTS 
	$('#e_items2').rotary({
		auto: {interval:5000},
		changer: 'div',
		onstage: {left:0},
		offstage: {left:-294},
		ext: {
			queue:false,
			duration:500,
			easing:'swing'
		},
		control: {
			ind: {
				element: $('#events_control2 li a'),
				binding: 'click',
				onclass: 'cur'
			}
		}
	});
	

	//add scrollable
	$('#thumbs').scrollable({
		circular: true,
		next: ".t_next",
		prev: ".t_prev"
		/*mousewheel: true*/
	})
	.autoscroll({
		interval: 3000		
	});
	
	
	
	//access jQuery Tools API & globalise gallery variable
	var thumbs=$('#thumbs').data("scrollable");
	var gallery;
	var g_event=0;
	var ti;
	var gallery_images="";
	
	
	//thumbs click event
	$('#thumbs .items>div div').bind('click',function(){
		ti = $('#thumbs .items>div div').index(this);
		
		//if there is no gallery ...
		if(!g_event)
		{
			
			//generate code for large gallery
			$('#thumbs img').each(function(){
				gallery_images+="<div><img width=\"952px\" height=\"492px\" src=\""+$(this).attr('rel')+"\" /><span></span></div>";
			});
			
			// ...crate it
			$('#gallery').append('<div class="scrollable"><div class="items">'+
				gallery_images
			+'</div></div><a class="g_prev" href="#">&lt;</a> <a class="g_next" href="#">&gt;</a>');
			
			// and add scrollable
			$('#gallery').scrollable({
				circular: true,
				next: ".g_next",
				prev: ".g_prev"
			});
			
			//set API variable
			gallery=$('#gallery').data("scrollable");
			
			//set click event for zoom out
			$('#gallery .items div').bind('click', function(){
				$('#gallery .scrollable').slideUp();
				$('#thumbs .scrollable').slideDown();
				$('#hc').slideDown();
				$('#gallery>a').fadeOut();
				$('#thumbs>a').fadeIn();
			});
			g_event=1;
		}
		
		//slide and change gallery image		
		$('#gallery .scrollable').slideDown().css({height: 500});
		$('#gallery>a').fadeIn();
		$('#thumbs>a').fadeOut();
		
		$('#hc').slideUp();
		
		$('#thumbs .scrollable').slideUp();
		gallery.seekTo(ti, 0);
		
	});
	

	$('.t_prev, .t_next,').click(function(){
		return false;
	});
	
	$('.g_prev, .g_next,').live('click', function(){
		return false;
	});	
	
	//$('#thumbs .items>div div').trigger('click');
	
	$.fn.pgeConCen = function(){
		Pheight=$(window).height()/2;
		Thisheight=$(this).height()/2;
		Cheight=Pheight-Thisheight;
		$(this).animate({
			"top":Cheight+"px"			
		},{ duration: 200, queue: false });
	};
	
	$('.pgewrapper').hide();
	$('.pgemask').css({opacity: 0.8});
		
	$('#book').click(function(){
		$('.pgewrapper').show();
		$('.pgecontainer').pgeConCen();	
	});
						   
	$(window).bind("resize",function(){
		$('.pgecontainer').pgeConCen();					   
	});
	
	$('.pgemask').click(function(){
		$('.pgewrapper').hide();				
	});
	
	$(window).bind("resize",function(){
		$('.pgecontainer').pgeConCen();					   
	});
	
});
