/* Author: 
Stuart Haston , NorthColur Ltd, 2011
*/


$(document).ready(function() {
	
	if($("#evsearch").length){
		$('#evsearch').liveSearch({ajaxURL: '_search.asp?searchterm='});
	}
	

	$().jSnow({
	         // vSize: if specified, the snow will appear only in the first top vSize pixels
	         // 		    fadeAway: the snow flakes will fade away before they disappear
	         // 		    followScroll: if set to true, the flakes will follow the vertical scroll, being always visible
		    
	         fadeAway:true
	       });
	
	//Default Action
		$(".evpanel").hide(); //Hide all content
		$(".evpanel h1").hide(); //Hide all content
		$("ul.tabs li:first").addClass("active").show(); //Activate first tab
		$(".evpanel:first").show(); //Show first tab content

		//On Click Event
		$("ul.tabs li").click(function() {
			$("ul.tabs li").removeClass("active"); //Remove any "active" class
			$(this).addClass("active"); //Add "active" class to selected tab
			var selectedtab = $(this).find("a").text();
			// alert(selectedtab);
			_gaq.push(['_trackPageview', '/tab/'+selectedtab]);
			_gaq.push(['_trackEvent', 'Advert', 'Shown', '/tab/'+selectedtab]);
			$(".evpanel").hide(); //Hide all tab content
			var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
			$(activeTab).fadeIn(); //Fade in the active content
			return false;
		});
	
	
	
	
	$('.load').live('click', function() {
	var lastmsg = $(this).attr("id");
	if(lastmsg)	{
		$("a#"+lastmsg).html('Loading...');
		$.ajax({type: "POST",url: "/_showload.asp",data: "page_id=1004&position="+ lastmsg,success: function(html){
			$("#showload").append(html);
			$("a#"+lastmsg).remove();
			$(body).animate({scrolltop: $(body)[0].scrollHeight});
			}
		});
	}
	else {
		$(".load").html("You have reached the end");
	}
	return false;
	});
	

	if($(".accordion").length){
	$( ".accordion" ).accordion({
		autoHeight: false,
		collapsible: true,
		active:false
		});
	};
	
	$('.accordion').bind('accordionchange', function(event, ui) {
	  /* In here, ui.newHeader = the newly active header as a jQ object
	              ui.newContent = the newly active content area */
	  $.scrollTo( ui.newHeader,{duration:500} ); // or ui.newContent, if you prefer
	});
	
	
	$('.accordion h2 a').click(function() {
		var thisadid = $(this).parent().attr("id");
		
		$("#sidead").load("/_ad.asp?clickid="+thisadid, function(response, status, xhr) {
		  if (status == "error") {
		    var msg = "Sorry but there was an error: ";
		    $("#sidead").html(msg + xhr.status + " " + xhr.statusText);
		  }
		});
		
		
		
	});
	
	
	

	$('#gallery a, #gallerylist a, #showload .imgblock a, #galleryholder a, .imgblock a').lightBox(); // Select all links in object with gallery ID

	$('#submitbutton').click(function() {
		if($('#tandc').is(":checked")){
                return true;
	        }
	        else
	        {
			alert("Please read and agree to the Terms and Conditions.");
                return false;
	        }
	 
		
	});
	

	
	
	$('#cnForm, #fmunsub').live('submit', function() {
		
		
		$("#flash").hide();
		
		$(".required").removeClass('required');
		var dataString = $(this).serialize() + '&Ajax=1';
		var hasError = false;
		
		
	
		var thisemail = $("input[name='stru_e']").val();
		var thisname = $("input[name='stru_n']").val();
		var thisenq = $("textarea[name='stru_en']").val();
		
		
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var regex = new RegExp(emailReg);
		if ((!regex.test(thisemail)) || thisemail==""){
			$("#stru_e").addClass('required');
			hasError = true;
		}
		if (thisname==""){
			$("#stru_n").addClass('required');
			hasError = true;
		}
		if (thisenq==""){
			$("#stru_en").addClass('required');
			hasError = true;
		}
		if (hasError == true)
	        {
					
				$("#flash").fadeIn(400).html('&nbsp;<span class="loading">Please enter your name and a valid email address.</span>');
				
	        }
	        else
	        {
			
	            $.ajax({
	                type: "POST",
	                url: "/scripts/sendemail.asp",
	                data: dataString,
	                cache: false,
	                success: function(html) {
					
					$('#cnForm').hide();
					$("#flash").fadeIn(400).html('&nbsp;<span class="loading">'+html+'</span>');
	                }
	            });
	        }
	  return false;
	});
	
	
	$('#sponsgal').cycle({ 

	    fx:     'fade', 
	    speed:  'fast', 
	    timeout: 2500,
random:  1 
	});

	

	
	
});


var fixed = false;
var $window = $(window), $sub = $('#sub'); 
///cache doms for speediness http://ejohn.org/blog/learning-from-twitter/#postcomment
// although still using window.scroll as setinterval (above) was too jumpy
$window.scroll(function () {
		
	// var inview = '#' + $("#cllist > article > h2:in-viewport:first").parent().attr('id'),
	// $link = $('nav a').filter('[hash=' + inview + ']');
	// 
	// if ($link.length && !$link.is('.highlightcl')) {
	// 	$('nav#sub a').removeClass('highlightcl');
	// 	$link.addClass('highlightcl');
	// }

	if($window.scrollTop() >= 300) {
		$sub.addClass('stickybar');
	} else {
		$sub.removeClass('stickybar');
	}
});






