/*

	PROJECT: FINDA
	EDITOR: KIEL FROST
*/

$(document).ready(function() {
    //////////////////////////////////////////////////////
    ////// Marketing Promo Carousel Widget
    ////// Kevin Vanderbeken - APN Online
    //////////////////////////////////////////////////////
    var initMarketingPromos = $('.promo-carousel ul').children().length;
    if ($('.promo-carousel').is('div')) {
        if (initMarketingPromos > 1) {
	        $('#promoController').show();
	        $(".promo-carousel").jCarouselLite({
	            auto: 7000,
	            speed: 500,
	            btnNext: "#nextPromo",
	            btnPrev: "#previousPromo",
	            visible: 1,
	            scroll: 1,
	            afterEnd: function(a) {
	                a.each(function() {
	                    $("#promoType").html($(this).find("input").attr("value"));
	                });
	            }
	        });
    	}
        else {
			$(".promo-carousel").css('visibility', 'visible');
		}
    }
	
	

	
	
	$("#searchField").addClass('googlesearch');
	$("#searchField").bind('focus', function() {
		$(this).removeClass();
	});

	$("searchField");
	$("#searchField").blur(function() {
		$(this).addClass('googlesearch');
	});

	// Change region
	$('.jdropbox').bind('change', function(e){
		window.location = this.value;
	});

	//TV day change
	$("select[name='jump']").bind('change', function(e){
		window.location = this.value;
	});

    // TV SHOW GUIDE
    if ($("#showTvGuide").is("button")) {
        $('#showTvGuide').bind('click', function(e) {
            var selectDay = $('#tvDay').val();
            var selectPhase = $('#tvPhases').val();
            var tvRegion = $('#tvRegion').val();
            window.location = selectDay+selectPhase+"/"+tvRegion+"/";
        });
    }

    //ACTIVE MENU
	var findaLocation = location.href;
	var tvCheck = findaLocation.indexOf("/whats-on/tv/guide/");
	var eventCheck = findaLocation.indexOf("/events/");
	if (tvCheck != -1 ) {
		$("#m_tvGuide a").addClass("current");
	}
    if (eventCheck != -1 ) {
		$("#m_community-events a").addClass("current");
	}

    // GET USER LOGIN STATUS
    $.getJSON("/myfinda/userstatus/", function(data) {
        if (eval(data.authenticated)) { // tried Boolean - doesnt work - so eval it is!
	    $("a[id='register']").css("display", "none").end();
            $("a[id='myaccount']").css("display", "inline").end();
            $("a[id='login']").css("display", "none").end();
            $("a[id='logout']").css("display", "inline").end();
            $("div[id='addComments']").find("h2").css("display", "block").end();
            $("div[id='addComments']").find("h3").css("display", "block").end();
            $("div[id='addComments']").find("div[id='commentRegisterLogin']").css("display", "none").end();
            $("div[id='addComments']").find("div[id='commentSubmitComment']").css("display", "block").end();
            $("p[class='commentUserInfo']").find("a[id='username']")
                .text(data.username)
                .end();
        } else {
            // switch button to login
            $("div[id='addComments']").find("h2").css("display", "block").end();
            $("div[id='addComments']").find("h3").css("display", "block").end();
            $("div[id='addComments']").find("div[id='commentRegisterLogin']").css("display", "block").end();
            $("div[id='addComments']").find("div[id='commentSubmitComment']").css("display", "none").end();
        }
        $("div[id='commentsFragment']").css("display", "block").end();
    });

        // THIRD PARTY LINK
// COMMENTED OUT - DUE TO functionality already existing in YUI and we were getting double popups
//        $('a.newWindow').bind('click', function(e) {
//            e.preventDefault(e);
//            window.open(this.href);
//        });

});

function getArgs() {
    var args = new Object();
    var query = location.search.substring(1);
    var pairs = query.split("&");
    for(var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');
        if (pos == -1) continue;
        var argname = pairs[i].substring(0,pos);
        var value = pairs[i].substring(pos+1);
        args[argname] = unescape(value);
    }
    return args;
}
var args = getArgs();
if (args.c) {
    $("div[id='commentThanks']").css("display", "block").end();
}



// Form field limiter
var limit = "2500"; // This represents COMMENT_MAX_LENGTH value which is default 3000 minus a few
function limiter(formName){
    var tex = document.forms[formName].comment.value;
    var len = tex.length;
    if(len >= limit){
        $("form[name='commentForm']").find("p[id='commentErrorMessage']").css("display", "block").end();
    } else {
        $("form[name='commentForm']").find("p[id='commentErrorMessage']").css("display", "none").end();
    }
    if(len > limit){
        tex = tex.substring(0,limit);
        document.forms[formName].comment.value = tex;
        return false;
    }
}
