﻿// nw_script_v1.0.js
// Author(s): Nehal Shah, Paul Waldschmidt, Michael Trythall, David Johnson
// Version: 1.0
// Created: 7/5/2007
// Updated: 10/16/2007

var Newsweek = window.Newsweek || {};

Newsweek.namespace = function(ns)
{
  if(!ns || !ns.length)
  {
      return null;
  }

  var nw_2 = ns.split(".");
  var nw_3 = Newsweek;

   for(var i=(nw_2[0]=="Newsweek")?1:0;i<nw_2.length;++i)
   {
       nw_3[nw_2[i]] = nw_3[nw_2[i]] || {};
       nw_3 = nw_3[nw_2[i]];
   }
   return nw_3;
};

Newsweek.namespace("Widget");
Newsweek.namespace("Util");


$(document).ready(function(){
 
    Newsweek.Widget.Comments_Reply.init(); 
    Newsweek.Widget.Comments_Report.init();         
    Newsweek.Widget.DynamicBox.init();
    initializeSubscribeForm();              
 
    if($('#header .ad').slice(0,1).is(':visible')) $('#header').addClass('tad');
});

var Paths = {
	InferRootPath : function() {
		var subdirectory = window.location.pathname;
		subdirectory = subdirectory.substring(0, subdirectory.lastIndexOf("/"));
		if (subdirectory.indexOf("/id") > -1) {
			subdirectory = subdirectory.substring(0, subdirectory.indexOf("/id"));
		}
		if (subdirectory.indexOf("/profile") > -1) {
			subdirectory = subdirectory.substring(0, subdirectory.indexOf("/profile"));
		}
		return window.location.protocol + "//" + window.location.host + subdirectory;
	},
	
	Init : function() {
		var defaultPath = Paths.InferRootPath();
		this.RootPath = defaultPath;
		this.FilePath = defaultPath;
		this.StaticContentRoot = defaultPath;
		this.RegistrationHost = defaultPath;
		this.SearchHost = defaultPath;
		
		if (typeof(rootpath) != 'undefined')
		{
			this.RootPath = rootpath;
		}
		if (typeof(filepath) != 'undefined')
		{
			this.FilePath = filepath;
		}
		if (typeof(staticContentRoot) != 'undefined')
		{
			this.StaticContentRoot = staticContentRoot;
		}
		if (typeof(registrationHost) != 'undefined')
		{
			this.RegistrationHost = registrationHost;
		}
		if (typeof(searchHost) != 'undefined')
		{
			this.SearchHost = searchHost;
		}
	},
	
	Current : null
}

Paths.Current = new Paths.Init();


function detectProxy(url)
{
    if(typeof(proxyUrl) != 'undefined')
    {
        var encodedUrl = proxyUrl + '?url=' + encodeURIComponent(url);
        
        return encodedUrl;
    }
    else
    {  
        return url;
    }
}

function initializeSubscribeForm()
{
	// Subscribe form
	$('#subFirstName').focus(function(){
	    if (this.value == 'First Name') this.value = '';
	});
	$('#subLastName').focus(function(){
	    if (this.value == 'Last Name') this.value = '';
	});
	$('#cds_email').focus(function(){
	    if (this.value == 'Email Address') this.value = '';
	});
}

function search(button)
{   
    if(button.id=="site-search-submit")
    {
        $("#searchForm").attr("action", Paths.Current.SearchHost + "/search.aspx");
        $("#searchForm").attr("target", "_self");
    }
    else if (button.id=="web-search-submit") 
    {
		// SMH we don't want to submit the default search parameters to msn
		$("//form[@id='searchForm']/input[@type='hidden']").remove();
        
        $("#searchForm").attr("action", "http://search.msn.com/results.asp");
        $("#searchForm").attr("target", "_blank");
    }
   //document.getElementById("searchForm").submit();
}

function searchTheRoot(id) {
   if (id == 'top') {
        if (Paths.Current.SearchHost.indexOf("Mappingtool") > -1)
        {
            $("#headerSearchForm").attr("action", window.location.protocol + "//" + window.location.host + "/search.aspx");
        }
        else
        {
            $("#headerSearchForm").attr("action", Paths.Current.SearchHost + "search.aspx");            
        }     
     $("#headerSearchForm").attr("target", "_self");
     document.getElementById("headerSearchForm").submit();
   } else {
        if (Paths.Current.SearchHost.indexOf("Mappingtool") > -1)
        {
            $("#footerSearchForm").attr("action", window.location.protocol + "//" + window.location.host + "/search.aspx");
        }
        else
        {
            $("#footerSearchForm").attr("action", Paths.Current.SearchHost + "search.aspx");
        }
     $("#footerSearchForm").attr("target", "_self");
     document.getElementById("footerSearchForm").submit();
   }
}

function getHelp(url) {
    window.open(url,'help', 'width=730,height=600,scrollbars=true,toolbar=no,menubar=no,resizable=true');
}

function logout(host, returnLoc) {
    window.location = host + 'profile/profile.aspx?action=logout&ReturnUrl=' + returnLoc;
}

function columnHeightHack(whoFor) {
    if (whoFor == 'FourColumnFeature') {
        var left = document.getElementById('left');
        var middle = document.getElementById('middle');
        var middleUpper = document.getElementById('middleUpper');
        var middleMiddle = document.getElementById('middleMiddle');
        var middleLowerLeft = document.getElementById('middleLowerLeft');
        var middleLowerRight = document.getElementById('middleLowerRight');
        
        var lh = left.offsetHeight;
        var mh = middle.offsetHeight;

        var mllh = middleLowerLeft.offsetHeight;
        var mlrh = middleLowerRight.offsetHeight;

        if (lh > mh) {
            var mlh = lh - (middleUpper.offsetHeight + middleMiddle.offsetHeight);
            middle.style.height = lh + 'px';
            middleLowerLeft.style.height = mlh + 'px';
        } else {
            var lhm = mh + 10;
            left.style.height = lhm + 'px';
            if (mllh < mlrh) {
                middleLowerLeft.style.height = mlrh + 'px';
            }
        }
    } else if (whoFor == 'FourColumn') {
        var c1o = document.getElementById('left');
        var c2o = document.getElementById('middleLeft');
        var c3o = document.getElementById('middleRight');
        var c4o = document.getElementById('right');
        var c1 = c1o.offsetHeight;
        var c2 = c2o.offsetHeight;
        var c3 = c3o.offsetHeight;
        var c4 = c4o.offsetHeight;
        if ((c1 > c2) && (c1 > c3) && (c1 > c4)) {
            var c1m = c1 - 10;
            c2o.style.height = c1m + 'px';
            c3o.style.height = c1m + 'px';
            c4o.style.height = c1m + 'px';
        } else if ((c2 > c1) && (c2 > c3) && (c2 > c4)) {
            c1o.style.height = c2 + 'px';
            c3o.style.height = c2 + 'px';
            c4o.style.height = c2 + 'px';
        } else if ((c3 > c1) && (c3 > c2) && (c3 > c4)) {
            c1o.style.height = c3 + 'px';
            c2o.style.height = c3 + 'px';
            c4o.style.height = c3 + 'px';
        } else {
            c1o.style.height = c4 + 'px';
            c2o.style.height = c4 + 'px';
            c3o.style.height = c4 + 'px';
        }
    } else if (whoFor == 'ThreeColumn') {
        var left = document.getElementById('left');
        var middle = document.getElementById('middle');
        var right = document.getElementById('mainRight');
        
        var lh = left.offsetHeight;
        var mh = middle.offsetHeight;
        var rh = right.offsetHeight;
        
        if ((lh > mh) && (lh > rh)) {
            middle.style.height = lh + 'px';
            right.style.height = lh + 'px';
        } else if ((mh > lh) && (mh > rh)) {
            left.style.height = mh + 'px';
            right.style.height = mh + 'px';
        } else {
            left.style.height = rh + 'px';
            middle.style.height = rh + 'px';
        }
    } else if (whoFor == 'TwoColumn') {
        var left = document.getElementById('mainLeft');
        var right = document.getElementById('mainRight');

        var lh = left.offsetHeight;
        var rh = right.offsetHeight;
        
        if (lh > rh) {
            right.style.height = lh + 'px';
        } else {
            left.style.height = rh + 'px';
        }
    }
}

function subscribeMag()
{
    $("#cds_name").val($("#subFirstName").val() + " " + $("#subLastName").val())
    $("#cds_page_id").val(32693);
//    document.getElementById("subscribeForm").action = "https://admin.buysub.com/servlet/PrePopGateway";
//    if(document.getElementById("regsub").checked)
//    {
//        document.getElementById("subscribeForm").action = "https://admin.buysub.com/servlet/PrePopGateway";
//    }
//    if(document.getElementById("giftsub").checked)
//    {
//        document.getElementById("subscribeForm").action = " https://admin.buysub.com/servlet/GiftsPrePopGateway";
//    }
    //document.getElementById("subscribeForm").submit();
}

function setHome(node) {
	if($.browser.msie) {
		node.style.behavior='url(#default#homepage)';node.setHomePage('http://www.theroot.com');
		return false;
	}
	return true;
}


//Ajax Ad Calls
function quickReadAdCall() {
	window.setTimeout(quickReadDelayedAdCall, 250);
}

function quickReadDelayedAdCall() {
	if (document.getElementById('wpni_adi_234x60AJAXquickread')) {
		placeAd2('homepage','234x60AJAXquickread',false,''); // module/quickread
	}
}

function quickGuideAdCall() {
	window.setTimeout(quickGuideDelayeAddCall, 250);
}

function quickGuideDelayeAddCall() {
	if (document.getElementById('wpni_adi_88x31AJAXquickguide')) {
		placeAd2('homepage','88x31AJAXquickguide',false,''); // module/quickguide
	}
}

function travelingHomepageAdTarget() {
	return '<div id="slug_88x31AJAXthp" class="sponsoredAd" style="float:right;margin:0 13px 0 0;width:160px;"><div id="wpni_adi_88x31AJAXthp" class="sponsorship" style="float:right;"><xsl:text> </xsl:text></div><div class="sponsoredAdText" style="color:#868685;font-size:0.8em;margin:5px 2px 0 0;">Sponsored by</div></div>';
}

function travelingHomepageAdCall()
{
	window.setTimeout(travelingHomepageDelayedAdCall, 250);
}

function travelingHomepageDelayedAdCall()
{
	if (document.getElementById('wpni_adi_88x31AJAXthp'))
	{
		placeAd2('module/travelinghomepage','88x31AJAXthp',false,'');
	}
}

function peekAdCall(id)
{
	if (typeof(placeAd2) == 'function')
	{
		var peekPageId = '';
		var peekCommercialNode = '';
		var idParts = id.split(':');
		if (idParts.length > 1) 
		{
			peekPageId = idParts[1];
		}
		if (idParts.length > 2) 
		{
			peekCommercialNode = idParts[2];
		}
		if (peekCommercialNode == '' && typeof(commercialNode) != 'undefined')
		{
			peekCommercialNode = commercialNode;
		}
		placeAd2(peekCommercialNode,'88x31AJAX'+peekPageId,false,'');
	}
	
	return false;
}

function hasFlash(divid)
{
	var version = deconcept.SWFObjectUtil.getPlayerVersion();
    if (document.getElementById && (version['major'] == 0)) {
      document.getElementById(divid).innerHTML = "<a href='http://www.adobe.com/go/getflashplayer'><img src='http://bc.newsweek.com/media/nw_flash_install_ccol.jpg' border=0></a>";
    } else if (document.getElementById && (version['major'] > 0 && version['major'] < 8)) {
      document.getElementById(divid).innerHTML = "<a href='http://www.adobe.com/go/getflashplayer'><img src='http://bc.newsweek.com/media/nw_flash_upgrade_ccol.jpg' border=0></a>";
    } else {
		return true;
    }
    return false;
}

function gotoMaintenance() {
    if (!document.getElementById('footerSearchForm')) {
        setTimeout('gotoMaintenance()',500);
    }
    if (Paths.Current.SearchHost.indexOf("Mappingtool") > -1) {
        $("#headerSearchForm").attr("action", window.location.protocol + "//" + window.location.host + "/maintenance.html");
        $("#footerSearchForm").attr("action", window.location.protocol + "//" + window.location.host + "/maintenance.html");
    } else {
        $("#headerSearchForm").attr("action", Paths.Current.SearchHost + "maintenance.html");
        $("#footerSearchForm").attr("action", Paths.Current.SearchHost + "maintenance.html");
    }  
}

//gotoMaintenance();