// dropdown menu
sfHover = function() {
var sfEls = document.getElementById("navigation").getElementsByTagName("li");
for (var i=0; i<sfEls.length; i++) {
sfEls[i].onmouseover=function() {
this.className+=" sfhover";
}
sfEls[i].onmouseout=function() {
this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
}
}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
// foto viewer
function fotoScroller(id) {
    var fotocount = $("#" + id + " .fotoholder img").length;
    
	if (fotocount > 1) { 
		$("#"+id+" .fotoholder img:first").clone().appendTo("#"+id+" .fotoholder");
		fotocount=fotocount+1;
		var imgwidth = $("#"+id+" .fotoholder img:first").width();
		var endpoint = ((imgwidth*-fotocount)+imgwidth)+"px";
		$("#"+id+" .nextfoto").addClass("nextbig").click(function(){
	      	if ($("#"+id+" .fotoholder").css("left") == endpoint) { $("#"+id+" .fotoholder").css("left","0px");	}
			$("#"+id+" .fotoholder").animate({"left": "-="+imgwidth+"px"}, "slow");
	    }); 
		$("#"+id+" .prevfoto").addClass("prevbig").click(function(){
	      	if ($("#"+id+" .fotoholder").css("left") == "0px") { $("#"+id+" .fotoholder").css("left",endpoint);	}
			$("#"+id+" .fotoholder").animate({"left": "+="+imgwidth+"px"}, "slow");
	    });
	}
}
function mediaScroller(id) {
    var fotocount = $("#" + id + " .fotoholder div").length;
    		
	if (fotocount > 1) { 
		$("#"+id+" .fotoholder div:first").clone().appendTo("#"+id+" .fotoholder");
		fotocount=fotocount+1;
		var imgwidth = $("#"+id+" .fotoholder div:first").width();
		var endpoint = ((imgwidth*-fotocount)+imgwidth)+"px";
		$("#"+id+" .nextfoto").addClass("nextbig").click(function(){
	      	if ($("#"+id+" .fotoholder").css("left") == endpoint) { $("#"+id+" .fotoholder").css("left","0px");	}
			$("#"+id+" .fotoholder").animate({"left": "-="+imgwidth+"px"}, "slow");
	    }); 
		$("#"+id+" .prevfoto").addClass("prevbig").click(function(){
	      	if ($("#"+id+" .fotoholder").css("left") == "0px") { $("#"+id+" .fotoholder").css("left",endpoint);	}
			$("#"+id+" .fotoholder").animate({"left": "+="+imgwidth+"px"}, "slow");
	    });

        $("#" + id).mouseenter(function () { clearTimeout(mediatimer); });
        $("#" + id).bind('mouseleave', function () {
            clearTimeout(mediatimer);
            mediatimer = setTimeout("keepMovin('" + id + "','" + endpoint + "'," + imgwidth + ")", 5000);
        });
        $("#pause").click(function () {
            clearTimeout(mediatimer);
            if ($(this).attr("rel") == 0) {
                $("#" + id).unbind('mouseleave');
                $(this).fadeOut("fast", function () { $(this).attr("rel", "1").css("backgroundImage", "url(/images/play.png)"); });
            } else {
                $("#" + id).bind('mouseleave', function () { mediatimer = setTimeout("keepMovin('" + id + "','" + endpoint + "'," + imgwidth + ")", 4000); });
                $(this).fadeOut("fast", function () { $(this).attr("rel", "0").css("backgroundImage", "url(/images/pause.png)"); });
            }
        });
        $("#" + id + " .fotoframe").mouseenter(function () { $("#pause").fadeTo("fast", 0.8); });
        $("#" + id + " .fotoframe").bind('mouseleave', function () { $("#pause").fadeOut("fast"); });
        mediatimer = setTimeout("keepMovin('" + id + "','" + endpoint + "'," + imgwidth + ")", 4000);
    }
}
function keepMovin(id, endpoint, imgwidth) {
    if ($("#" + id + " .fotoholder").css("left") == endpoint) { $("#" + id + " .fotoholder").css("left", "0px"); }
    $("#" + id + " .fotoholder").animate({ "left": "-=" + imgwidth + "px" }, "slow");
    mediatimer = setTimeout("keepMovin('" + id + "','" + endpoint + "'," + imgwidth + ")", 4000);
}
// lightview style pop up
var pid;
function hidePop() { document.getElementById('bopop').style.visibility = 'hidden';document.getElementById(pid).style.display = 'none';ie6Form(); }
function showPop(nid) { 
	if (document.getElementById('bopop').style.visibility == 'visible') { hidePop(); }
	getPageSize();
	document.getElementById('fade').style.height=pageHeight;
	document.getElementById(nid).style.display = 'block';
	document.getElementById('bopop').style.visibility = 'visible';
	pid=nid;
	ie6Form(); 
} 
var pageWidth,pageHeight;
function getPageSize() {	        
    var xScroll, yScroll;	
	if (window.innerHeight && window.scrollMaxY) {	xScroll = window.innerWidth + window.scrollMaxX;yScroll = window.innerHeight + window.scrollMaxY; } 
	else if (document.body.scrollHeight > document.body.offsetHeight){ xScroll = document.body.scrollWidth;yScroll = document.body.scrollHeight; } 
	else { xScroll = document.body.offsetWidth;yScroll = document.body.offsetHeight; }	
	var windowWidth, windowHeight;	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){ windowWidth = document.documentElement.clientWidth; } 
		else { windowWidth = self.innerWidth;	}
		windowHeight = self.innerHeight;
	} 
	else if (document.documentElement && document.documentElement.clientHeight) { windowWidth = document.documentElement.clientWidth;windowHeight = document.documentElement.clientHeight; } 
	else if (document.body) { windowWidth = document.body.clientWidth;windowHeight = document.body.clientHeight; }		
	if(yScroll < windowHeight){	pageHeight = windowHeight; } else { pageHeight = yScroll; }
	if(xScroll < windowWidth){ 	pageWidth = xScroll; } else { pageWidth = windowWidth; }
	return [pageWidth,pageHeight];	
}

function ie6Form() {	
	if (document.all) {
		var version=/MSIE \d+.\d+/;var brwsr=navigator.appVersion.match(version);
		if ((""+brwsr).indexOf("6")>0) { $("td.page *:input").toggle();	}
	}
}

// tool tip code
function showtip(e,message){var x=0;var y=0;var m;var h;if(!e)
var e=window.event;if(e.pageX||e.pageY){x=e.pageX;y=e.pageY;}
else if(e.clientX||e.clientY){x=e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;y=e.clientY+document.body.scrollTop+document.documentElement.scrollTop;}
m=document.getElementById('tooltipmsg');m.style.top=y+6+"px";
var messageHeigth=(message.length/20)*10+25;if((e.clientY+messageHeigth)>510){m.style.top=y-messageHeigth+"px";}
var xp=document.body.offsetWidth;
if((x+150)<xp){m.style.left=x+20+"px";}else if ((x-150)<0) {m.style.left=0+"px";}else {m.style.left=x-150+"px";}
m.innerHTML=message;m.style.display="block";}
function hidetip(){var m;m=document.getElementById('tooltipmsg');m.style.display="none";}


function openEsroWindow(eventID) {
    a = window.open('https://esro8.colo.basicorange.nl/deflint/tickets/hall.asp?event=' + eventID, 'esro', 'height=600,width=655,scrollbars,resizable');
    a.focus();
}

function openEsroBasket() {
    a = window.open('https://esro8.colo.basicorange.nl/deflint/tickets/my_basket.asp', 'esro', 'height=600,width=655,scrollbars,resizable');
    a.focus();
}


$(document).ready(function () {

    /*Target external link*/
    $('a[href^="http://"]').each(function () {
        $(this).attr('target', '_blank')
    })


    $.getJSON("http://twitter.com/statuses/user_timeline.json?screen_name=TheaterDeFlint&count=1O&callback=?", function (tweetdata) {

        var counter = 0;

        $.each(tweetdata, function (i, tweet) {

            var when = ConvertJsonDate(tweet.created_at);
            var month = when.getMonth() + 1;
            month = (month < 10) ? '0' + month : month;
            var second = when.getSeconds();
            second = (second < 10) ? '0' + second : second;
            var minute = when.getMinutes();
            minute = (minute < 10) ? '0' + minute : minute;
            var pdate = when.getDate() + '-' + month + '-' + when.getFullYear() + ' ' + when.getHours() + ':' + minute + ':' + second + " - ";

            if (counter == 0) {
                $('.twitter').append('<span class="tweet">' + pdate + '<br />' + replaceURLWithHTMLLinks(tweet.text) + '</span>');
            }
            else {
                $('.twitter').append('<span class="tweet" style="display:none;">' + pdate + '<br />' + replaceURLWithHTMLLinks(tweet.text) + '</span>');
            }

            counter = counter + 1;

        });
    });


    $('a.carousel').click(function () {

        if ($(this).attr('href') == '#carouselNext') {
            var el = $('.tweet:visible').prev('.tweet');
            $('.tweet:visible').hide();
            if (el.length) {
                el.fadeIn(500);
            }
            if (el.next('.tweet').text() == '') {
                $(".next-tweet").hide();
            }
            else {
                $(".next-tweet").show();
            }
            if (el.prev('.tweet').text() == '') {
                $(".next-tweet").hide();
            }
            else {
                $(".prev-tweet").show();
            }
        }

        if ($(this).attr('href') == '#carouselPrev') {
            var el = $('.tweet:visible').next('.tweet');
            $('.tweet:visible').hide();
            if (el.length) {
                el.fadeIn(500);
            }

            if (el.prev('.tweet').text() == '') {
                $(".next-tweet").hide();

            }
            else {
                $(".next-tweet").show();

            }

            if (el.next('.tweet').text() == '') {
                $(".prev-tweet").hide();
            }
            else {
                $(".prev-tweet").show();
            }

        }

        return false;

    });



});


function ConvertJsonDate(date) {

    var dateArray = date.split(' ');
    var gmtDate = new Date(
          Date.parse(dateArray[0] + ', ' + dateArray[2] + ' '
             + dateArray[1] + ' ' + dateArray[3] + ' '
             + dateArray[5].substring(0, 4))
        );
    var cetDate = new Date();
    cetDate.setTime(3600000 + gmtDate.getTime());

    return cetDate;
}


function replaceURLWithHTMLLinks(text) {
    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    return text.replace(exp, "<a href='$1'>$1</a>");
}
