﻿var isFilmclub;
var ClickedItem;
var FeaturedContentTimer;

var limit = 1; // Page no
var ContainerWidth = 546; // Thumbnail container width
var isFCVideoPlaying = false;
var RecycleTime = 8000;

var GetFeaturedItemSetProps = function(event){
	jQuery.getJSON($(ClickedItem).attr("href"), 
		function(data){
		    $("#ContentIcon").attr("class", "");
		    switch (data.type)
		    {
				default:
		        case 0: $("#ContentIcon").addClass("NoIcon"); break;
		        case 1: $("#ContentIcon").addClass("FilmClub"); break;
		        case 2: $("#ContentIcon").addClass("KiralaIzle"); break;
		        case 3: $("#ContentIcon").addClass("TekrarIzle"); break;
		        case 4: $("#ContentIcon").addClass("FilmClub2"); break;
		        case 5: $("#ContentIcon").addClass("FilmClub1001"); break;
		        case 6: $("#ContentIcon").addClass("StarIcon"); break;
		        case 7: $("#ContentIcon").addClass("SecIzle"); break;
		    }

			$("#showcaseitem span.title").html(data.title);
			$("#showcaseitem span.alttitle").html(data.atitle != null ? data.atitle : "");
			$("#showcaseitem div.context").html(data.text);
			
			if (data.link != null){
			    $("#showcaseitem div.link a.btnGo").css("display", "");
			    $("#showcaseitem div.link a.btnGo").attr("href", data.link);
			}
			else
			    $("#showcaseitem div.link a.btnGo").hide();
			    
			if (data.big != null)
			    $("#showcaseitem div.picture img").attr("src", FeaturedContentDir + data.big);
			else
			    $("#showcaseitem div.picture img").attr("src", "/assets/img/featured_content_default.jpg");
			
			if (typeof(isFilmclub) == undefined){
			    isFilmclub = false;
			}
			
			if (isFilmclub){
			    if (data.alttext != null && data.alttext != ""){
			        $("#showcaseitem #fragman").removeClass("none");
						$("#showcaseitem .picture img").attr("src", FeaturedContentDefaultImage);
			        $("#showcaseitem .showcaseopacity").addClass("filmclub");
					CreatePlayer(FeaturedContentDir + data.big, FeaturedContentVideoDir + data.alttext);
			    }
			    else{
			        $("#showcaseitem #fragman").addClass("none");
			        $("#showcaseitem .showcaseopacity").removeClass("filmclub");
					CreatePlayer();
			    }
			}
			
			$("#showcaseitem div.picture").fadeIn("slow", function(){ 
				$("#showcaseitem div#fragman").show(); 
			});
		}
	);
}

var CreatePlayer = function(thumbpath, videopath){
	$("#fragman").html("<div id=\"FragmanVideo\"></div>");

	if (thumbpath != null && videopath != null){
		var flashvars = { videoPath:videopath, thumbPath:thumbpath, containerWidth:'417', containerHeight:'237' };
	
		var params = { allowFullScreen : "true", allowscriptaccess : "always", wmode : "transparent" };
		var attributes = { id : "FragmanVideo" };

		swfobject.embedSWF("/assets/flash/TivibuPlayer.swf", "FragmanVideo", "417", "237", "9.0.0","expressInstall.swf", flashvars, params, attributes);
	}
}

var videoCompleted = function() {
	isFCVideoPlaying = false;
	ResetFCTimer();
}
		
var videoStart = function() {
	isFCVideoPlaying = true;
	ClearFCTimer();
}
		
var videoStop = function() {
	if (isFCVideoPlaying){
		getMovie("FragmanVideo").videoReset();
		ResetFCTimer();
	}
}

var getMovie = function(movieName) {
	if (window.document[movieName]) 
		return window.document[movieName];

	if (navigator.appName.indexOf("Microsoft Internet")==-1)
	{
		if (document.embeds && document.embeds[movieName])
			return document.embeds[movieName]; 
	}
	else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
		return document.getElementById(movieName);
}

var SetFCTimer = function(){
	if (!isFCVideoPlaying){
		FeaturedContentTimer = setInterval(SetFeaturedContentTimer, RecycleTime);
	}
}

var ClearFCTimer = function(){
	clearInterval(FeaturedContentTimer);
	FeaturedContentTimer = null;
}

var ResetFCTimer = function(){
	ClearFCTimer();
	SetFCTimer();
}

var SetFeaturedContentTimer = function(){
	if ($(".infiniter .item").length > 1){
		var CurrentThumb = $(".infiniter .item.selected");
		var CurrentThumbIndex = $(".infiniter .item").index(CurrentThumb) + 1;
		if ($(CurrentThumb).next().length == 0){
			$("#showcase #navigation .container .infiniter").animate({ left: 0 }, 300);
			limit = 1;
            CheckButtons();
			$(".infiniter .item:first").find("a").trigger("click");
		}else{
			var ThumbPageIndex = parseInt(CurrentThumbIndex / 7) + 1;
			if (ThumbPageIndex != limit) SetThumbPage(ThumbPageIndex);
			$(CurrentThumb).next().find("a").trigger("click");
		}
	}
}

var SetThumbPage = function(page){
	$("#showcase #navigation .container .infiniter").animate({ left: -(ContainerWidth * (page-1)) + 'px' }, 300);
	limit = page;
    CheckButtons();
}

var CheckButtons = function(){
	CheckPrevButtonState();
	CheckNextButtonState();
}

var CheckPrevButtonState = function() {
    if (limit == 1)
        $('#pagination .prev').addClass("disabled");
    else
        $('#pagination .prev').removeClass("disabled");
}

var CheckNextButtonState = function() {
    if (limit == Math.ceil(FeaturedContentListSize / 7))
        $('#pagination .next').addClass("disabled");
    else
        $('#pagination .next').removeClass("disabled");
}

var makeTall = function() {
    $($(this).parent().parent().parent()).css("z-index", 999);
    $(this).animate({ "height": 67, "top": -31 }, 200);
    $(this).find('a').animate({ "height": 67 }, 200);
    $(this).find('p').show().animate({ "opacity": 1 }, 1000);
}

var makeShort = function() {
    $($(this).parent().parent().parent()).css("z-index", 0);
    $(this).animate({ "height": 36, "top": 0 }, 50);
    $(this).find('a').animate({ "height": 36 }, 50);
    $(this).find('p').hide().animate({ "opacity": 0 }, 0);
}

$(function() {

	$(".infiniter .item a").click(function(event){
		ResetFCTimer();
		isFCVideoPlaying = false;
		ClickedItem = $(this);
		$("#showcaseitem div#fragman").addClass("none");
		$("#showcaseitem div.picture").fadeOut("slow", GetFeaturedItemSetProps);
		$(".infiniter .item").removeClass("selected");
		$(this).parent().parent().addClass("selected");
		event.preventDefault();
	}); //Thumbnail Click

    // initialize scrollable
    var config = {
        over: makeTall, // function = onMouseOver callback (REQUIRED)
        timeout: 40, // number = milliseconds delay before onMouseOut
        out: makeShort // function = onMouseOut callback (REQUIRED)
    };

    $("#showcase #navigation .container .item")
		.hoverIntent(config)
		.hover(function() {
				$(this).addClass('hover');
				ClearFCTimer();
			}, function() {
				$(this).removeClass('hover');  //Callback
				ResetFCTimer();
		});

    $("#container.single ul.contentswitchers li").mouseleave(function() {
        $(this).removeClass("hover");
    });

    $("#container.single ul.contentswitchers li").mouseenter(function() {
        $(this).addClass("hover");
        var link = $(this).find("h4 a").attr("href");
        $("#container.single .imgcontainer img").attr('src', link);
    });

    $("#container.single ul.contentswitchers li a").click(function(event) {
        var link = $(this).attr("href");
        $("#container.single .imgcontainer img").attr('src', link);
        event.preventDefault();
    });

    $("#.prodContainer .tvlogo li").mouseleave(function() {
        $(this).removeClass("hover");
    });

    $("#.prodContainer .tvlogo li").mouseenter(function() {
        $(this).addClass("hover");
    });    

    $('#pagination .prev').click(function(evt) {
		evt.preventDefault();
		if (limit > 1) {
            $("#showcase #navigation .container .infiniter").animate({ left: "+=" + ContainerWidth }, 300);
            limit--;
            CheckButtons();
        }
    });
    $('#pagination .next').click(function(evt) {
		evt.preventDefault();
		if (limit < Math.ceil(FeaturedContentListSize / 7)) {
            $("#showcase #navigation .container .infiniter").animate({ left: "-=" + ContainerWidth }, 300);
            limit++;
            CheckButtons();
        }
    });

	SetFCTimer(); //Timer For Automatic Cycling
});
