//*************************** VALIDATION ***************************//

//validateSearch
function validateSearch()
{
	fv =  new formValidator();
	
	if (fv.isEmpty("q"))
		fv.raiseError("Please enter a search term.");
		
	if (fv.numErrors() > 0)
	{
		fv.displayErrors();
		return false;
	}
	else
	  return true;
}

//validatePoll
function validatePoll()
{
	fv =  new formValidator();
	
	if (!fv.isCheckedRadio("poll"))
		fv.raiseError("Please select an option before voting.");
		
	if (fv.numErrors() > 0)
	{
		fv.displayErrors();
		return false;
	}
	else
	  return true;
}

//validateNewsletter
function validateNewsletter()
{
	fv =  new formValidator();
	
	if (fv.isEmpty("news_email"))
		fv.raiseError("Please enter your Email Address.");
	else
	{	
  		if (!fv.isEmailAddress("news_email"))
			fv.raiseError("Please enter a valid Email Address.");
	}
		
	if (fv.numErrors() > 0)
	{
		fv.displayErrors();
		return false;
	}
	else
	  return true;
}

//validateSchedSearch
function validateSchedSearch()
{
	fv =  new formValidator();
	
	if (fv.isEmpty("sched_search_term"))
		fv.raiseError("Please enter a search term.");
		
	if (fv.numErrors() > 0)
	{
		fv.displayErrors();
		return false;
	}
	else
	  return true;
}

//validateContact
function validateContact()
{
	fv =  new formValidator();
	
	if (fv.isEmpty("contact_name"))
		fv.raiseError("Please enter your First Name.");
		
	if (fv.isEmpty("contact_surname"))
		fv.raiseError("Please enter your Surname.");
		
	if (fv.isEmpty("contact_email"))
		fv.raiseError("Please enter your Email Address.");
	else
	{	
  		if (!fv.isEmailAddress("contact_email"))
			fv.raiseError("Please enter a valid Email Address.");
	}
	
	if (fv.isEmpty("contact_message_subject"))
		fv.raiseError("Please enter your Subject.");
		
	if (fv.isEmpty("contact_message_body"))
		fv.raiseError("Please enter your Message.");
		
	if (fv.numErrors() > 0)
	{
		fv.displayErrors();
		return false;
	}
	else
	  return true;
}

//validateCompetition
function validateCompetition()
{
	fv =  new formValidator();
	
	if (fv.isEmpty("competition_answer"))
		fv.raiseError("Please enter the competition answer.");
		
	if (fv.numErrors() > 0)
	{
		fv.displayErrors();
		return false;
	}
	else
	  return true;
}

//validateReminder
function validateReminder()
{
	fv =  new formValidator();
	
	if (fv.isEmpty("reminder"))
		fv.raiseError("Please enter your cellphone number");
		
	if (fv.numErrors() > 0)
	{
		fv.displayErrors();
		return false;
	}
	else
	  return true;
}

//validateLogin
function validateLogin()
{
	fv =  new formValidator();
	
	if (fv.isEmpty("email_address"))
		fv.raiseError("Please enter your email address");
	else
	{	
  		if (!fv.isEmailAddress("email_address"))
			fv.raiseError("Please enter a valid email address");
	}
	
	if (fv.isEmpty("password"))
		fv.raiseError("Please enter your password");
		
	if (fv.numErrors() > 0)
	{
		fv.displayErrors();
		return false;
	}
	else
	  return true;
}

//validateForgot
function validateForgot()
{
	fv =  new formValidator();
	
	if (fv.isEmpty("email_address"))
		fv.raiseError("Please enter your email address");
	else
	{	
  		if (!fv.isEmailAddress("email_address"))
			fv.raiseError("Please enter a valid email address");
	}
		
	if (fv.numErrors() > 0)
	{
		fv.displayErrors();
		return false;
	}
	else
	  return true;
}

//validateRegister
function validateRegister()
{
	fv =  new formValidator();
	
	if (fv.isEmpty("email_address"))
		fv.raiseError("Please enter your email address");
	else
	{	
  		if (!fv.isEmailAddress("email_address"))
			fv.raiseError("Please enter a valid email address");
	}
	
	if (fv.isEmpty("username"))
		fv.raiseError("Please enter your selected username");
	
	if (fv.isEmpty("password"))
		fv.raiseError("Please enter your selected password");
		
	if (fv.numErrors() > 0)
	{
		fv.displayErrors();
		return false;
	}
	else
	  return true;
}

//*************************** JQUERY ***************************//

//slider
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;
		
	}, 8000);
};

$(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();
});

//jCarousel
$(function(){
	$("#sunday ul").jcarousel({ scroll: 1 });
});

//Show Tray
/*$(function(){
	$(".st_s_content").mouseenter(function () {
      $(this).css({'background-color' : '#f0f2f4'});
	  $(this).find("span.st_s_cat strong").css({'display' : 'block'});
    });
	$(".st_s_content").mouseleave(function () {
      $(this).css({'background-color' : '#ffffff'});
	  $(this).find("span.st_s_cat strong").css({'display' : 'none'});
    });
});*/

//Schedule
$("ul#schedule_days").ready(function() {
	$(this).find('.box_23rds_sched_days li:last').css({width: "90px", border: "0"});
});
$("ul#schedule_dates").ready(function() {
	$(this).find('.box_23rds_sched_dates li:last').css({width: "90px", border: "0"});
});

function bind_schedule_days()
{
    $("ul#schedule_days li").each(function(i) {
        $(this).click(function () {
            $("ul#schedule_days li").removeClass("on");
            $($("ul#schedule_days li")[i]).addClass("on");

            $(".box_23rds_sched").removeClass("schedactive");
            $($(".box_23rds_sched")[i]).addClass("schedactive");
        });
    });
    $("ul#schedule_dates li").each(function(i) {
        $(this).click(function () {
            $("ul#schedule_days li").removeClass("on");
            $($("ul#schedule_days li")[i]).addClass("on");

            $(".box_23rds_sched").removeClass("schedactive");
            $($(".box_23rds_sched")[i]).addClass("schedactive");
        });
    });
}

function load_schedule()
{
    var width = $(".left").css("width");
    var height = $(".left").css("height");
        
    $("#loading_overlay").css("width", width);
    $("#loading_overlay").css("height", height);
    $("#loading_icon").css("left", parseInt(width) / 2);
    $("#loading_icon").css("top", parseInt(height) / 3);

    $("#loading_overlay").css("display", "block");
    $("#loading_icon").css("display", "block");

    $.ajax({
        url: "ajax/" + $(this).attr("href"),
        dataType: "html",
        success: function(html){
            $(".left").html(html);
            $(".box_23rds_schedtitle").find("a").bind("click", load_schedule);
            bind_schedule_days()
        }
    });
    return false;
}

$( function() {
    $(".box_23rds_schedtitle").find("a").bind("click", load_schedule);
    bind_schedule_days()
});


//Mega Dropdown
$(document).ready(function() {
	function addMega(){
		$(this).addClass("hovering");
	}
	function removeMega(){
		$(this).removeClass("hovering");
	}
	var megaConfig = {
		interval: 0,
		sensitivity: 1,
		over: addMega,
		timeout: 0,
		out: removeMega
	};
	$("li.mega").hoverIntent(megaConfig)
});

//Contact
$(function() {
	$(".box_23rds_contactbody p span.txt_input").click(function () {
      $(this).css({'background-position' : '0px -30px'});
    });
	$(".box_23rds_contactbody p span.txt_input input").blur(function () {
      $(".box_23rds_contactbody p span.txt_input").css({'background-position' : '0px 0px'});
    });
	$(".box_23rds_contactbody p span.txt_area").click(function () {
      $(this).css({'background-position' : '0px -128px'});
    });
	$(".box_23rds_contactbody p span.txt_area textarea").blur(function () {
      $(".box_23rds_contactbody p span.txt_area").css({'background-position' : '0px 0px'});
    });
});

//Toggle QA
$(document).ready(function() {
	$('[class^=toggle-item]').hide();
	$('[class^=link]').click(function() {
		var $this = $(this);
		var x = $this.attr("className");
		$('.toggle-item-' + x).toggle("medium");
		return false;
	});
});

//poll Toggle
$(document).ready(function() {
	$(".polllink").click(function() {
		$(".box_13rds_poll").find('p.poll_answ').css({visibility: "visible", display: "none"}).hide();
		$(".box_13rds_poll").find('p.poll_btn_vote').css({visibility: "visible", display: "none"}).hide();
		$(".box_13rds_poll").find('p.poll_btn_results').css({visibility: "visible", display: "none"}).hide();
		$(".box_13rds_poll").find('p.poll_res').css({visibility: "visible", display: "none"}).show();
	});
});

//*************************** PHASE 2 ***************************//

//JCAROUSEL
(function($){$.fn.jCarouselLite=function(o){o=$.extend({btnPrev:null,btnNext:null,btnGo:null,mouseWheel:false,auto:null,speed:200,easing:null,vertical:false,circular:true,visible:3,start:0,scroll:1,beforeStart:null,afterEnd:null},o||{});return this.each(function(){var b=false,animCss=o.vertical?"top":"left",sizeCss=o.vertical?"height":"width";var c=$(this),ul=$("ul",c),tLi=$("li",ul),tl=tLi.size(),v=o.visible;if(o.circular){ul.prepend(tLi.slice(tl-v-1+1).clone()).append(tLi.slice(0,v).clone());o.start+=v}var f=$("li",ul),itemLength=f.size(),curr=o.start;c.css("visibility","visible");f.css({overflow:"hidden",float:o.vertical?"none":"left"});ul.css({margin:"0",padding:"0",position:"relative","list-style-type":"none","z-index":"1"});c.css({overflow:"hidden",position:"relative","z-index":"2",left:"0px"});var g=o.vertical?height(f):width(f);var h=g*itemLength;var j=g*v;f.css({width:f.width(),height:f.height()});ul.css(sizeCss,h+"px").css(animCss,-(curr*g));c.css(sizeCss,j+"px");if(o.btnPrev)$(o.btnPrev).click(function(){return go(curr-o.scroll)});if(o.btnNext)$(o.btnNext).click(function(){return go(curr+o.scroll)});if(o.btnGo)$.each(o.btnGo,function(i,a){$(a).click(function(){return go(o.circular?o.visible+i:i)})});if(o.mouseWheel&&c.mousewheel)c.mousewheel(function(e,d){return d>0?go(curr-o.scroll):go(curr+o.scroll)});if(o.auto)setInterval(function(){go(curr+o.scroll)},o.auto+o.speed);function vis(){return f.slice(curr).slice(0,v)};function go(a){if(!b){if(o.beforeStart)o.beforeStart.call(this,vis());if(o.circular){if(a<=o.start-v-1){ul.css(animCss,-((itemLength-(v*2))*g)+"px");curr=a==o.start-v-1?itemLength-(v*2)-1:itemLength-(v*2)-o.scroll}else if(a>=itemLength-v+1){ul.css(animCss,-((v)*g)+"px");curr=a==itemLength-v+1?v+1:v+o.scroll}else curr=a}else{if(a<0||a>itemLength-v)return;else curr=a}b=true;ul.animate(animCss=="left"?{left:-(curr*g)}:{top:-(curr*g)},o.speed,o.easing,function(){if(o.afterEnd)o.afterEnd.call(this,vis());b=false});if(!o.circular){$(o.btnPrev+","+o.btnNext).removeClass("disabled");$((curr-o.scroll<0&&o.btnPrev)||(curr+o.scroll>itemLength-v&&o.btnNext)||[]).addClass("disabled")}}return false}})};function css(a,b){return parseInt($.css(a[0],b))||0};function width(a){return a[0].offsetWidth+css(a,'marginLeft')+css(a,'marginRight')};function height(a){return a[0].offsetHeight+css(a,'marginTop')+css(a,'marginBottom')}})(jQuery);

jQuery(document).ready(function() {

	//Top Dropdown
	jQuery("a.logged_user").click(function() {
		jQuery(this).parent().find('ul').slideToggle();
	});

	//Head Search Shift
	jQuery(".search p.term input").click(function () {
		jQuery(this).parent().addClass('search_shift');
    });
	
	//Whats New Item Hover
	jQuery('.box_23rds_item').hover(
		function () {
			jQuery(this).find('span.date, span.photos, span.time, span.location').hide();
			jQuery(this).find('a.comments, a.likes').show();
		},
		function () {
			jQuery(this).find('span.date, span.photos, span.time, span.location').show();
			jQuery(this).find('a.comments, a.likes').hide();
		}
	);
	
	//Show Tray Hover
	jQuery('.jcarousel-clip').prepend('<div style="position: relative;"><div class="false-prev"></div><div class="false-next"></div></div>');
	jQuery('.jcarousel-skin-tango li').hover(
		function () {
			jQuery(this).find('.show_pop').css({"margin-top":"0"});
			jQuery('.st_body, .jcarousel-clip').css({"overflow":"visible"});
			jQuery(this).css({"z-index":"10000"});
			jQuery('.jcarousel-next, .jcarousel-prev').css({"z-index":"1"});
			jQuery('.false-prev, .false-next').css({"display":"block"});
		},
		function () {
			jQuery(this).find('.show_pop').css({"margin-top":"-10000px"});
			jQuery('.st_body, .jcarousel-clip').css({"overflow":"hidden"});
			jQuery(this).css({"z-index":"9000"});
			jQuery('.jcarousel-next, .jcarousel-prev').css({"z-index":"9001"});
			jQuery('.false-prev, .false-next').css({"display":"none"});
		}
	);
	/*jQuery('.jcarousel-skin-tango li').hover(
		function () {
			jQuery(this).find('.show_pop').css({"margin-top":"0", "z-index":"10000"});
			jQuery('.st_body, .jcarousel-clip').css({"overflow":"visible", "z-index":"2"});
			jQuery(this).css({"z-index":"10000"});
			jQuery('.jcarousel-next, .jcarousel-prev').css({"z-index":"1"});
		},
		function () {
			jQuery(this).find('.show_pop').css({"margin-top":"-10000px"});
			jQuery('.st_body, .jcarousel-clip').css({"overflow":"hidden"});
			jQuery(this).css({"z-index":"9000"});
			jQuery('.jcarousel-next, .jcarousel-prev').css({"z-index":"9001"});
		}
	);*/
	
	//Gallery Item Hover
	jQuery('.gallery_item').hover(
		function () {
			jQuery(this).find('p.link_view > a').show();
			jQuery(this).find('p.link_photos').css({"background-position":"1px -16px"});
			jQuery(this).find('p.link_videos').css({"background-position":"1px -25px"});
		},
		function () {
			jQuery(this).find('p.link_view > a').hide();
			jQuery(this).find('p.link_photos').css({"background-position":"1px 4px"});
			jQuery(this).find('p.link_videos').css({"background-position":"1px 1px"});
		}
	);
	
	//Gallery Carousel Initialize
	jQuery(".gallery_list").jCarouselLite({
        btnNext: ".gallery_next",
        btnPrev: ".gallery_prev"
    });
	
	//Reminder Show
	jQuery(".box_23rds_showdetail div.reminder p.remind a").click(function() {
		jQuery(this).parent().hide();
		jQuery(this).parent().parent().find('p.remind_field').show();
	});
	
	//Show Poll Results
	jQuery("a.results").click(function() {
		jQuery(this).parent().parent().hide();
		jQuery('.poll_results').show();
	});
	
	//General Style Chops
	jQuery('ul.menu > li:last').find('a').css({"padding":"0"});
	jQuery('.box_23rds_item:nth-child(2n)').addClass('no_pad_right').after('<p class="clearfix"></p>');
	jQuery('.gallery_page li:nth-child(3n)').addClass('no_marg_right').after('<p class="clearfix"></p>');
	jQuery('.box_23rds_episodes > .episode_item:nth-child(2n)').addClass('bg_grey');
	jQuery('.box_23rds_poll_box_row:last').css({"padding":"0!important"});

});

