var pageIndex = 0;

var pages   	= ["home", "sprekers", "agenda", "aanmelden", "sponsors", "locatie", "wildcards","geeknight"];
var elements = ["mainpage", "sprekerspage", "agendapage", "aanmeldenpage", "sponsorspage", "locatiepage", "wildcardspage", "geeknightpage"];
        
var $fromElement;
var $toElement;

$(function () {
    $('.namedanchor').remove();
    $('#searchTerm').watermark('Zoek techdays.nl met Bing', { className: 'watermark' });
    $("body,html").css("overflow-x", "hidden");
    $(window).resize($.throttle(100, resize));

    $("#menu a").click(function () { $(this).blur(); });

    $(window).bind('hashchange', function (e) {
        var pageID = e.getState("p");
        pageIndex = $.inArray(pageID, pages);
        if ((!pageIndex) || (pageIndex < 0)) pageIndex = 0;

        $("#menu li a").removeClass("active");
        $toElement = $("#menu li:eq(" + pageIndex + ") a");
        $toElement.addClass("active");

        $("#contentpagesholder div:not(.active) .noClickOverlay").remove();
        $("#contentpagesholder div.active").removeClass("active");

        $("#" + elements[pageIndex]).addClass("active");
        $("#contentpagesholder .page").not(".active").prepend('<div class="noClickOverlay">&nbsp;</div>');
        //$("#" + elements[pageIndex] + " div:not(.active)").prepend('<div class="noClickOverlay">&nbsp;</div>');  

        $("#contentpagesholder div.page:not(.active)").css("opacity", 0.5);
        $("#contentpagesholder div.page.active").css("opacity", 1.0);

        var curLangSwitchUrl = $("#langswitchlink").attr("href");
        var newLangSwitchUrl = $.param.fragment(curLangSwitchUrl, { p: pageID });
        $("#langswitchlink").attr("href", newLangSwitchUrl);

        slide();
    })

    $(window).trigger('hashchange');

    $("#leftarrow").click(PreviousPageClick);
    $("#rightarrow").click(NextPageClick);

    $('.ctaslides').cycle({ fx: 'fade' });

    AdjustScrollBars();
    
    $("#initial-click").click(function (event) {
        event.preventDefault();
        $('#initial-box').hide();
        $('#secondary-box').show();
    });
    
    $("#twitterholder").tweet(
    {
        avatar_size: 32,
        count: 4,
        query: "#techdaysnl",
        loading_text: "",
        loadcallback: function () { $("#cachedtwitterholder").empty(); },
        template: "{time}{join}{text}"
    }).bind("empty", function () { $(this).append("No matching tweets found"); });
});

function GoToPage(index) 
{
    $.bbq.pushState("p=" + pages[index]);    
}

function NextPageClick() 
{
    if (pageIndex >= (pages.length - 1)) {
        GoToPage(0);
    } else {
        GoToPage(pageIndex + 1);
    }
}

function PreviousPageClick() 
{
    if (pageIndex < 1) return;
    GoToPage(pageIndex-1);
}

function slide() {
	if (pageIndex < 1)
	{
		$("#leftarrow").hide();
	} else
	{
		$("#leftarrow").show();
	}

    var windowWidth = $(window).width();
    var leftPos = (-pageIndex - 1) * 910 + windowWidth / 2 + 880 / 2 + 20;

    if ($fromElement) {
        $fromElement.effect("transfer", { to: $toElement }, 500, function () { $fromElement = $toElement; });
    } else {
        $fromElement = $toElement;
    }
            
    $("#contentpagesholder").animate({ 'left': leftPos, duration: 1.0 });
}
		
function AdjustScrollBars(){
			
	//if (($(window).width()<=800) && ($(window).height()<=600)) {
	if ($(window).width()<=1000) {
		$("body,html").css("overflow-x", "auto");			
	} else {
		$("body,html").css("overflow-x", "hidden");	
	}		
}
function resize()
{
    var windowWidth = $(window).width();
    var leftPos = (-pageIndex-1) * 910 + windowWidth / 2 + 880 / 2 + 20;
    $("#contentpagesholder").css('left', leftPos);			
	AdjustScrollBars();
}        
