// JavaScript Document
//Requires JQUERY

$(window).resize(function() {
  resizeWin();
});


function displayPopup(content, popwidth){

    try {
        pageTracker._trackPageview("/popup/" + content);
    } catch (err){
	//	alert("F"+ele);
	}

	
	if ($("#frame").length == 0){
		$('body').append('<div id="frame"><div id="closeButton"><a href="javascript:closePop();"><img src="/assets/images/misc/closebox.png" width="30" height="30" alt="Close" /></a></div><div class="first"><span class="first"></span><span class="last"></span></div><div id="frameContent"></div><div class="last"><span class="first"></span><span class="last"></span></div></div><a href="javascript:closePop();" id="coverLink"><div id="cover" class="blackout"> </div></a>');
	
		if(popwidth!=null){
			$("#frame").css('width', popwidth + "px");	
			$("#closeButton").css('left', (popwidth-10) + "px");
		}
	
		$.ajax({
			url: "/popups/"+content+".html",
			success: function(data) {
				$("#frameContent").html(data);
			}
		});
	
		//$("#frameContent").html('<div style="text-align:center; padding:50px;">Coming Soon</div>');
		
		$("#cover").width($(window).width()).height($(window).height());
		$("#cover").show();
		
		centerDiv("frame");
	}
	
	
}

function centerDiv(myDiv){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#"+myDiv).height();
	var popupWidth = $("#"+myDiv).width();
	var popupLeft = windowWidth/2-popupWidth/2;
	if(popupLeft<0){ popupLeft = 0; }
	//centering
	$("#"+myDiv).css({
		"position": "absolute",
		"left": popupLeft
	});
	
}

function resizeWin(){
	if ($("#frame").length > 0){
		centerDiv("frame");
		$("#cover").width($(window).width()).height($(window).height());
	}
}
  
function closePop(){
	if($("#frameContent object#large_vid").length>0){
		$("#large_vid")[0].onVidClose();
	}
/*
	if(getMovieName("large_vid")!= undefined){
		getMovieName("large_vid").onVidClose();
	}
*/	
	$("#frame").remove();
	$("#cover").remove();
	$("#coverLink").remove();
}


/* SLIDESHOW */
function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 5000 );
});


function getMovieName(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName]
	} else {
		return document[movieName]
	}
}


