﻿// nw_script_v1.0.js
// Author(s): Nehal Shah, Paul Waldschmidt, Michael Trythall, David Johnson
// Version: 1.0
// Created: 7/5/2007
// Updated: 11/1/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(){
 
    initializeSubscribeForm();
});

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 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.newsweek.com');
		return false;
	}
	return true;
}


//Ajax Ad Calls
function quickReadAdCall()
{
	window.setTimeout(quickReadDelayedAdCall, 250);
}

function quickReadDelayedAdCall()
{
	if (document.getElementById('wpni_adi_468x60AJAXquickread'))
	{
		placeAd2('module/quickread','468x60AJAXquickread',false,'');
	}
}

function quickGuideAdCall()
{
	window.setTimeout(quickGuideDelayeAddCall, 250);
}

function quickGuideDelayeAddCall()
{
	if (document.getElementById('wpni_adi_88x31AJAXquickguide'))
	{
		placeAd2('module/quickguide','88x31AJAXquickguide',false,'');
	}
}

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 multimediaAdCall()
{
	if (document.getElementById('wpni_adi_leaderboardAJAXphoto'))
	{
		placeAd2(commercialNode,'leaderboardAJAXphoto',false,'');
	}
	if (document.getElementById('wpni_adi_articleFlexAJAXphoto'))
	{
		placeAd2(commercialNode,'articleFlexAJAXphoto',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;
}
