var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;

function gup(name,myurl)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( myurl );
  if( results == null )
    return "";
  else
    return results[1];
}

(function($){
    $.fn.rand = function(k){
        var b = this,
            n = b.size(),
            k = k ? parseInt(k) : 1;

        // Special cases
        if (k > n) return b.pushStack(b);
        else if (k == 1) return b.filter(":eq(" + Math.floor(Math.random()*n) + ")");

        // Create a randomized copy of the set of elements,
        // using Fisher-Yates sorting
        r = b.get();
        for (var i = 0; i < n - 1; i++) {
            var swap = Math.floor(Math.random() * (n - i)) + i;
            r[swap] = r.splice(i, 1, r[swap])[0];
        }
        r = r.slice(0, k);

        // Finally, filter jQuery stack
        return b.filter(function(i){
            return $.inArray(b.get(i), r) > -1;
        });
    };
})(jQuery);


jQuery(document).ready(function($) {
	
	 // Get the anchor name - show the calendar
	 var anchorName = document.location.hash.substring(1);
	 if(anchorName)
	 {
	 	jQuery("#" + anchorName).show();
	 	jQuery("#homeWhatsOnBlock").hide();
	 	jQuery(".whatson-filter.whatson-calendar").addClass("onit");
	 }
	 
	  
	  headline_count = jQuery("li.concrete").size();
	 // jQuery("li.concrete:eq("+current_headline+")").css('top','5px');
	  jQuery("li.concrete").hide();
	  jQuery("li.concrete:eq("+current_headline+")").fadeIn();
	
	  headline_interval = setInterval(headline_rotate,7000); //time in milliseconds
	  jQuery('#feedme-mouth').hover(function() {
	    clearInterval(headline_interval);
	  }, function() {
	    headline_interval = setInterval(headline_rotate,7000); //time in milliseconds
	    headline_rotate();
	  });
	  
	  /* Concrete slider */
	  scrollersInit();
	  itemGrid();
	  itemGridNoSliding();
	  heroItem();
	  prepareGallery(jQuery(".singleWhatsOnGallery"),{itemWidth:705,itemsOnShow:1,maskWidth:685,maskHeight:435});
	  
	  jQuery(".whatson-filter.whatson-calendar").click(function(){
	  	
	  	if(jQuery(this).hasClass("onit"))
		  		{
		  			jQuery(".whatson-filter").removeClass("onit");
		  			jQuery("#calendar-holder").fadeOut('fast');
		  			jQuery("#homeWhatsOnBlock").fadeIn('fast');
		  		}
		  		else
		  		{
		  			jQuery(".whatson-filter").removeClass("onit");
		  			jQuery("#homeWhatsOnBlock").hide();
		  			jQuery("#calendar-holder").fadeIn('fast');
		  			jQuery(this).toggleClass('onit');
		  		}
	 });
	  
	  
	  if(jQuery("li.allwhatson").length > 0)
	  {
	  	// Kick off timer for flashyness.
	  	
	  	//setInterval ( "randomFlash()", 1000 );
	  	//setInterval ( "randomFlash()", 500 );

	  	
	  	if(jQuery("li.whatsonposts").length > 0)
	 	{
		  	jQuery(".whatson-filter.whatson").click(function(){
		  		if(jQuery(this).hasClass("onit"))
				{
					jQuery(".whatson-filter").removeClass("onit");
					jQuery("li.allwhatson").fadeOut('fast').fadeIn('fast');
			//		  			jQuery("li.allwhatson").fadeIn('fast');
				}
				else
				{
					jQuery(".whatson-filter").removeClass("onit");
					jQuery("li.allwhatson").hide();
					if(jQuery("#calendar-holder").is(":visible"))
					{
						jQuery("#calendar-holder").fadeOut('fast');
						jQuery("#homeWhatsOnBlock").show();
					}
					jQuery("li.whatsonposts").fadeIn('fast');
					
					jQuery(this).toggleClass('onit');
				}
		  	});
	 	}
	 	else
	 		jQuery(".whatson-filter.whatson").hide();
	 	
	 	if(jQuery("li.commingSoonPosts").length > 0)
	  	{	
		  	jQuery(".whatson-filter.coming-soon").click(function(){
		  		if(jQuery(this).hasClass("onit"))
		  		{
		  			jQuery(".whatson-filter").removeClass("onit");
		  			jQuery("li.allwhatson").fadeOut('fast').fadeIn('fast');
		  			//jQuery("li.allwhatson").fadeIn('fast');
		  		}
		  		else
		  		{
		  			jQuery(".whatson-filter").removeClass("onit");
		  			jQuery("li.allwhatson").hide();
		  			{
						jQuery("#calendar-holder").fadeOut('fast');
						jQuery("#homeWhatsOnBlock").show();
					}
		  			jQuery("li.commingSoonPosts").fadeIn('fast');
		  			
		  			jQuery(this).toggleClass('onit');
		  		}
		  	});
	  	}
	  	else
	 		jQuery(".whatson-filter.coming-soon").hide();
	  	
	  	if(jQuery("li.archivedPosts").length > 0)
	  	{
		  	jQuery(".whatson-filter.archived-posts").click(function(){
		  		if(jQuery(this).hasClass("onit"))
		  		{
		  			jQuery(".whatson-filter").removeClass("onit");
		  			jQuery("li.allwhatson").fadeOut('fast').fadeIn('fast');
//		  			jQuery("li.allwhatson").fadeIn('fast');
		  		}
		  		else
		  		{
		  			jQuery(".whatson-filter").removeClass("onit");
		  			jQuery("li.allwhatson").hide();
		  			{
						jQuery("#calendar-holder").fadeOut('fast');
						jQuery("#homeWhatsOnBlock").show();
					}
		  			jQuery("li.archivedPosts").fadeIn('fast');
		  			jQuery(this).toggleClass('onit');
		  		}
		  	});
	  	}
	  	else
	 		jQuery(".whatson-filter.archived-posts").hide();
	  }
	  
	  if(jQuery("div [id ^= 'ngg-gallery-1-']").length > 0)
	  {
	  	jQuery("div [id ^= 'ngg-gallery-']").hide();
	  	jQuery("div [id ^= 'ngg-gallery-1-']").show();
	  	jQuery(".whatson-filter.peeps").toggleClass('onit');
	  }
	  
	  jQuery(".whatson-filter.peeps").click(function(){
	  	if(!jQuery(this).hasClass("onit"))
	  	{
	 		jQuery(".whatson-filter").removeClass("onit");
	  		jQuery("div [id ^= 'ngg-gallery-']").hide();
	  		jQuery("div [id ^= 'ngg-gallery-1-']").fadeIn(1000);	
	  		jQuery(this).addClass("onit");
	  	}
	  });
	  
	  jQuery(".whatson-filter.events").click(function(){
	  	if(!jQuery(this).hasClass("onit"))
	  	{
	  		jQuery(".whatson-filter").removeClass("onit");
	  		jQuery("div [id ^= 'ngg-gallery-']").hide();
	  		jQuery("div [id ^= 'ngg-gallery-2-']").fadeIn(1000);	
	  		jQuery(this).addClass("onit");
	  	}
	  });
	  
	  jQuery(".whatson-filter.sponsors").click(function(){
	  	if(!jQuery(this).hasClass("onit"))
	  	{
	  		jQuery(".whatson-filter").removeClass("onit");
	  		jQuery("div [id ^= 'ngg-gallery-']").hide();
	  		jQuery("div [id ^= 'ngg-gallery-3-']").fadeIn(1000);	
	  		jQuery(this).addClass("onit");
	  	}
	  });
	  
	  jQuery(".whatson-filter.audience").click(function(){
	  	if(!jQuery(this).hasClass("onit"))
	  	{
	  		jQuery(".whatson-filter").removeClass("onit");
	  		jQuery("div [id ^= 'ngg-gallery-']").hide();
	  		jQuery("div [id ^= 'ngg-gallery-4-']").fadeIn(1000);	
	  		jQuery(this).addClass("onit");
	  	}
	  });
	  
	  
	});

function randomFlash()
{
	jQuery("li.whatsonposts:visible").rand(1).show(function()
  	{
  		jQuery(this).children("span.ldr").children("img").each(function(){jQuery(this).hide();}).delay(100);
  		jQuery(this).children("span.ldr").children("img").rand(1).fadeIn('slow');
  			  		
  	});	
}

function headline_rotate() {
	  current_headline = (old_headline + 1) % headline_count; 
	  jQuery("li.concrete:eq(" + old_headline + ")").fadeOut("slow", function() {
	    jQuery(this).fadeOut;
	     jQuery("li.concrete:eq(" + current_headline + ")").fadeIn('slow');  
	 	 old_headline = current_headline
	  });
	 ;
}


function scrollersInit(){prepareScroller(jQuery(".itemGrid"),{itemWidth:320,itemsOnShow:3,maskWidth:940,maskHeight:255})}

function heroItem(){jQuery("#front-gallery-stage").hover(function(){
	jQuery(this).find(".panel").stop().animate({height:"90px"},700,"easeOutBack");
	jQuery(this).find(".text").stop(true,true).fadeIn();
	jQuery(this).find(".whiteButton").stop(true,true).fadeIn();
},
function(){
	jQuery(this).find(".panel").stop().animate({height:"40px"},300,"swing").css("opacity",'0.8');
	jQuery(this).find(".text").stop(true,true).fadeOut(300);
	jQuery(this).find(".whiteButton").stop(true,true).fadeOut(300);})}

function itemGrid(){jQuery(".itemGrid li").hover(function(){
	jQuery(this).find(".panel").stop().animate({height:"90px"},700,"easeOutBack");
	jQuery(this).find(".text").stop(true,true).fadeIn();
	jQuery(this).find(".whiteButton").stop(true,true).fadeIn();
},
function(){
	jQuery(this).find(".panel").stop().animate({height:"40px"},300,"swing").css("opacity",'0.8');
	jQuery(this).find(".text").stop(true,true).fadeOut(300);
	jQuery(this).find(".whiteButton").stop(true,true).fadeOut(300);})}


function itemGridNoSliding(){jQuery(".itemGrid-noslide li").hover(function(){
	jQuery(this).find(".panel").stop().animate({height:"90px"},700,"easeOutBack");
	jQuery(this).find(".text").stop(true,true).fadeIn();
	jQuery(this).find(".whiteButton").stop(true,true).fadeIn();
},
function(){
	jQuery(this).find(".panel").stop().animate({height:"40px"},300,"swing").css("opacity",'0.8');
	jQuery(this).find(".text").stop(true,true).fadeOut(300);
	jQuery(this).find(".whiteButton").stop(true,true).fadeOut(300);})}

function prepareScroller(targetWrapperObj,settings){
	var itemWidth=settings.itemWidth||500;
	var itemsOnShow=settings.itemsOnShow||5;
	var maskWidth=settings.maskWidth||500;
	var maskHeight=settings.maskHeight||100;
	
	targetWrapperObj.each(function(){
		var count=jQuery(this).find("li").size();
		var itemIndex = 1;
		targetWrapperObj.addClass("scrollerActive");
		var scrollWidth=count*itemWidth;
		var increment=itemsOnShow*itemWidth;
		//var increment=itemWidth;
		var currentX=0;
		var maxScroll=-(scrollWidth-increment);
		var list=jQuery(this).find("ul");
		list.wrap('<div class="scrollerMask" style="width:'+maskWidth+'px;height:'+maskHeight+'px;overflow:hidden;position: relative;" />');
		list.css({width:scrollWidth+"px"})
		
//		var startSrc = jQuery(this).find("ul li:nth-child("+itemIndex+")").find("img").attr("src");
//		startSrc = gup("image",startSrc);
//		startSrc = "http://www.basementtheatre.co.nz/wp-content/uploads/image.php?cropratio=2:1&quality=100&width=685&height=435&image=" + startSrc;
//		jQuery("#front-gallery-stage img").attr("src",startSrc);
		
		if(count>itemsOnShow){
			var prevButton=jQuery('<span class="prevButton disabled">Prev</span>');
			var nextButton=jQuery('<span class="nextButton">Next</span>');
			prevButton.click(function()
			{
				
				
				if((currentX+increment)>0)
				{
					var scrollTo=0;
					jQuery(this).addClass("disabled")
				}else{
					itemIndex = itemIndex - 1;
					//jQuery("#front-gallery-stage img").hide();
					var scrollTo=currentX+increment;
//					startSrc = jQuery(this).siblings(".scrollerMask").find("ul li:nth-child("+itemIndex+")").find("img").attr("src");
//					startSrc = gup("image",startSrc);
//					startSrc = "http://www.basementtheatre.co.nz/wp-content/uploads/image.php?cropratio=2:1&quality=100&width=685&height=435&image=" + startSrc;
//					jQuery("#front-gallery-stage img").attr("src",startSrc);
//					jQuery("#front-gallery-stage img").delay(1000).fadeIn('slow');
				}
		
				list.animate({left:scrollTo+"px"},1000,"easeOutCirc");
				currentX=scrollTo;
				nextButton.removeClass("disabled");
				
				
			})
			nextButton.click(function(){
				if((currentX-increment)<maxScroll)
				{
					var scrollTo=maxScroll;
					jQuery(this).addClass("disabled");
				}else
				{
					itemIndex = itemIndex + 1;
					var scrollTo=currentX-increment;
//					jQuery("#front-gallery-stage img").hide();
//					startSrc = jQuery(this).siblings(".scrollerMask").find("ul li:nth-child("+itemIndex+")").find("img").attr("src");
//					startSrc = gup("image",startSrc);
//					startSrc = "http://www.basementtheatre.co.nz/wp-content/uploads/image.php?cropratio=2:1&quality=100&width=685&height=435&image=" + startSrc;			
//					jQuery("#front-gallery-stage img").attr("src",startSrc);
//					jQuery("#front-gallery-stage img").delay(1000).fadeIn('slow');
				}
				list.animate({left:scrollTo+"px"},1000,"easeOutCirc");
				currentX=scrollTo;
				prevButton.removeClass("disabled");
				
								
			})
			
		jQuery(this).prepend(nextButton).prepend(prevButton)}})}

function clickable(){jQuery(".clickable:has(a):not(.disabled),.listWithThumb:has(a)").live("click",function(){var link=jQuery("a",this).eq(0).attr("href");window.location=link;}).hover(function(){if(!jQuery(this).hasClass("disabled")){jQuery(this).addClass("hover").css({"cursor":"pointer"});}},function(){jQuery(this).removeClass("hover").css({"cursor":"default"});})}
function defaultText(){jQuery(".defaultText").unbind("focus,blur").focus(function(){if(jQuery(this).val()==jQuery(this)[0].defaultValue){jQuery(this).val("");}}).blur(function(){if(jQuery(this).val()==""){jQuery(this).val(jQuery(this)[0].defaultValue);}});}


function prepareGallery(targetWrapperObj,settings){
	var itemWidth=settings.itemWidth||500;
	var itemsOnShow=settings.itemsOnShow||1;
	var maskWidth=settings.maskWidth||500;
	var maskHeight=settings.maskHeight||100;
	
	targetWrapperObj.each(function(){
		var count=jQuery(this).find("li").size();
		targetWrapperObj.addClass("scrollerActive");
		var scrollWidth=count*itemWidth;
		var increment=itemsOnShow*itemWidth;
		var currentX=0;
		var maxScroll=-(scrollWidth-increment);
		var list=jQuery(this).find("ul");
		list.wrap('<div class="scrollerMask" style="width:'+maskWidth+'px;height:'+maskHeight+'px;overflow:hidden;position: relative;" />');
		list.css({width:scrollWidth+"px"})

		if(count>itemsOnShow){
			var prevButton=jQuery('<span class="prevButton disabled">Prev</span>');
			var nextButton=jQuery('<span class="nextButton">Next</span>');
			prevButton.click(function()
			{
				if((currentX+increment)>0)
				{
					var scrollTo=0;
					jQuery(this).addClass("disabled")
				}else{
					var scrollTo=currentX+increment;
				}
		
				list.animate({left:scrollTo+"px"},1000,"easeOutCirc");
				currentX=scrollTo;
				nextButton.removeClass("disabled");
			})
			nextButton.click(function(){
				if((currentX-increment)<maxScroll)
				{
					var scrollTo=maxScroll;
					jQuery(this).addClass("disabled");
				}else
				{
					var scrollTo=currentX-increment;
				}
				list.animate({left:scrollTo+"px"},1000,"easeOutCirc");
				currentX=scrollTo;
				prevButton.removeClass("disabled");
			})
			
		jQuery(this).prepend(nextButton).prepend(prevButton)}})}

