function rolloverMap() {

	$(".marker").hover(function(){
		$(this).find("img").fadeIn(200).css({"display":"block"});
		$(this).css({'z-index':'1000'});
    },function(){
		$(this).find("img").fadeOut(200);
		$(this).css({'z-index':'0'});
    });

}



function tabs() {
	$("ul.tabs").tabs("div.most-read-panes > div");
	$("ul.tabs-bottom").tabs("div.panes-bottom > div");
	$("ul.tabs-video").tabs("div.panes-video > div");
	$("ul.stabs").tabs("div.panes > div.item");
	$("#media-tabs ul.tabs").tabs("#media-tab-body > div.tab-body");
	$("ul.tabs-covers").tabs("div.panes-items > div");
}



function scrollable() {
	$("#n-agent .n-sub-c-02 .scrollable").scrollable();
	$("#n-agent .n-sub-c-03 .scrollable").scrollable();
	$("#n-events .n-sub-c-02 .scrollable").scrollable();
	$("#n-events .n-sub-c-03 .scrollable").scrollable();
	$("#slideshow .scrollable").scrollable();
	$("#slideshow-events .scrollable").scrollable();
	$("#slideshow-video .scrollable").scrollable();
	$(".calendar .scrollable").scrollable();
}

function gallery() {

	var slide = $("#featured-slideshow");
	slide.find(".nav li:first").addClass("selected");	
	slide.find(".content .item:first").show();	
	var getTotal = $("#featured-slideshow .item").size();
	$("span#number-total").html(getTotal);
	$("#featured-slideshow .nav li a").click(function() {
			
		//remove "selected" class & hide current item	
		slide.find(".selected").removeClass("selected");
		slide.find(".item").hide();
		var getNumber = $(this).attr("rel");
		$("span#number").html(getNumber);
		
		//get link, and then show selected item
		$(this).siblings("div").hide();
		var getID = $(this).attr("href");
		slide.find(getID).fadeIn(300);
		$(this).parent().addClass("selected");
		$("#featured-slideshow .nav ul div").show();
				
		return false;
		
	});



	var slideS = $("#featured-slideshow-s");
	slideS.find(".nav li:first").addClass("selected");	
	slideS.find(".content .item:first").show();	
	$("#featured-slideshow-s .nav li a").click(function() {
			
		//remove "selected" class & hide current item	
		slideS.find(".selected").removeClass("selected");
		slideS.find(".item").hide();
		
		//get link, and then show selected item
		$("#featured-slideshow-s .nav ul div").show();
		$(this).siblings("div").hide();
		var getID = $(this).attr("href");
		slideS.find(getID).fadeIn(300);
		$(this).parent().addClass("selected");
		$(this).parent().find("div").show();
		
		return false;
		
	});
	
	miniSlideShowTimer($("#featured-slideshow-s .nav li a").length);
	
	
	
}

function miniSlideShowAdvance(position) {
    $("#featured-slideshow-s .nav li a:eq(" + position + ")").click();
}

function miniSlideShowTimer(arrSize) {
    current = 0;
    limit = arrSize - 1;
    advance = setInterval(function(){current++;if(current>limit)current = 0;miniSlideShowAdvance(current);},6000);
}

function fontResizer() {

	$(".font-s").click(function() {
	    $("#article-body").css({"font-size":"10px","line-height":"15px"});
	    $("#article-body p").css({"font-size":"10px","line-height":"15px"});
		return false;
	});
	$(".font-m").click(function() {		
		$("#article-body").css({"font-size":"14px","line-height":"18px"});
		$("#article-body p").css({"font-size":"14px","line-height":"18px"});
		return false;
	});
	$(".font-l").click(function() {
	    $("#article-body").css({"font-size":"18px","line-height":"22px"});
	    $("#article-body p").css({"font-size":"18px","line-height":"22px"});
		return false;
	});

}

function sfish() {
    $('ul.sf-menu').superfish({
	    delay:       120,                            // one second delay on mouseout 
        animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
        speed:       175,                          // faster animation speed 
        dropShadows: false                            // disable drop shadows 
    });
    $("#n-topics, #n-opinion, #n-video, #n-agent, #n-events").hover(
        function(){
            $(this).find("a.sf-with-ul").css({'background-color':'#fff','color':'#000'});
        },
        function(){
            $(this).find("a.sf-with-ul").css({'background-color':'#000','color':'#fff'});
        }
    );
}





$(document).ready(function(){
    sfish();
    brightcove.createExperiences();
 	rolloverMap();
	tabs();
	scrollable();
	gallery();
	fontResizer();
	hideEmpties();
	tweakDesign();
	brightcoveHomeTab();
	myCaptureHomeTab();
});

function newBrowserWindow(url, windowWidth, windowHeight) {    
    window.open(url,"tw","status=0,toolbar=0,location=0,menubar=0,resizable=0,scrollbars=1,width=" + windowWidth + ",height=" + windowHeight);
}

function hideEmpties() {
    if ($.trim($("#left-col-small").html()).length == 0) {
        $("#left-col-small").hide();
    }
}

function tweakDesign() {
    $(".travel42 .item li div").after("<div class=\"cb\"></div>");
    
    $(".travel42 .item li:odd").addClass("lightgray");    
    $(".rcboxes .module-top h2").before("<div>Test</div>");    
    $(".rcboxes .module-top h2").css({opacity:1});
    $(".rcboxes .module-top div").css({'position':'absolute','width':'100%','height':'100%','background-color':'#000','z-index':'601'});
    $(".rcboxes .module-top div").fadeTo(0,.5);
    $(".newsindex:nth-child(3n)").after("<div class=\"cb\"></div>");
    var url = document.location.href;
    if (url.indexOf("#optins") > 0) {
        $("#optins").attr("tabindex","99");        
        $("#optins").focus();
    }
}

function makeBrightcovePlayer(playerID,videoID,domID) {
    var playerName = "TW" + videoID.toString();
	var params = {};
	params.playerID = playerID;
	params.videoId = videoID;
	//params.autoStart = "true";
	params.bgcolor = "#000000";
	params.width = "316";
	params.height = "267";
	params.isVid = "true";
	

	var player = brightcove.createElement("object");
	player.id = playerName;
	var parameter;
	for (var i in params) {
		parameter = brightcove.createElement("param");
		parameter.name = i;
		parameter.value = params[i];
		player.appendChild(parameter);
	}

	var playerContainer = document.getElementById(domID);

	brightcove.createExperience(player, playerContainer, true);	
}

function navigateToUrl(url){
    document.location=url;
}		


function brightcoveHomeTab(vid){
    if (vid === undefined){
        if (document.getElementById("bc-player-id")){
            vid = document.getElementById("bc-player-id").innerHTML;
        } else {
            vid = "";
        }
    }
    
    brightcove.removeExperience("tabsBCexp");
       
    var params = {};
    params.playerID = "738694952001";
    params.playerKey = "AQ~~,AAAAii5R3eE~,9tlJeiMJlf9f-mDwctrzQLW3cLp7sciD";
    params.videoId = vid;
    //params.autoStart = "true";
    params.bgcolor = "#000000";
    params.wmode = "transparent";
    params.width = "540";
    params.height = "360";
    params.isVid = "true";
    params.isUI = "true";
    params.dynamicStreaming = "true";
    


    var player = brightcove.createElement("object");
    player.id = "tabsBCexp";
    var parameter;
    for (var i in params) {
        parameter = brightcove.createElement("param");
        parameter.name = i;
        parameter.value = params[i];
        player.appendChild(parameter);
    }

    var playerContainer = document.getElementById("brightcove-home-player");

    if (playerContainer) {
        brightcove.createExperience(player, playerContainer, true);
    }
    
    return false;
}

var player;
var experienceModule;
 
// initial setup in the special onTemplateLoaded() function
function onTemplateLoaded(id) {
  player = brightcove.getExperience(id);
  experienceModule = player.getModule(APIModules.EXPERIENCE);
  experienceModule.addEventListener(BCExperienceEvent.TEMPLATE_READY, onTemplateReady);
  //alert("loaded");
}
 
// event listener for the player being ready
function onTemplateReady(event) {
  experienceModule.removeEventListener(BCExperienceEvent.TEMPLATE_READY, onTemplateReady);
  videoPlayer = player.getModule(APIModules.VIDEO_PLAYER);
  videoPlayer.addEventListener(BCMediaEvent.CHANGE, onMediaChange);
  onMediaChange(null);
    
  //alert("ready");
  $("h4#bcVideoTitle").html(videoPlayer.getCurrentVideo().displayName); /* these only exist on a landing page*/
  $("p#bcVideoDesc").html(videoPlayer.getCurrentVideo().shortDescription);
} 
 
// change the displayName with a new video
function onMediaChange(event) {
    var displayName = videoPlayer.getCurrentVideo().displayName;
    var displayDesc = videoPlayer.getCurrentVideo().shortDescription;
    
    $("#bc-home-title").html(displayName);
    $("#bc-home-desc").html(displayDesc);
}                    

function OpenFAQ(){
    window.open("/content.aspx?id=202852", "FAQ", "width=450,height=400,toolbar=no,scrollbars=yes,resizable");
}

function myCaptureHomeTab() {
    //#mycapture-photo-grid
    
    $("#mycapture-photo-grid li a").click(function(){        
        var articleID = $(this).attr("rel");
        $("#photos").html("<div id=\"ajax-loader\" style=\"position:relative;width:540px;margin:160px 0 0 0;text-align:center;\"><img src=\"/_images/ajax-loader.gif\" alt=\"loading...\"/></div>");
        
        
        $.ajax({
            type: "GET",
            url: "/mycapture/albums/" + articleID + ".xml",
            dataType: "xml",
            success: function(xml) {
                //alert(xml);                
                var i = 1;
                var newAlbumStr = "";
                var imagesXML = $(xml).find("Images");
                var iCount = $(imagesXML).find("image").length;
                $(imagesXML).find("image").each(function(){
                    
                    
                    //imgSrc = albumXml.SelectNodes("Images/image")(i).SelectSingleNode("original").InnerText
                    //imgAlt = albumName & " - " & (i + 1).ToString & " of " & albumXml.SelectNodes("Images/image").Count
                    //imgTitle = albumXml.SelectNodes("Images/image")(i).SelectSingleNode("imagetitle").InnerText
                    //imgDesc = albumXml.SelectNodes("Images/image")(i).SelectSingleNode("imagedescription").InnerText
                    //imgWidth = CInt(albumXml.SelectNodes("Images/image")(i).SelectSingleNode("originalwidth").InnerText)
                    //imgHeight = CInt(albumXml.SelectNodes("Images/image")(i).SelectSingleNode("originalheight").InnerText)

                    var albumName = $(this).find("albumname").text();
                    var imgSrc = $(this).find("original").text();
                    var imgAlt = albumName + " - " + i.toString() + " of " + iCount.toString();
                    var imgTitle = $(this).find("imagetitle").text();
                    var imgDesc = $(this).find("imagedescription").text();
                    var imgWidth = $(this).find("originalwidth").text();
                    var imgHeight = $(this).find("originalheight").text();
                    var imgAttr = "";
                    
                    if (imgWidth > imgHeight){
                        imgAttr = " width=\"540\" ";
                    } else {
                        imgAttr = " height=\"360\" ";
                    }
                    
                    newAlbumStr += "<li><div class=\"photo\"><img src=\"" + imgSrc + "\" alt=\"" + imgAlt + "\" " + imgAttr + "/></div>";
                    newAlbumStr += "<div class=\"info\"><h4>" + albumName + "</h4>";
                    
                    if (imgDesc.length > 0) {
                    newAlbumStr += "<p>" + imgDesc + "</p>";
                    } else {
                    newAlbumStr += "<p>" + imgAlt + "</p>";
                    }

                    newAlbumStr += "</div></li>";
                    
                    i++;
                });
                
                $("#photos").html("<ul>" + newAlbumStr + "</ul>");
                initHomePhotos(0,0);
            }            
        });
        
        return false;
    });
}


// variables for Fade Popup
var div_main, div_popupWrapper, div_popup, frame_popup, browserHeight, browserWidth, scrollTop, scrollLeft, isFirstRun_popIn = true, fadeOpacity = .9, fadeTime = 400;
//Fade Popup In
function popIn(type, url, width, height) {
    if (isFirstRun_popIn == true)      //run this section the first time only
    {
        div_main = $(document);
        div_popup = $('#popup');
        div_popupWrapper = $('#popupWrapper').click(popOut);
        div_popupWrapper.width(div_main.width()/*+1000*/).height(div_main.height()/*+1000*/);
        isFirstRun_popIn = false;
    }
    else
        frame_popup.remove();   //remove previous iframe

    if (type == "alert") {
        $('#popupContent').html(url);
    } else {
        //set new iframe
        frame_popup = $('<iframe/>').attr({ id: 'popFrame', frameBorder: '0', height: '95%', width: '100%' }).appendTo('#popupContent');
        if (type == "page")
            frame_popup.attr('src', url);
        else if (type == "image")
            frame_popup.attr('src', '/seeImage.html?url=' + url);
    }
        
    //set popup size
    browserHeight = $(window).height();
    browserWidth = $(window).width();
    if (height == 0)
        height = browserHeight - 100;

    //Set popup position           
    scrollTop = $(window).scrollTop();
    scrollLeft = $(window).scrollLeft();
    if (browserHeight < height)
        div_popup.css('top', scrollTop + 10);
    else
        div_popup.css('top', scrollTop + browserHeight / 2 - height / 2 - 10);
    if (browserWidth < width)
        div_popup.css('left', scrollLeft + 10);
    else
        div_popup.css('left', scrollLeft + browserWidth / 2 - width / 2);


    //fade in popup, fade out everything else
    if (typeof frame_popup != 'undefined') {
        frame_popup.load(function() {
            div_popupWrapper.fadeTo(fadeTime, fadeOpacity);
            div_popup.fadeIn(fadeTime).css('width', width).css('height', height + 20);
        });
    } else {
        div_popupWrapper.fadeTo(fadeTime, fadeOpacity);
        div_popup.fadeIn(fadeTime).css('width', width).css('height', height + 20);
    }
}

//Fade Popup Out
function popOut() {
    div_popup.fadeOut(fadeTime);
    div_popupWrapper.fadeTo(fadeTime, '0').hide();
}

