// JavaScript Document
// Function to allow one JavaScript file to be included by another.
// Copyright (C) 2006-08 www.cryer.co.uk
function js_include(jsFile)
{
  var url = '';
  document.write('<script type="text/javascript" src="' + url + jsFile + '"></scr' + 'ipt>'); 
} 

//js_include('/JavaScript/jQuery/jquery.form.js');
//js_include('/JavaScript/jQuery/Submit-Ajax-Form.js');


var f = [AddAdvertismentNotice, CountDownTimer, StyleOptions, Fav, GetRecommended, ChangeStyle, Carousel];

//Execute this function on document.ready: includes all functions
function __init__(options)
{
    if(!options || !f)
        return false;

    try
    {
            start(f,options); // iterate through the available function list and try to execute each function in the 'f' var
    }catch(err){alert()}
}

//Start the functions
function start(f /*the function list*/, options /*the options object*/)
{
    for(i in f)
    {
            var fn = f[i]; // get the function name
            try{fn(options);}catch(err){alert(err)} // try to execute the function
    }
}

function ChangeStyle(options)
{
    var page = options.page;

    if(page != 'submit' && page != 'contact' && page != 'link to us')
        return false;

    $('.linktous, #gameForm_table textarea, #gameForm_table input:not(:file, :button, :submit, :reset), #gameForm_table select,\n\
        #contactForm_table textarea, #contactForm_table input:not(:file, :button, :submit, :reset), #contactForm_table select')
    .bind("mouseover mouseout", function(e){
        $(this).toggleClass("sbg-inputHover");
	})
    .bind("focus blur", function(e)
    {
        $(this).toggleClass("sbg-inputFocus");
    });
    return false;
}


function FillGame(options)
{
    var page = options.page;
    var game_swf = options.game_swf;
    var game_height = options.game_height;
    var game_width = options.game_width;
    var container = options.container;

    if(page != 'ingame')
            return false;

    $('#SBG-GameContainer, #SBG-Options, #SBG-GameDescription, #SBG-GameOutline').show();

    var fo = new FlashObject(game_swf, "FlashObject", game_width, game_height, "6", "");
    fo.addParam("allowScriptAccess", "sameDomain");
    fo.addParam("quality", "best");
    fo.addParam("loop", "true");
    fo.addParam("allowFullScreen", "true");
    fo.addParam("wmode", "opaque");

    fo.write(container);

    return false;
}

var playTheGame = false;
var startBtnName = 'PlayTheGame';

function AddPlayBtn(options)
{
	var page = options.page;

        if(page != 'ingame')
            return false;
		
	if(playTheGame == true)
		return false;

	var style = 'display: none;';
	var startBtn = '<a id="'+startBtnName+'" style="'+style+'" href="#"></a>';
	
	$('#SBG-Progress').after(startBtn);
	$('#'+startBtnName).text('Click here to close the ad and start the game!');
	$('#'+startBtnName).fadeIn('slow');
	
	function fadeOut()
	{
            var preGameAd = $('.sbg-336:eq(0)');
            var preGameAd_announcement = $('.advertisment:eq(1)');

            playTheGame = true;
            $('#'+startBtnName).hide().empty().remove();
            preGameAd.hide();
            preGameAd_announcement.hide();
            FillGame(options);
            return false;
	}
		
	function onClick()
	{
            if(playTheGame == true)
            {
                    $(this).hide().empty().remove();
                    return false;
            }

            fadeOut();
            return false;
	}
	
	$('#'+startBtnName).click(onClick);
}


function CountDownTimer(options)
{
	var page = options.page;

        if(page != 'ingame')
            return false;
				
	
	function callBack_function()
	{
            AddPlayBtn(options);
            $('#SBG-Progress').hide();
	}
	
	var countDownOpts =  { opts: options, callBack: callBack_function };
	
	$('#SBG-CountDown').countDown(countDownOpts);

        return false;
}

function ShowRandomExtra(options)
{		
    var page = options.page;

    if(page != 'ingame')
            return false;

    var container = '#SBG-FeaturedGames';
    var loading = '#LoadingExtraGames';
    var getParam = 'extra';
    var no = 4;

    function successFunction(html)
    {
      $(container).html(html);
      $(loading).hide();
    }

    var ajaxOptions =
    {
            type:'GET',
            url:'/Modules/ShowGames/ShowGames.class.php',
            data: getParam+'=true&no='+no,
            dataType: 'html',
            success: successFunction
    }

    function getTheGames()
    {
            $.ajax(ajaxOptions);
    }

    getTheGames();
}



function bookmarksite(title,url)
{ 
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print)
	{ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	//if(document.all)// ie
		//window.external.AddFavorite(url, title);
}


function AddAdvertismentNotice(options)
{
    var page = options.page;

    if(page != 'ingame')
        return false;

    var advertisement = '<div class="advertisment">Advertisement</div>';
    $('.sbg-336, .sbg-160').before(advertisement);
    //$('.sbg-728').before(advertisement);
}

function StyleOptions(options)
{
    try
    {
        $('.sbg-options').each(function(i)
        {
                var action = $(this).attr('id');
                var opts = {'background-image':'url(/Images/'+action+'.png)'};
                $(this).css(opts);
        });
    }catch(err){}
}

function Fav(options)
{
    var page = options.page;

    if(page != 'ingame')
        return false;

    function onClik()
    {
            bookmarksite(document.title, document.URL);
            return false;
    }

    try
    {
            $('#fav').click(onClik);
    }catch(err){}
}

function GetRecommended(options)
{
    var page = options.page;

    if(page == 'extra' || page == 'submit')
        return false;

    try
    {
        var container = '#RECOMM';
        var loading = '#LoadingRecommendedGames';

        function successFunction(html)
        {
          $(container).html(html);
          $(loading).hide();
        }

        var opts =
        {
                type:'GET',
                url:'/Modules/ShowGames/ShowGames.class.php',
                data: 'rec=true',
                dataType: 'html',
                success: successFunction
        }

        function fetchGames(o) { $.ajax(o); }

        fetchGames(opts);
    }

    catch(err){}
}

function Carousel(options)
{
    var page = options.page;

    if(page != 'home' && page != 'category')
        return false;

    var carousel_options =
        {
            vertical: true,
            scroll: 2
        }

    $('#mycarousel').jcarousel(carousel_options);
}


function CheckUserForm()
{

    if($('#gameMeta_author').val() == '')
    {
        alert('Please type in your name!');
        $('#gameMeta_author').focus();
        return false;
    }

    if($('#gameMeta_email').val() == '')
    {
        alert('Please type in your email address!');
        $('#gameMeta_email').focus();
        return false;
    }

    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if(reg.test($('#gameMeta_email').val()) == false)
    {
            alert("Please type a valid email adress!");
            $('#gameMeta_email').focus();
            return false;
    }

    if($('#gameMeta_website').val() == '')
    {
        alert('Please type in the address of your website!');
        $('#gameMeta_website').focus();
        return false;
    }

    var web_reg = /^www\.(.[^.]*)\.([A-Za-z]{2,4})$/;
    if(web_reg.test($('#gameMeta_website').val()) == false)
    {
            alert("Please type a valid website adress like: www.yoursite.com!");
            $('#gameMeta_website').focus();
            return false;
    }

    if($('#game_category').val() == '')
    {
        alert('Please select a category for your game!');
        $('#game_category').focus();
        return false;
    }

    if($('#game_name').val() == '')
    {
        alert('Please insert a name for your game!');
        $('#game_name').focus();
        return false;
    }

    if($('#game_keywords').val() == '')
    {
        alert('Please write a few keywords for your game!');
        $('#game_keywords').focus();
        return false;
    }

    if($('#game_description').val() == '')
    {
        alert('Please describe your game!');
        $('#game_description').focus();
        return false;
    }

    if($('#game_thumb').val() == '')
    {
        alert('Please select a thumbnail for your game!');
        $('#game_thumb').focus();
        return false;
    }

    if($('#game_swf').val() == '')
    {
        alert('Please select a flash file for your game!');
        $('#game_swf').focus();
        return false;
    }


    if($('#game_agreement:checked').val() != 'true')
        {
            alert('Please check the agreement box before you submit!');
            return false;
        }


    return true;
}

function CheckContactForm()
{

    if($('#contact_name').val() == '')
    {
        alert('Please type in your name!');
        $('#contact_name').focus();
        return false;
    }

    if($('#contact_email').val() == '')
    {
        alert('Please type in your email address!');
        $('#contact_email').focus();
        return false;
    }

    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if(reg.test($('#contact_email').val()) == false)
    {
            alert("Please type a valid email adress!");
            $('#contact_email').focus();
            return false;
    }

    if($('#contact_website').val() != '')
    {
        var web_reg = /^www\.(.[^.]*)\.([A-Za-z]{2,4})$/;
        if(web_reg.test($('#contact_website').val()) == false)
        {
                alert("Please type a valid website adress like: www.yoursite.com!");
                $('#contact_website').focus();
                return false;
        }
    }

    if($('#contact_message').val() == '')
    {
        alert('Please write something in your message!');
        $('#contact_message').focus();
        return false;
    }
    
    return true;
}