﻿var VideoTabPage = 1;
var CurrentTabType = "tvod";

$(function(){
    $("#tabtvod").click(function(){
        if (!$(this).hasClass("selected")){
            CurrentTabType = "tvod";
            
            $(".tabs .fl.xa li").removeClass("selected");
            $(this).addClass("selected");
            
            $("#filterQ").val("");
            $("#catId").val("0");
            
            $("#catId").unbind("change");
            bindVideoCategories(CurrentTabType);
            $("#catId").bind("change", function(){ BindVideoTabContent(CurrentTabType); });
            
            BindVideoTabContent(CurrentTabType);
            $("#filterQ").unbind("keypress");
            $("#filterQ").bind("keypress", VideoTabContent);
            
            $(".bottom .btnGo .ico1").html("Tüm <b>Kirala - İzle</b> Videoları");
            $(".bottom .btnGo").attr("href", "/video/kirala-izle/butun-turler/butun-kategoriler");
            
            $("#channelPaging").hide();
        }
    });
    $("#tabsvod").click(function(){
        if (!$(this).hasClass("selected")){
            CurrentTabType = "svod";
            
            $(".tabs .fl.xa li").removeClass("selected");
            $(this).addClass("selected");
            
            $("#filterQ").val("");
            $("#catId").val("0");
            
            $("#catId").unbind("change");
            bindVideoCategories(CurrentTabType);
            $("#catId").bind("change", function(){ BindVideoTabContent(CurrentTabType); });
            
            BindVideoTabContent(CurrentTabType);
            $("#filterQ").unbind("keypress");
            $("#filterQ").bind("keypress", VideoTabContent);
            
            $(".bottom .btnGo .ico1").html("Tüm <b>Seç - İzle</b> Videoları");
            $(".bottom .btnGo").attr("href", "/video/sec-izle/butun-turler/butun-kategoriler");
            
            $("#channelPaging").hide();
        }
    });
    $("#tabtstv").click(function(){
        if (!$(this).hasClass("selected")){
            CurrentTabType = "tstv";
            
            $(".tabs .fl.xa li").removeClass("selected");
            $(this).addClass("selected");
            
            $("#filterQ").val("");
            $("#catId").val("0");
            
            $("#catId").unbind("change");
            bindVideoCategories(CurrentTabType);
            $("#catId").bind("change", function(){ BindVideoTabContent(CurrentTabType); });
            
            BindVideoTabContent(CurrentTabType);
            $("#filterQ").unbind("keypress");
            $("#filterQ").bind("keypress", VideoTabContent);
            
            $(".bottom .btnGo .ico1").html("Tüm <b>Tekrar - İzle</b> Videoları");
            $(".bottom .btnGo").attr("href", "/video/tekrar-izle/butun-turler/butun-kategoriler");
            
            $("#channelPaging").hide();
        }
    });
    
    $("#filterQ").bind("keypress", VideoTabContent);
    $("#catId").bind("change", function(){ BindVideoTabContent(CurrentTabType); });
    bindVideoCategories(CurrentTabType);
});

var VideoTabContent = function(evt) {
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode == 13) {
        p = 1;
        BindVideoTabContent(CurrentTabType);
    }
    charCode = null;
}

function BindVideoTabContent(typename){
    blockChannelWidget();
    
    var catId = $("#catId").val();
    catId = catId == "0" ? "" : catId;
    var containerNode = $(".showcase.video");
    var pagingNode = $("#channelPaging");
    var q = $("#filterQ").val();
    q = (q == "Arama" || q == "Hemen Ara") ? "" : q;

    $.get("/video/HomepageVideo.aspx", { type: typename, catId: catId, p: VideoTabPage, m: 12, q: q }, function(response) {

        containerNode.html(response.listHtml);
        pagingNode.html(response.pagingHtml);

        $("li", containerNode).hover(function() {
            $(this).addClass("selected")
        }).mouseleave(function() {
            $(this).removeClass("selected")
        });

        containerNode = null;
        pagingNode = null;
        
        $(".showcase").removeClass("tvlogo").addClass("video");

        unblockChannelWidget();
        $.uniform.update("#catId");

    }, "json");

    catId = null;
}

function bindVideoCategories(type){
    $.get("/video/HomepageVideoCategories.aspx", { type: type }, function(response){
        $("#catId").html("")
            .append("<option value=\"0\">Kategori</option>");
        for(var i=0; i<response.length; i++){
            var TypeValue = response[i];
            $("#catId").append("<option value=\"" + TypeValue + "\">" + TypeValue.replace(/\^/g, ",") + "</option>");
        }
        $.uniform.update("#catId");
    }, "json");
}

var blockChannelWidget = function() {
    $(".leftSide").block({
        message: null,
        overlayCSS: {
            backgroundColor: '#ffffff',
            opacity: 0.4
        },
        css: {
            cursor: 'wait'
        },
        baseZ: 99999
    });
}

var unblockChannelWidget = function() {
    $(".leftSide").unblock();
}
