/**********************************/
/* copyright headshift 2009		*/
/* 
	authors : 	Stuart Grant 
				* plugin authours  
				credited in file
*/
/**********************************/


$(document).ready(function(){
//search clear
	$("#topSearch, .txt, .txtarea, input").not('input[type=submit]').focus(function() {
		$(this).addClass("focus")
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		$(this).removeClass("focus")
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
    
    


	
//focus	
	$("input, textarea").not('input[type=submit]').focus(function () { 
				 $(this).addClass("focus")
							   });
	$("input, textarea").blur(function () { 
				 $(this).removeClass("focus")
							  });
	
	$("label, .shopping .basket_btn").hover(
      function () {
        $(this).addClass('hover');
      }, 
      function () {
        $(this).removeClass('hover');
      }
    );
	
	
	
	
	
	
 });



$(function () {
			
			
	$('.openBasket').click(function(){
			
			if($('#cart').length > 0){
				
				$('#cart').stop().slideToggle(400);
				
			}else{
				$('<div id="cart" />').prependTo('#outline').show().addClass('loading');	
				$('#cart').load('/magazine/ajax_basket.php #cart_content', 
				function(){
					$(this).hide().removeClass('loading').slideDown(800);
					$('.closeBasket').click(closeBasket);
				});
			}
			return false;

	});	
	
	$('.closeBasket').click(function(){
				$('#cart').slideUp(400);
				$('body .closeBasket').addClass('openBasket').removeClass('closeBasket');
							return false;

	});	
	
	
	
	$(this).keypress(function (escapeBasket) {
            if (escapeBasket.keyCode == 27) {
				$('#cart').slideUp(400);
            }
        });
	
  
});


function closeBasket() {
	
	$('#cart').slideUp(400);
	$('body .closeBasket').addClass('openBasket').removeClass('closeBasket');
				return false;

}



//SIDE NAVIGATION
function initMenus() {
	$('ul.sidenav ul').hide();
	$.each($('ul.sidenav'), function(){
		$('#' + this.id + '.expandfirst ul:first').show();
	});
	$.each($('ul.sidenav'), function(){	$('.open').show(); });
	
	$('ul.sidenav li a').click(
		function() {
			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;

			if($('#' + parent).hasClass('noaccordion')) {
				$(this).next().slideToggle('normal');
				return false;
			}
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				if($('#' + parent).hasClass('collapsible')) {
					$('#' + parent + ' ul:visible').slideUp('normal');
				}
				return false;
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$('#' + parent + ' ul:visible').slideUp('normal');
				checkElement.slideDown('normal');
				return false;
			}
		}
	);
}
$(document).ready(function() {initMenus();});



//footer carousel fade
$(function () {
	
	$('div.items div a').hover(
      function () {
		$(this).find('.tt, .arrow').css({ 'display' : 'none'}).stop().fadeIn('fast');
		}, 
      function () {
		$(this).find('.tt, .arrow').hide();
		}
    );
	
});


//c3 tabs

$(function () {
	var tabContainers = $('div.tabs .tab_content');
	tabContainers.hide().filter(':first').show();
	
	$('div.tabs ul.tabNavigation a').click(function () {
		tabContainers.hide();
		tabContainers.filter(this.hash).show();
		$('div.tabs ul.tabNavigation a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
});




$(function () {		
		
		$('#our_people div.items div:first').addClass('active');
		$('.the_profiles .people_content').hide();
		$('.the_profiles .people_content:first').show();
		
	   $('#our_people div.items div a').click(function () {
			$(this).parents('div.items').find('.active').removeClass('active');
			$(this).addClass('active');
			$('.people_content').hide();
			$('.the_profiles div').stop().filter(this.hash).fadeIn(300);
			return false;
		});
});	






$(function () {
	var tabContainers = $('div.tabs_most .tab_content');
	tabContainers.hide().filter(':first').show();
	
	$('div.tabs_most ul.tabNavigation a').click(function () {
		tabContainers.hide();
		tabContainers.filter(this.hash).show();
		$('div.tabs_most ul.tabNavigation a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
});



//search tabs
$(function () {
	var tabContainers = $('div.search_tabs .tab_content').not('.notab');
	tabContainers.hide().filter(':first').show();
	
	$('div.search_tabs ul.tabNavigation a').click(function () {
		tabContainers.hide();
		tabContainers.filter(this.hash).show();
		$('div.search_tabs ul.tabNavigation a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
});



//article tabs
$(function () {
	var tabContainers = $('div.article_tabs .tab_content')
	tabContainers.hide().filter(':first').show();
	
	$('div.article_tabs ul.tabNavigation a').click(function () {
		tabContainers.hide();
		tabContainers.filter(this.hash).show();
		$('div.article_tabs ul.tabNavigation a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
});


//Home carousels
$(function () {
	if ( $("#issues_foot").length > 0 ) {  
		var n = $('#issues_foot div.items div').size();
			
			$('#issues_foot').serialScroll({
				target:'.carousel_issues',
				items:'div.items div',
				prev:'a.prev_issues',
				next:'a.next_issues',
				start:0, 
				duration:200,
				force:true,
				stop:true,
				lock:false,
				cycle:false, //don't pull back once you reach the end
				jump: false //click on the images to scroll to them
			});
	}
});	



//People carousel
$(function () {
	if ( $("#our_people").length > 0 ) {  
		var n = $('#our_people div.items div').size();
			
			$('#our_people').serialScroll({
				target:'.carousel_people',
				items:'div.items div',
				prev:'.omgleft',
				next:'.omgright',
				start:0, 
				duration:200,
				force:true,
				stop:true,
				lock:false,
				cycle:false, //don't pull back once you reach the end
				jump: false //click on the images to scroll to them
			});
	}
});	





$(function () {		
		 
		 $('.centrepart2 a.img_link img').hover(function(){
			 $(this).addClass('hover');								
		 },function(){
			 $(this).removeClass('hover')							
		 });
			 
		 $('.carousel_article div.items div').each(function() {
			var imgwidth = $(this).find('img').outerWidth();	
			$(this).find('span.centrepart2').css({'width' : imgwidth }); 	
		});
	
});	


//standard carousel functions
$(function () {		
			
		$('div.navi a:first').addClass('active');
		$('div.navi a').append(' | &nbsp;&nbsp;&nbsp;&nbsp; |');
		
		$('div.navi a').click(function(){
				$(this).siblings('a.active').removeClass('active');
				$(this).addClass('active');
		 });
		
		$('a.next').click(function(){
			$(this).parents('.row').find('.navi a.active').not('.navi a:last').removeClass('active').next('a').addClass('active');															$(this).parents('.row').find('#thumbtabs li.active').not('#thumbtabs li:last').removeClass('active').next('li').addClass('active');												
		});
		
		$('a.prev').click(function(){
			$(this).parents('.row').find('.navi a.active').not('.navi a:first').removeClass('active').prev('a').addClass('active');												
			$(this).parents('.row').find('#thumbtabs li.active').not('#thumbtabs li:first').removeClass('active').prev('li').addClass('active');												
		});
		
		//New carousel navigation
		$('#thumbtabs li:first').addClass('active');
		$('.carousel_article div.items div:first').addClass('active');
		$('.carousel_article div.items div').hide();
		$('.carousel_article div.items div:first').show();
		$('.carousel_article').css({ 'overflow' : 'hidden'});
		
	   $('#thumbtabs li').click(function () {
			$(this).siblings('.active').removeClass('active');
			$(this).addClass('active');
			  var thumb_index = $("#thumbtabs li").index(this) + 1;
			$('.carousel_article div.items').find('.active').removeClass('active').hide();
			$('.carousel_article ').find('.items div:nth-child(' + thumb_index + ')').addClass('active').show();
			return false;
		});
		
});		

		

			
			
function article_share(){	
	$(' .action_bar ul ').css({display: 'none'}); 						
		$('.action_bar li').hover(function(){
			 $(this).find('ul:first')							
				.css({visibility: 'visible',display: 'none'})	
				.fadeIn('fast');								
		 },function(){
			 $(this).find('ul:first')							
			 .css({visibility: 'hidden'});						
		 });
}

$(document).ready(function(){							
	 article_share();
});
 
 
// Pagination
var items_per_page = 20; 
var paginated = false;
var section = "News";
var subsection = "";

function historyCallback (hash) {
	if (hash >= 0) {
			  	$.post("/bru_pagination.php", { 'page' : hash, 'section' : section, 'subsection' : subsection }, function(data) {
			$('#items').html(data);
		});	
	}
}

var pageArticlesCallback = function (page_index, jq){
    if ( page_index == 0 ) { 
 		// $(".magazine_layout .items div").show();
        $(".pagination .prev").hide(); 
		//if (paginated) {
			historyCallback(page_index);
		//}
        scrollBack(page_index);
    }else {
		historyCallback(page_index);
		paginated = true;
        scrollBack(page_index);
    }
}

var pageselectCallback = function (page_index, jq){
    $(".magazine_layout .items div").hide();
 	var start = page_index * items_per_page;
    if ( start == 0 ) { 
        $(".magazine_layout .items div:first").show();
        $(".pagination .prev").hide(); 
        var newcount = items_per_page;
        $(".magazine_layout .items div:not(.clear):lt("+newcount+")").show(); 
        scrollBack(page_index);
    }else {
        var newcount = start -1;
        $(".magazine_layout .items div:not(.clear):gt("+newcount+"):lt("+items_per_page+")").show();
        scrollBack(page_index);
        $(".pagination .next .current").parents('.pagination .next').hide(); 
    }
}

var pageSearchCallback = function (page_index, jq){
    $(".search_results > div").hide();
 	var start = page_index * items_per_page;
    if ( start == 0 ) { 
        $(".search_results > div:first").show();
        $(".pagination .prev").hide(); 
        var newcount = items_per_page;
        $(".search_results > div:not(.clear):lt("+newcount+")").show(); 
        scrollBack(page_index);
    }else {
        var newcount = start -1;
        $(".search_results > div:not(.clear):gt("+newcount+"):lt("+items_per_page+")").show();
        scrollBack(page_index);
        $(".pagination .next .current").parents('.pagination .next').hide(); 
    }
}
function initPagination() {
	if ($('#entrycount').size() == 1) {
		items_per_page = 15;
		var num_entries = $('#entrycount').attr('value');
		pageCallback = pageArticlesCallback;
	} else {
		items_per_page = 20;
		var num_entries = $('.magazine_layout .items div:not(.clear)').length;
		pageCallback = pageselectCallback;
		if (num_entries == 0) {
			// try with search
			items_per_page = 15;
			num_entries = $('.search_results > div').length;
			pageCallback = pageSearchCallback;
			$('#search_results_count').html(num_entries);
		}
	}
    // Create pagination element
    $(".pagination").pagination(num_entries, {
		current_page: 0,
		items_per_page: items_per_page,
		link_to: "#",
		num_display_entries: 10,
		next_text: "Next",
		next_show_always: false,
		prev_text: "Prev",
		prev_show_alway: false,
		num_edge_entries: 1,
		ellipse_text:"...",
		callback: pageCallback
    });
    scrollBack();
 }
 function scrollBack(page_index) {
        $(' .pagination li a').click(function(){
            $(this).parents('body').scrollTo( 'SkipToContent', 1, { queue:true }, {easing:'elasout'} );
        });
    }
$(document).ready(function(){    
   if ($('.fastsearch').size() == 0) {
      initPagination();
      scrollBack();
   }
});

$(function(){
  $("#search-results div.row:even").addClass("even");

  qs = window.location.search.substring(1);
  args = qs.split("&");
  for (i=0; i<args.length; i++) {
	key = args[i].split("=")[0];
	if ( key == "fromenquiry") {
		$('h1:first').before("<div class='message green'>Thank you for your enquiry request</div>");
		
	} 
	if ( key == "frominfo") {
		$('h1:first').before("<div class='message green'>Thank you for your request for more information</div>");
	}
  }
}); 
