﻿var pageHeight = 504;
var itemsPerPage = 12;
var pagingPagesPerPage = 12;
var page = 1;
var pageCount = 1;
var pagingPage = 1;
var list = '#list-games';
var showlist = '#list-shows';

$(document).ready(function () {
    if (QSo != "###") {
        if (QSo.show != undefined) {
            callService('', '', QSo.show);
            $('body').scrollTo(680, 500);
        }
        if (QSo.filter != undefined) {
            callService('nickToons');
        }
        else callService('newgames');
    }
    else callService('newgames');

    $('#paging,' + list + ',' + showlist).disableTextSelect();
    initTabs();
});

function initGames()
{
    var gameCount = $(list).children().size();
    $('#pages').html('');
    
    // paging
    $('#paging-curr').remove();
    pagingCreated=false;
    
    // *** deep link to select a show: ****
    // QSo is an object that contains all qs params:
    
    pagingPage=1;
    page=1;    
    if(gameCount>itemsPerPage)
    {
        $('#pages').css({'display':'block'});
        
        pageCount = Math.floor(gameCount/itemsPerPage);
        if(gameCount/itemsPerPage>pageCount) pageCount++;
        
        var paging = [];
        for(var i=1; i<=pageCount; i++) {                
            paging[i] = '<a id="page'+i+'">'+i+'</a>';
            if(i%10==0) paging[i] += '<span class="more-pages">...</span><br /><span class="less-pages">...</span>';
        }
        $('#pages').append(paging.join(''));
        $('#pages a:first').addClass('on');

        $('#button-next').unbind().show().click(function () {
            if (!$(this).hasClass('disabled')) {
                page++; goPage(page);
                btg.Controller.reloadAds();
                trackPage();
                trackFeatured("games next", s_account, 'games');
            }

        });
        $('#button-back').unbind().show().click(function(){
            if (!$(this).hasClass('disabled')) {
                page--; goPage(page);
                btg.Controller.reloadAds();
                trackPage();
                trackFeatured("games prev", s_account, 'games');
            }
        });
        $('#pages a').unbind().click(function(){
            goPage(this.id.substr(4, 5));
            btg.Controller.reloadAds();
            trackPage();
            trackFeatured("games pages", s_account, 'games');

        });
        // ... paging controls:
        $('.more-pages').unbind().click(function(){
            showMorePages();
            btg.Controller.reloadAds();
            trackPage();
            trackFeatured("games more", s_account, 'games');
        });
        $('.less-pages').unbind().click(function(){
            showLessPages();
            btg.Controller.reloadAds();
            trackPage();
            trackFeatured("games less", s_account, 'games');
        });
        
        goPage(1);
        goPagingPage(1);
    } 
    else 
    {
        $('#button-next').hide();
        $('#button-back').hide();
    } 
    $(list+' a').unbind().click(function(){
        window.location=$(this).attr('href'); 
    });
}

function initShows()
{
    var showCount = $(showlist).children().size();
    $('#pages').html('');
    
    // paging
    $('#paging-curr').remove();
    pagingCreated=false;
    
    pagingPage=1;
    page=1;
    if(showCount>itemsPerPage)
    {
        $('#pages').css({'display':'block'});
        
        pageCount = Math.floor(showCount/itemsPerPage);
        if(showCount/itemsPerPage>pageCount) pageCount++;
        
        var paging = [];
        for(var i=1; i<=pageCount; i++) {                
            paging[i] = '<a id="page'+i+'">'+i+'</a>';
            if(i%10==0) paging[i] += '<span class="more-pages">...</span><br /><span class="less-pages">...</span>';
        }
        $('#pages').append(paging.join(''));
        $('#pages a:first').addClass('on');
                
        $('#button-next').unbind().show().click(function(){
            if (!$(this).hasClass('disabled')) {
                page++; goPage(page, true);
                btg.Controller.reloadAds();
                trackPage();
                trackFeatured("games next", s_account, 'games');
            }
        });
        $('#button-back').unbind().show().click(function(){
            if (!$(this).hasClass('disabled')) {
                page--; goPage(page, true);
                btg.Controller.reloadAds();
                trackPage();
                trackFeatured("games prev", s_account, 'games');
            }
        });
        $('#pages a').unbind().click(function(){
            goPage(this.id.substr(4, 5), true);
            btg.Controller.reloadAds();
            trackPage();
            trackFeatured("games pages", s_account, 'games');

        });
        // ... paging controls:
        $('.more-pages').unbind().click(function(){
            showMorePages();
            btg.Controller.reloadAds();
            trackPage();
            trackFeatured("games more", s_account, 'games');

        });
        $('.less-pages').unbind().click(function(){
            showLessPages();
            btg.Controller.reloadAds();
            trackPage();
            trackFeatured("games less", s_account, 'games');

        });
        
        goPage(1,true);
        goPagingPage(1);
    } 
    else 
    {        
        $('#button-next').hide();
        $('#button-back').hide();
    }
    try
    { 
        if(ie7)
        {
            $(showlist+' a').unbind().click(function(){
                window.location=$(this).attr('href'); 
            });
        }
    } 
    catch(err) 
    {}
    $('#list-shows a[href=""],#list-shows a[href*="?show"]').removeAttr("href").unbind().click(function(){ 
       callService('','',$(this).children('.list-show').html());
       return false;
    });
}

function initTabs()
{
    $('#tab-newgames').click(function(){
        if (!$(this).hasClass('on')) callService('newgames');
        btg.Controller.reloadAds();
        trackPage();
        trackPage();

    });
    
    $('#dropdown a').click(function () {
        if (!$(this).hasClass('on')) callService('gametype', $(this).html());
        btg.Controller.reloadAds();
        trackPage();
        trackPage();
    });
    $('#tab-shows').click(function () {
        $('#tabs a, #tabs div').removeClass('on');
        $('#tab-shows').addClass('on');
        $('#loading,' + list).hide();
        $(showlist).show();
        $('#breadcrumb').html('You are viewing: <span>Shows A-Z</span>');
        initShows();
        btg.Controller.reloadAds();
        trackPage();
        trackPage();

    });
}

function getQueryVariable(variable) 
{
    var query = window.location.search.substring(1);
    var vars = query.split('&');
    for (var i=0;i<vars.length;i++) 
    {
        var pair = vars[i].split('=');
        if (pair[0] == variable) return pair[1];
    }
    return '';
}

function callService(tab, type, show) {
    $('#tabs a, #tabs div').removeClass('on');
    if(tab!='') $('#tab-'+tab).addClass('on');
    else $('#tab-shows').addClass('on');
    $(list).show().html('');
    $('#loading').show();
    $('#list-shows').hide();
    if(tab == 'newgames') {
        $.ajax(
	    {
	        type: "POST",
	        url: "/services/contentLINQ.asmx/Games",
	        beforeSend: function (xhr) {
		        xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
		    },
		    data: "{show:'all','category':'all'}",
		    contentType: "application/json; charset=utf-8",
		    dataType: "json",
		    success: function(json)
		    {
		       $('#loading').hide();
		       var gamesData = json.d;
		       for(var i=0; i<=gamesData.length-1; i++) {
				    var thisGame = gamesData[i];
				    var elm = '<a title="Play '+ thisGame.Show +' - '+ thisGame.Name +'" href="'+ thisGame.Url +'">';
				    elm += '<span class="list-img">';
				    if(thisGame.Thumb=="") thisGame.Thumb = "/games/media/images/generic_show.gif";
                    elm += '<img src="'+ thisGame.Thumb +'" alt="'+ thisGame.Name +'" />';
                    elm += '</span>';
                    elm += '<span class="list-show">'+ thisGame.Name +'</span>';
                    elm += '<span class="list-title">'+ thisGame.Description +'</span>';
                    elm += '</a>';
				    $(list).append(elm);
		       }
		       $('#list-games').css({"top":0});
		       $('#breadcrumb').html('You are viewing: <span>Games</span>');
		       initGames();
		    },
		    error: function (desc) {
                $('#loading').hide();
		        $(list).html("<strong>Oops! Sorry, something has gone wrong with this page. Please try again.</strong>");
		    }
	    });
	}
	else if(tab == 'gametype') {
	    type = type.toString().toLowerCase();
        $.ajax(
	    {
		    type: "POST",
		    url: "/services/contentLINQ.asmx/Games",
		    beforeSend: function (xhr) {
		        xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
		    },
		    data: "{'show':'all','category':'"+ type +"'}",
		    contentType: "application/json; charset=utf-8",
		    dataType: "json",
		    success: function(json)
		    {
		       $('#loading').hide();
		       var gameData = json.d;
		       for(var i=0; i<=gameData.length-1; i++) {
				    var thisGame = gameData[i];
				    var elm = '<a title="Play '+ thisGame.Show +' - '+ thisGame.Name +'" href="'+  thisGame.Url +'">';
				    elm += '<span class="list-img">';
				    if(thisGame.Thumb=="") thisGame.Thumb = "/global/img/thumb.gif";
                    elm += '<img src="'+ thisGame.Thumb +'" alt="'+ thisGame.Name +'" />';
                    elm += '</span>';
                    elm += '<span class="list-show">'+ thisGame.Name +'</span>';
                    elm += '<span class="list-title">'+ thisGame.Description +'</span>';
                    elm += '</a>';
                    $(list).append(elm);
		       }
		       $('#list-games').css({"top":0});
		       $('#breadcrumb').html('You are viewing: <span>'+ type +' games</span>');
		       initGames();
		    },
		    error: function(){
		       $('#loading').hide();
		       $(list).html("<strong>Oops! Sorry, something has gone wrong with this page. Please try again.</strong>");
		    }
	    });
}
else if (tab == 'nickToons') {
    $.ajax(
	    {
	        type: "POST",
	        url: "/services/contentLINQ.asmx/Games",
	        beforeSend: function (xhr) {
	            xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
	        },
	        data: "{'show':'NickToons','category':'all'}",
	        contentType: "application/json; charset=utf-8",
	        dataType: "json",
	        success: function (json) {
	            $('#loading').hide();
	            var gameData = json.d;
	            for (var i = 0; i <= gameData.length - 1; i++) {
	                var thisGame = gameData[i];
	                var elm = '<a title="Play ' + thisGame.Show + ' - ' + thisGame.Name + '" href="' + thisGame.Url + '">';
	                elm += '<span class="list-img">';
	                if (thisGame.Thumb == "") thisGame.Thumb = "/global/img/thumb.gif";
	                elm += '<img src="' + thisGame.Thumb + '" alt="' + thisGame.Name + '" />';
	                elm += '</span>';
	                elm += '<span class="list-show">' + thisGame.Name + '</span>';
	                elm += '<span class="list-title">' + thisGame.Description + '</span>';
	                elm += '</a>';
	                $(list).append(elm);
	            }
	            $('#list-games').css({ "top": 0 });
	            $('#breadcrumb').html('You are viewing: <span>Nicktoons games</span>');
	            initGames();
	        },
	        error: function () {
	            $('#loading').hide();
	            $(list).html("<strong>Oops! Sorry, something has gone wrong with this page. Please try again.</strong>");
	        }
	    });
}



	else if(show!='')
	{
	    $.ajax(
	    {
		    type: "POST",
		    url: "/services/contentLINQ.asmx/Games",
		    beforeSend: function (xhr) {                            
		        xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
		    },
		    data: "{'show':'"+ show +"','category':'all'}",
		    contentType: "application/json; charset=utf-8",
		    dataType: "json",
		    success: function(json)
		    {  
		       $('#loading').hide();
		       var gameData = json.d;
    		   
		       for(var i=0; i<=gameData.length-1; i++) {
				    var thisGame = gameData[i];
				    var elm = '<a title="Play '+ thisGame.Show +' - '+ thisGame.Name +'" href="'+  thisGame.Url +'">';
				    elm += '<span class="list-img">';
				    if(thisGame.Thumb=="") thisGame.Thumb = "/shows/thumbs/generic_show.jpg";
                    elm += '<img src="'+ thisGame.Thumb +'" alt="'+ thisGame.Name +'" />';
                    elm += '</span>';
                    elm += '<span class="list-show">'+ thisGame.Name +'</span>';
                    elm += '<span class="list-title">'+ thisGame.Description +'</span>';
                    elm += '</a>';
                    
                    $(list).append(elm);
		       }
		       $('#list-games').css({"top":0});
		       $('#breadcrumb').empty().html('You are viewing: <span>'+gameData[0].Show+' games</span>');
		       initGames();
		    },
		    error: function(){
		       $('#loading').hide();
		       $(list).html("<strong>Oops! Sorry, something has gone wrong with this page. Please try again.</strong>");
		    }
	    });
	}
}
