//*************************** 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;
}

//*************************** 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").css("display", "inline");
	$("#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 showcarousel_itemFirstInCallback(carousel, item, idx, state) {
    var days = new Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
    days[parseInt($("#show_carousel").attr("today_weekday"))] = "Today";
    var current_weekday = parseInt($("#show_carousel").attr("current_weekday"));

    var entry_days_index = parseInt($(item).attr('entry_day'));

    if (entry_days_index != current_weekday)
    {
        var entry_day = days[entry_days_index];
        $(".st_title").children("h1").text(entry_day + " on SABC 3");
        $(".st_body_nav").find("a").removeClass("on");
        $($(".st_body_nav").find("a")[entry_days_index]).addClass("on");
        $("#show_carousel").attr("current_weekday", entry_days_index);
    }
};


function showcarousel_itemVisibleInCallbackBeforeAnimation(carousel, item, idx, state) {
    //var img = $(item).find("img");
    //img.attr("src", img.attr("href"));
};


function showcarousel_initCallback(carousel) {

    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
    
    $(".st_body_nav a").each(function(i) {
        var elem = $(this);
        elem.click(function() {
            carousel.scroll($.jcarousel.intval(elem.attr("entry_start")));
        });
    });
};

$(function(){
    var on_air_entry_index = $("#show_carousel").attr("on_air_entry_index");
    var start_index = parseInt(on_air_entry_index) - 1;
    if (start_index < 0)
        start_index = 0;
    
    $("#show_carousel").css("display", "block");

    $("#show_carousel ul").jcarousel({ 
            scroll: 1,
            auto: 6,
            wrap: 'last',
            start: start_index,
            initCallback: showcarousel_initCallback,
            itemFirstInCallback: {
                onBeforeAnimation: showcarousel_itemFirstInCallback
            },
            itemVisibleInCallback: {
                onBeforeAnimation: showcarousel_itemVisibleInCallbackBeforeAnimation
            }
    });
});

//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();
	});
});

//poll Submit
$(document).ready(function() {
    $(".pollvote").click(function() {
        $("#frmPoll").ajaxSubmit({success: function(data) {
            $('div.box_13rds.no-marg.opinion_poll').replaceWith(data);
        }});
    });
});

//Newsletter Subscribe

function newsletterSubscribe()
{
    if (validateNewsletter())
    {
        var news_email_value = $("#news_email").attr("value");
        $("#news_email").attr("value", "Subscribing, Please Wait...");

        //SiteCatalyst
        var s=s_gi('accsabcsabc3dev');
        s.linkTrackVars='eVar2,events';
        s.linkTrackEvents='event1';
        s.eVar2=news_email_value;
        s.events='event1';
        s.tl(this,'o','Newsletter Sign up');

        $.ajax({
            url: "mailersubscribe",
            dataType: "html",
            type: "POST",
            data: "news_email=" + news_email_value,
            success: function(html){
                $("#news_email").attr("value", "Subscription Complete, Thank You.");
            }
        });
    }
    return false;
}
