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


$(function(){
	// Ajax forms
	$('.ajax-form').ajaxForm({
			dataType:  'script'
	});
		
	$('a.increase-quantity').live('click',function(){
     	input_el = $(this).parents('form').find('input.quantity');
			input_el.val(parseInt(input_el.val())+1);
			return false;
  });

	$('a.decrease-quantity').live('click',function(){
      input_el = $(this).parents('form').find('input.quantity');
			input_el.val(parseInt(input_el.val())-1);
			return false;    
  });

})


$(document).ready(function(){
//search clear
	$("#topSearch, .txt, .txtarea, input").not('input[type=submit]').not('input[type=radio]').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]').not('input[type=radio]').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').live('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').live('click',closeBasket);
				});
			}
			return false;

	});	
	
	$('.closeBasket').live('click',function(){
				$('#cart').slideToggle(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 () {
	
	$('.carousel_issues div.items div a').hover(
      function () {
		$(this).find('.tt').css({ 'display' : 'none'}).stop().fadeIn('fast');
		}, 
      function () {
		$(this).find('.tt').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 () {
	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();
});


//event expands

$(function () {
	$('.event .extended').hide();
	$('.event .toggle').addClass('open');
	
	$('.event .abstract').hover(
      function () {
        $(this).addClass('hover');
      }, 
      function () {
        $(this).removeClass('hover');
      }
    );
	
	$('.event .abstract').click(function() {
		$(this).siblings('.extended').slideToggle('fast');
		
		if ( $(this).find('div.toggle').hasClass('open') ) {
				$(this).find('div.toggle').addClass('close');
				$(this).find('div.toggle').removeClass('open');
		} else {
				$(this).find('div.toggle').addClass('open');
				$(this).find('div.toggle').removeClass('close');
		}
			
		return false;
	  });

});


//search zebra striping


$(function () {
	$(".premium_search .row select option:nth-child(odd)").addClass("odd");
});



//article thumbnail scroller

//homepage scrolls

//Home carousels
$(function () {
	if ( $("#home_carousel01").length > 0 ) {  
			var n = $('#home_carousel01 div.items div').size();
			var navi_width = $('#home_carousel01 div.navi').width();
			var navi_link_width = navi_width/n; 
				
			$('#home_carousel01 div.items').find('div').each(function(i) {
				 $('#home_carousel01').find('div.navi').append( '<a href="#"></a>' );
				 $('#home_carousel01 div.navi a').width(navi_link_width);
			   });
			
			$('#home_carousel01').serialScroll({
				target:'.home_carousel01',
				items:'div.items div',
				navigation:'.navi a',
				prev:'#h1_carousel_nav a.prev',
				next:'#h1_carousel_nav a.next',
				//offset:-230, //when scrolling to photo, stop 230 before reaching it (from the left)
				start:0, 
				duration:200,
				force:true,
				stop:true,
				lock:false,
				cycle:false, //don't pull back once you reach the end
				//easing:'easeOutQuart', //use this easing equation for a funny effect
				jump: false //click on the images to scroll to them
			});
	}
});	



//Home carousels
$(function () {
	if ( $("#home_carousel02").length > 0 ) {  
		var n = $('#home_carousel02 div.items div').size();
			var navi_width = $('#home_carousel02 div.navi').width();
			var navi_link_width = navi_width/n; 
				
			$('#home_carousel02 div.items').find('div').each(function(i) {
				 $('#home_carousel02').find('div.navi').append( '<a href="#"></a>' );
				 $('#home_carousel02 div.navi a').width(navi_link_width);
			   });
			
			$('#home_carousel02').serialScroll({
				target:'.home_carousel02',
				items:'div.items div',
				navigation:'.navi a',
				prev:'#h2_carousel_nav a.prev',
				next:'#h2_carousel_nav a.next',
				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
			});
	}
});	

//small carousels

$(function () {
	if ( $("#expert_carousel").length > 0 ) {  
			var n = $('#expert_carousel div.items div').size();
			var navi_width = $('#expert_carousel div.navi').width();
			var navi_link_width = navi_width/n; 
				
			$('#expert_carousel div.items').find('div').each(function(i) {
				 $('#expert_carousel').find('div.navi').append( '<a href="#"></a>' );
				 $('#expert_carousel div.navi a').width(navi_link_width);
			   });
			
			$('#expert_carousel').serialScroll({
				target:'.expert_carousel',
				items:'div.items div',
				navigation:'.navi a',
				prev:'#expert_carousel_nav a.prev',
				next:'#expert_carousel_nav a.next',
				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
			});
	}
});	

//small carousels
$(function () {
	if ( $("#caseStudy_carousel").length > 0 ) {  
			var n = $('#caseStudy_carousel div.items div').size();
			var navi_width = $('#caseStudy_carousel div.navi').width();
			var navi_link_width = navi_width/n; 
				
			$('#caseStudy_carousel div.items').find('div').each(function(i) {
				 $('#caseStudy_carousel').find('div.navi').append( '<a href="#"></a>' );
				 $('#caseStudy_carousel div.navi a').width(navi_link_width);
			   });
			
			$('#caseStudy_carousel').serialScroll({
				target:'.caseStudy_carousel',
				items:'div.items div',
				navigation:'.navi a',
				prev:'#caseStudy_carousel_nav a.prev',
				next:'#caseStudy_carousel_nav a.next',
				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
			});
	}
});	


//footer_carousel
// $(function () {
// 		$('#issues').serialScroll({
// 			target:'.carousel_issues',
// 			items:'div.items div',
// 			prev:'a.prev',
// 			next:'a.next',
// 			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
// 		});
// });	


//c2 carousel
	$(function () {
			var n = $('#c2_carousel div.items .box').size();
			var navi_width = $('#c2_carousel div.navi').width();
			var navi_link_width = navi_width/n; 
		
			
			$('#c2_carousel div.items').find('div').each(function(i) {
				 $('#c2_carousel').find('div.navi').append( '<a href="#"></a>' );
				 $('#c2_carousel div.navi a').width(navi_link_width);
			   });
			
			// $('#c2_carousel').serialScroll({
			// 	target:'.carousel_contagious',
			// 	items:'div.items .box',
			// 	navigation:'.navi a',
			// 	prev:'#c2_nav a.prev',
			// 	next:'#c2_nav a.next',
			// 	start:0, 
			// 	duration:200,
			// 	force:true,
			// 	stop:true,
			// 	lock:false,
			// 	cycle:false, //don't pull back once you reach the end
			// 	jump: true //click on the images to scroll to them
			// });
	});

//Article Carousel w/ thumbnails


	$(function () {
			var n = $('#carousel01 div.items div').size();
			var navi_width = $('#carousel01 div.navi').width();
			var navi_link_width = navi_width/n;
						// 
						//  $('#carousel01').serialScroll({
						// 	target:'.carousel_article',
						// 	items:'div.items div',
						// 	navigation: '#thumbtabs li a',
						// 	prev:'#nav01 a.prev',
						// 	next:'#nav01 a.next',
						// 	start:0,
						// 	duration:200,
						// 	force:true,
						// 	stop:true,
						// 	lock:false,
						// 	cycle:false, //don't pull back once you reach the end
						// 	jump: true //click on the images to scroll to them
						// });
						// 	
			
			$('#carousel01 div.items').find('div').each(function(i) {
				 $(this).attr({ id: 'itemNumber' + i });
				 $('#carousel01').find('div.navi').append( '<a href="#itemNumber'+i+'"></a>' );
				 $('#carousel01 div.navi a').width(navi_link_width);
				 $('#carousel01 div.navi a').click(function(j){
					var n = parseInt($(j.target).attr('href').replace('#itemNumber', '')) +1;
					$('#carousel01 div.carousel_article').stop().scrollTo( 'div:eq('+ n +')', 800 );
					return false;
				 }); 
			});
		
			//thumbnail navigation
		   $('#thumbtabs li').click(function () {
				$(this).siblings('.active').removeClass('active');
				$(this).addClass('active');
				  var thumb_index = $("#thumbtabs li").index(this) + 1;
				$(this).parents('.carousel_content').find('.navi a.active').removeClass('active');
				$(this).parents('.carousel_content').find('.navi a:nth-child(' + thumb_index + ')').addClass('active');
			});
			 $('.carousel_content .navi a').click(function () {
				$(this).siblings('.active').removeClass('active');
				$(this).addClass('active');
				  var link_index = $(".carousel_content .navi a").index(this) + 1;
				$(this).parents('.carousel_content').find('#thumbtabs li.active').removeClass('active');
				$(this).parents('.carousel_content').find('#thumbtabs li:nth-child(' + link_index + ')').addClass('active');
			});
			 
			
	
	});	

//Article related  carousel

	$(function () {
			var n = $('#article_carousel_related div.items div').size();
			var navi_width = $('#article_carousel_related div.navi').width();
			var navi_link_width = navi_width/n; 
		
			
			$('#article_carousel_related div.items').find('div').each(function(i) {
				 $('#article_carousel_related').find('div.navi').append( '<a href="#"></a>' );
				 $('#article_carousel_related div.navi a').width(navi_link_width);
			   });
			
			// $('#article_carousel_related').serialScroll({
			// 	target:'.article_carousel_related',
			// 	items:'div.items div',
			// 	navigation:'.navi a',
			// 	prev:'#related_carousel_nav a.prev',
			// 	next:'#related_carousel_nav a.next',
			// 	start:0, 
			// 	duration:200,
			// 	force:true,
			// 	stop:true,
			// 	lock:false,
			// 	cycle:false, //don't pull back once you reach the end
			// 	jump: true //click on the images to scroll to them
			// });
	});	

//Article See Alsocarousel
	$(function () {
			var n = $('#article_seeAlso div.items div').size();
			var navi_width = $('#article_seeAlso div.navi').width();
			var navi_link_width = navi_width/n; 
		
			
			$('#article_seeAlso div.items').find('div').each(function(i) {
				 $('#article_seeAlso').find('div.navi').append( '<a href="#"></a>' );
				 $('#article_carousel_related div.navi a').width(navi_link_width);
			   });
			
			// $('#article_seeAlso').serialScroll({
			// 	target:'.article_seeAlso',
			// 	items:'div.items div',
			// 	navigation:'.navi a',
			// 	prev:'#seealso_carousel_nav a.prev',
			// 	next:'#seealso_carousel_nav a.next',
			// 	start:0, 
			// 	duration:200,
			// 	force:true,
			// 	stop:true,
			// 	lock:false,
			// 	cycle:false, //don't pull back once you reach the end
			// 	jump: true //click on the images to scroll to them
			// });
	});	

//standard carousel functions
$(function () {		
		/*$('.scrollable').mousewheel(
			function (event, delta) {
    		this.scrollLeft -= (delta * 30);
			$('body').unmousewheel();
		  });*/
		
		
		$('div.navi a:first').addClass('active');
		$('div.navi a').append(' | &nbsp;&nbsp;&nbsp;&nbsp; |');
		$('#thumbtabs li:first').addClass('active');

		$('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');												
		});
		
		 //center width fix
			 
		 $('.carousel_article div.items div').each(function() {
			var imgwidth = $(this).find('img').width() +4;															
			$(this).find('span.centrepart1, span.centrepart2').css({'width' : imgwidth }); 	
		});
	
	
		
});	
			
			
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();
});
 
 
 

