var page = 1;
var flashMovieWithVars = null;
function getDivByItemName(itemId){
    return itemId+'1';
}
function generatePages(item, itemsPerPage, additionalPaths) {
     var pageContent = getDivByItemName(item);
     //getting the amount of elements inside content div
     var number_of_items = $('#'+pageContent+additionalPaths).children().size();
     //calculate the number of pages we are going to have
     var number_of_pages = Math.ceil(number_of_items/itemsPerPage);

     if (number_of_pages == 1 ){
         return;
     }
    
     var divs = $('#'+pageContent+additionalPaths).find('div.item');
     var navigation_html = '';
     var current_link = 0;
     
     while(number_of_pages > current_link){
         var href = item+'_'+(current_link +1);
         navigation_html += '<a id="'+pageContent+'-link'+(current_link + 1)+'" class="page_link p'+(current_link +1)+'" href="javascript:void(0);" onclick="changePage(\''+item+'\', '+(current_link+1)+', \''+href+'\')"><!-- --></a>';
         divs.slice(current_link*itemsPerPage,(current_link +1)*itemsPerPage).wrapAll('<div id="'+pageContent+'-page'+(current_link + 1)+'" class="add pagination" />');

         current_link++;
     }

     $('#'+pageContent+additionalPaths).children().css('display', 'none');
     $('#'+pageContent+additionalPaths).append('<div id="'+pageContent+'Pages" class="pagination paginationLinks">'+navigation_html+'</div><div class="clear"><!-- --></div>');
     $('#'+pageContent+' img.title').css('display', 'block');
     //add active_page class to the first page link
     $('#'+pageContent+'Pages a:first').addClass('p1active');

     $('#'+pageContent+'-page1').css('display', 'block');
}

function addressChange(addr) {
   try {
    flashObj.phpAddress(addr);
    } catch (e) {
        //alert(e);
        //alert('Address error');
    }
/*
	flashMovieWithVars.flash(
		function() {
                    try {
			this.phpAddress(addr);
                    } catch (e) {
                        //alert(e);
                        //alert('Address error');
                    }
		}
	);
	*/
}

function changeFlashSection(section){
    try {
        flashObj.loadModule(section);
    } catch (e) { }
    /*
    flashMovieWithVars.flash(
		function() {
                    try {
			this.loadModule(section);
			$.scrollToElement($('#flashObj'));
                    } catch (e) {
                        //alert(e);
                        //alert('Address error');
                    }
		}
	);
	*/
}

function changeFlashTab(section){
    try {
        flashObj.updatePage(section);
    } catch (e) {
    }
    /*
    flashMovieWithVars.flash(
		function() {
                    try {
			this.updatePage(section);
			
                    } catch (e) {
                        //alert(e);
                        //alert('Address error');
                    }
		}
	);
	*/
}

function changePage(item, clickedPage, pag){
    //console.log('called change page');
    //console.log(item, clickedPage);
    if (pag !== '')
        addressChange(pag);
    var pageContent = getDivByItemName(item);//alert(page);alert(clickedPage);
    var pgID = '#' + pageContent + '-page' + page;
    var evalStr = '';
    if ((isNaN(clickedPage) == true) && (clickedPage.length == 1)) {
        clickedPage = clickedPage[0];
    }
    if (isNaN(clickedPage) == true) {
        
        pageContent = item;
        pgID = '.headQ';
        var strs = clickedPage;
        evalStr = '$("#'+pageContent+'_'+strs[0]+'1").css("display", "block")';
    //console.log('here1','"#'+pageContent+'_'+strs[0]+'1"');
        pageContent = item+'_'+strs[0]+'1';
        //alert('Old page nr. '+page);
        clickedPage = strs[1];
        $('#'+pageContent+' div.add').each(function(i) {$(this).css('display', 'none');});
        $('#'+pageContent+'Pages a').each(function(i) {var j=i+1; $(this).removeClass('p'+j+'active');})
        $('.head_menu').each(function(i) {
            var className = $(this).attr('id');
            if (strs[0]+'Active' !== className)
                $(this).removeClass(className);
            else
                $(this).addClass(className);
        })
    } else {
        
        $('#'+pageContent+' div.add').each(function(i) {
            $(this).css('display', 'none');
        });
        $('#'+pageContent).css('display', 'block');
        //alert('Page content '+pageContent);//alert('#' + pageContent + '-page' + clickedPage);
    }
	//console.log('page content' + pageContent);
   $(pgID).fadeOut('slow', function(){
       //console.log('here2','#' + pageContent + '-page' + page);
       $('#' + pageContent + '-page' + page).css("display", "none");
       //console.log('here3','#' + pageContent + '-page' + clickedPage);
       //console.log('#' + pageContent + '-page' + clickedPage);
       $('#' + pageContent + '-page' + clickedPage).fadeIn('fast', function (){
        //console.log(evalStr);
            if (evalStr !== '') {
                eval(evalStr);
            }
            
            
        });
    });

    //change current page
    page = clickedPage;//alert(page);
    $('#'+pageContent+'Pages a').each(function(i) {
        var p = i+1;
        $(this).removeClass('p'+p+'active');
    });
    $('#'+pageContent+'-link'+page).addClass('p'+page+'active');
    $('#' + pageContent + '-page' + page).css('display', 'block');


    if (pageContent == 'headquarters1') {
        $('#'+pageContent+'-page1').css('display', 'block');
    } else {
	$('#'+pageContent).fadeIn();
    }
}

function splitHref() {
    var locals = window.location.href.split('#', 2);
    if (locals.length > 1) {
    var local = locals[1].split('_', 5);//alert(local);
    $("#nav").find('a').each(function(i) {
        if ('#'+local[0] == $(this).attr('role')) {
            var currentPage = 1
            if (it == 2)
                currentPage='';

            if (local.length > 1)
                currentPage = local[1];
            
            var it = i+1;
            googleAction(local[0], 'href');
            if (local.length > 2)
                currentPage = local[1]+'_'+local[2];//+'_'+local[3];
            changeTabs(it, currentPage, '');
        }
    });
} else {
    changeTabs('news', 1);
}

}
/*
* item = the number of the tab being pressed
* action = the type of action performed (link, href, flash, etc)
 */
function googleAction(itemName, action, pageNr) {
    //console.log('Category '+itemName+' action '+action+' pageNr '+pageNr);
    var categ = capitaliseFirstLetter(itemName);
    var act = capitaliseFirstLetter(action)
    if (pageNr == undefined)
        _gaq.push(['_trackEvent', categ, act, categ+' by '+action]);
    else
        _gaq.push(['_trackEvent', categ, act, categ+' by '+action+' on page '+pageNr]);
}

function changeTabs(item, pageNr, move) {
    //console.log('calling changeTabs');
    //console.log(item, pageNr);
    if (item == 'headquarters' && !isNaN(pageNr)) {
            changeTabs('headquarters', new Array('gameplay', '1'), move);
            return;
    }
    resetHeights();

    //alert('in change tab');
    $('.head_menu').each(function(i) {
        var className = $(this).attr('id');
        $(this).removeClass(className);
    })
    
    if ( (move !== undefined) && (move == true) ) {;
        //$.scrollToElement($('#cont'));
        //console.log('e4ee'+move);
    }
//console.log('called change tabs');
    var page = pageNr;
    try{
		if (isNaN(pageNr)) {
			page = pageNr[0];
			var i = 1;
			while (i < pageNr.length) {
				page = page+'_'+pageNr[i];
				i++;
			}//alert(item+'_'+page);
                        addressChange(item+'_'+page);
                        //console.log(item);
                        if ((pageNr[0] == 'character') && (phase == 1)){
                            $('.main').css('height', '2400px');
                            $('.scrollable').css('height', '1700px');
                            $('.contentLeftContent').css('height', '1700px').css('background-image', 'url("images/left_long.png")');
                            $('.contentRightContent').css('height', '1700px').css('background-image', 'url("images/right_long.png")');
                            $('.contentMiddleContent').css('height', '1700px');
                            $('.contentHolder').css('height', '1700px');
                        } else {
                            resetHeights();
                        }
		} else {//alert(item+'_'+pageNr);
                    addressChange(item+'_'+pageNr);
		}
    } catch (e) {

    }
    //window.location.hash = item+'_'+pageNr;
    $('.articlesSummary').css('display', 'block');
    $('.articlesLarge').css('display', 'none');//alert($('#'+menuItem+'_link').attr('role'));
    api.move( $('#'+item+'_link').attr('role') - $('#'+menuItem+'_link').attr('role') );
    menuItem = item;//alert('page nr'+pageNr);
    $('.headQ').css('display', 'none');

    changePage(item, pageNr, item+"_"+page);
    if ( (move !== undefined) && (move == true) ) {
        $.scrollToElement($('#cont'));
    } else {
        ;//$.scrollToElement($('#gus'));
    }
    return false;
}

function resetHeights(){
    $('.main').css('height', '1850px');
    $('.scrollable').css('height', '1104px');
    $('.contentLeftContent').css('height', '1100px').css('background-image', 'url("images/left_long.png")');
    $('.contentRightContent').css('height', '1100px').css('background-image', 'url("images/right_long.png")');
    $('.contentMiddleContent').css('height', '1100px');
    $('.contentHolder').css('height', '1100px');
}

function gotoArticle( articlesId, articleId,item, pageNr){
    //console.log('calling gotoArticle');
    //console.log(articlesId, articleId,item, pageNr);
    changeTabs(item, pageNr, true);
    setTimeout('showArticle("'+ articlesId + '","' + articleId + '")', 1000);
}

function showArticle(articlesId, articleId) {
    //console.log('calling showArticle');
    //console.log(articlesId, articleId);
    if(articleId == 'story1-art2'){
        $('.main').css('height', '2400px');
        $('.scrollable').css('height', '1700px');
        $('.contentLeftContent').css('height', '1700px').css('background-image', 'url("images/left_long.png")');
        $('.contentRightContent').css('height', '1700px').css('background-image', 'url("images/right_long.png")');
        $('.contentMiddleContent').css('height', '1700px');
        $('.contentHolder').css('height', '1700px');
    } else {
        var tmp = articleId.split('-', 2)[0];
        if (tmp == 'distribution1') {
            $('.main').css('height', '2000px');
            $('.scrollable').css('height', '1300px');
            $('.contentLeftContent').css('height', '1300px').css('background-image', 'url("images/left_long.png")');
            $('.contentRightContent').css('height', '1300px').css('background-image', 'url("images/right_long.png")');
            $('.contentMiddleContent').css('height', '1300px');
            $('.contentHolder').css('height', '1300px');
        } else {
            resetHeights();
        }
    }
    
    $.scrollToElement($('#cont'));
    //console.log(articlesId +'___'+ articleId);
    $('.article').css('display', 'none');
    //console.log('start show');
    $('.articlesHide').fadeOut('slow', function(){
        //console.log('start fade in');
       $(articlesId+'_articles').fadeIn('fast', function (){
            //$('#additionalLinks').hide();
            $('#'+articleId).show();
        });
    });
    //console.log('end move___'+ articleId);
}

function hideArticle(articlesId, articleId) {
    //console.log('hide art');
    $(articlesId+'_articles').fadeOut('slow', function(){
        $(articlesId).fadeIn('fast', function (){
            $('#'+articleId).hide();
            //$('#additionalLinks').show();
        });
    });

    if (articlesId == '#headquarters_character1') {$('.main').css('height', '2400px');
        $('.scrollable').css('height', '1700px');
        $('.contentLeftContent').css('height', '1700px').css('background-image', 'url("images/left_long.png")');
        $('.contentRightContent').css('height', '1700px').css('background-image', 'url("images/right_long.png")');
        $('.contentMiddleContent').css('height', '1700px');
        $('.contentHolder').css('height', '1700px');
    } else {
        resetHeights();
    }
}

function capitaliseFirstLetter(string)
{
    return string.charAt(0).toUpperCase() + string.substr(1);
}

