var slidepos=0;
var slidecount=0;

function checkMobile() {
    var uagent = navigator.userAgent.toLowerCase();

    if ((uagent.search('iphone') > -1) || (uagent.search('ipod') > -1) || (uagent.search('android') > -1)) {
       return true;
    } else {
       return false;
    }
}

function setup() {
    if ($.browser.msie) {
        $('#sidebar').corner("tr br");
    }

    $('#menu').scrollFollow({
        speed: 500,
        offset: 16
    });

    if (screen.width < 1000) {
        $('#centered').css({ 'left': '0px', 'margin-left': '0px' });
        $('body').css({ 'background-position': '-535px 0px' });
    }
}

function setSlideshow(i) {
    slidecount=i;
    //Set up slider
    $.scrollTo.defaults.axis = 'x';
    $.scrollTo(0);
    $('#slides').scrollTo(slidepos);

	$('.previous').hover(
    function () {
        if (slidepos>0) {
            $(this).addClass("hoverOn");
            document.body.style.cursor = "pointer";
        }
    }, 
    function () {
        $(this).removeClass("hoverOn");
            document.body.style.cursor = "default";
        }
    );

	$('.next').hover(
    function () {
        if (slidepos<(slidecount-1)) {
            $(this).addClass("hoverOn");
            document.body.style.cursor = "pointer";
        }
    }, 
    function () {
        $(this).removeClass("hoverOn");
        document.body.style.cursor = "default";
    }
    );

    $('.previous').click(function() {
        $('.next').show();
        if ($('#slide' + slidepos).hasClass("video")) {
            var player = document.getElementById('player' + slidepos);
            player.sendEvent("STOP");
            $('#player' + slidepos).hide();
        }
        slidepos = parseInt(slidepos) - 1;
        if (slidepos <= 0) {
            slidepos = 0;
            $(this).hide();
        }
        $('#slides').stop().scrollTo('#slide' + slidepos, 560, { onAfter: function() { $('#player' + slidepos).show() } });

        setCaption(slidepos);
    });

	$('.next').click(function(){
	    $('.previous').show();
    	if ($('#slide' + slidepos).hasClass("video")) {
            var player = document.getElementById('player'+slidepos);
            player.sendEvent("STOP");
            $('#player'+slidepos).hide();
        }
        slidepos=1+parseInt(slidepos);
        if (slidepos>=(slidecount-1)) {
            slidepos=(slidecount-1);
            $('.next').hide();
        }
        $('#slides').stop().scrollTo('#slide'+slidepos, 560, {onAfter:function(){$('#player'+slidepos).show()}} );

        setCaption(slidepos);
    });

    $('#slideNavigation a').each(function(i) {
        $(this).click(function() {
            if ($('#slide' + slidepos).hasClass("video")) {
                var player = document.getElementById('player' + slidepos);
                player.sendEvent("STOP");
                $('#player' + slidepos).hide();
            }
            slidepos = this.id.substr(3);
            if (slidepos <= 0) {
                slidepos = 0;
                $('.previous').hide();
                $('.next').show();
            } else {
                $('.previous').show();
            }

            if (slidepos >= (slidecount - 1)) {
                slidepos = (slidecount - 1);
                $('.next').hide();
                $('.previous').show();
            } else {
                $('.next').show();
            }

            $('#slides').stop().scrollTo('#slide' + slidepos, 560, { onAfter: function() { $('#player' + slidepos).show() } });
            setCaption(slidepos);
        });
    });

    // This fixes centering for different versions of IE
    if (jQuery.support.leadingWhitespace) {
        var navwidth = (slidecount * 20);
        var navmargin = (560 - navwidth) / 2;
    } else {
        var navwidth = (slidecount * 30);
        if (navwidth<100) {
            navwidth = 100;
        }
        var navmargin = (540 - navwidth) / 2;
        if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
            navmargin = navmargin/2;
        }
    } 
    navwidth += 'px';
    navmargin += 'px';
    $('#slideNavigation').css({ 'width': navwidth, 'margin-left': navmargin });
   
    //Set to default position
    $('.previous').hide();
    setCaption(0);
	
	/* Embed all Flash */
	$('#slides ul li').each(function (i) {
		if ($(this).hasClass('video')) {
			var filename=$(this).children('div.holder').children('div#filename').html();
			var fileheight=$(this).children('div.holder').children('div#fileheight').html();
			var filewidth=$(this).children('div.holder').children('div#filewidth').html();
			var filepreview=$(this).children('div.holder').children('div#filepreview').html();

			var flashvars = {file:filename, image:filepreview, frontcolor:'ffffff', lightcolor:'ff4411', skin:'flash/overlay.swf', controlbar:'over', displayclick:'none'};
			var params = {wmode: 'transparent', allowScriptAccess: 'always'};
			var attributes  = {id: 'player'+i};
			swfobject.embedSWF('flash/player.swf?r='+ new Date().getTime(), 'holder'+i, filewidth, fileheight, '8', false, flashvars, params, attributes);
		}
	});
}

function setCaption(n) {
	var cid = '#caption'+(n);
	var lid = '#url'+(n);
	var nid = '#nav'+(n);

	$('#captions p').each(function (i) {
		$(this).hide();
	});
	$(cid).show();

	$('#slideNavigation div.url').each(function (i) {
		$(this).hide();
	});
	$(lid).show();

	$('#slideNavigation a').each(function (i) {
		$(this).removeClass("currentSlide");
	});
	$(nid).addClass("currentSlide");
}


function showVideo(n) {
    $('#slideshow').hide();
    var vid = '#video' + (n);
    $(vid).show();
    return false;
}

function hideVideo(n) {
    var player = 'player' + n;
    swfobject.getObjectById(player).sendEvent("STOP");
    var vid = '#video' + (n);
    $(vid).hide();
    $('#slideshow').show();
    return false;
}