// JavaScript Document

window.setCookie = window.setCookie || function (Name,Value,Expires)
{
    var pvalue   = escape(Value) ;
    var pExpires = Expires ;

    var pPath    = ';Path=/' ;

    // Get subdomain if more than one "." in the host name
    var pDomain  = document.URL;
    pDomain = pDomain.substring( pDomain.indexOf( '//' ) + 2 );
    idx = pDomain.indexOf( ':' );
    if ( idx >= 0 )
        pDomain = pDomain.substring( 0, idx );
    idx = pDomain.indexOf( '/' );
    if ( idx >= 0 )
        pDomain = pDomain.substring( 0, idx );
    idx = pDomain.indexOf( '.' );
    if ( idx != pDomain.lastIndexOf( '.' ) )
        pDomain = ';domain=' + pDomain.substring( idx );
    else
        pDomain = '';

    if ( pExpires == '' ) { pExpires = 'Fri, 31 Dec 2099 23:59:59 GMT;'; }
    document.cookie = (Name + '=' + pvalue + ';expires=' + pExpires + pPath + pDomain);
};

window.getCookie = window.getCookie || function(Name)
{
    var aCookie = document.cookie.split("; ");

    for ( var i=0 ; i < aCookie.length ; i++ )
    {
        var aCval = aCookie[i].split("=");
        if ( ( Name == aCval[0]) && ( aCval.length > 1 ) ) return unescape( aCval[1] ) ;
    }

    return ' ' ;
}

try {
	var currentPage = document.body.id
	var docRef = document.referrer;
	var loc = String(window.location);
	// this means the user is coming straight to pro web site or coming to pro site though pictage.com
	// if the user is coming through pictage.com then we have to show pictage.com top menu
	// else we show pro site top menu
	var cameFromNewSite = loc.indexOf('pictageNewPhotographerSite') != -1;
	var hasNewSiteCookie = getCookie('pictageNewPhotographerSite') === 'yes';
	var testRedirect = loc.indexOf('pictageTestRedirect') != -1;
	var noRedirect =  loc.indexOf('noredirect') != -1;
	var testHideNav = loc.indexOf('pictageTestHideNav') != -1;
	var begin = docRef.indexOf("http://") + 8;
	var cameFromThisSite = docRef.indexOf("pro.pictage.com") != -1;//loc.indexOf(docRef.substring(begin, docRef.indexOf("/", begin))) != -1;
	
	var redirect = (!cameFromNewSite && hasNewSiteCookie && !cameFromThisSite && !noRedirect) || testRedirect;
	var showNav = !hasNewSiteCookie && !testHideNav &&!cameFromNewSite && window.name != "noshow";
	
	if (redirect) {
		//window.location="http://www.pictage.com/photographer/";
	} 
	
	else if (showNav) {
		if( parent.tophdr == 'undefined' || parent.tophdr == null) {
			// show pro site top menu
			// but do not show this navigation - if the user in under Community tab
			//if(currentPage != 'community') {
				document.write('<!--<div id="header"><h1><a href="../index.html" title="Home">Pictage</a></h1><div id="topnav2"><ul><li id="nav_prohome"><a href="../index.html" id="current" title="Pictage Pro Home"><span>Welcome</span></a></li><li id="nav_products"><a href="../products.shtml" title="Products & services currently offered by Pictage"><span>Products</span></a></li><li id="nav_community"><a href="../community/index.html" title="Pictage community website and forums"><span>Community</span></a></li><li id="nav_support"><a href="../photog_help.htm" title="Pictage support, instructional guides and videos"><span>Support</span></a></li><li id="nav_home"><a href="http://www.pictage.com" id="current" title="Pictage Studio Home"><span>Studio Home</span></a></li></ul><div id="adminlink">New member? <a href="../getting_started.htm" title="Learn how to get started with Pictage!">Get started</a></div></div></div>--><div id="header"><h1><a href="http://www.pictage.com/" title="Pictage Studio Home">Pictage</a></h1><div id="topnav2"><ul><li id="nav_home"><a href="http://www.pictage.com" title="Pictage Studio Home"><span>Studio Home</span></a></li><li id="nav_prohome"><a href="../index.html" title="Products & services currently offered by Pictage"><span>Products & Services</span></a></li><li id="nav_community"><a href="../community/index.html" title="Pictage community website and forums"><span>Community</span></a></li><li id="nav_myfolders"><a href="http://www.pictage.com" title="My Folders - Accessible from Studio Home"><span>My Folders</span></a></li><li id="nav_support"><a href="../photog_help.htm" title="Pictage support, instructional guides and videos"><span>Support</span></a></li></ul><div id="adminlink">New member? <a href="../getting_started.htm" title="Learn how to get started with Pictage!">Get started</a></div></div></div>');
	//		}
		} else {
		
			// show pictage.com top menu
			
			// check the referring URL.
			// if it had studio_info.xml as part of the URL, then the user is coming from Studio Home or My Folders.
			// 	put that url in cookie for future reference.
			// else the user is coming from one of the internal links of pro site.
			//	get the url from cookie and call synPictageTopNav().
				if(docRef.match('get_studio_info.xml') != null) {
			} else {
				synPictageTopNav();
			}
		}
	}
	else {
		window.name="noshow";
		if (!hasNewSiteCookie) {setCookie("pictageNewPhotographerSite", "yes", "");}
		document.write("<br />");
	}
	
	
    } catch(err) {	alert(err); 	}

//
// This function will make sure the top menu is in sync with body id.
//
function synPictageTopNav() {
	var currentPage = document.body.id
	// replace studio_manager with studio_header
    	if(currentPage == null || currentPage == '') {
		parent.tophdr.keepIn('Support');
    	} else {

		if(currentPage == 'products' || currentPage == 'home') {
			parent.tophdr.keepIn('ProductsServices');
		}

		// community
		if(currentPage == 'community') {
			parent.tophdr.keepIn('Community');
		}

		// support
		if(currentPage == 'support') {
			parent.tophdr.keepIn('Support');
		}
	}
	setProSiteCookie(86400);
}

//
// To refresh the menu with the tab selection.
//
function replaceCurrentTabParameter(newNavFrameURL, param) {
	var indexOfCT = newNavFrameURL.indexOf('currentTab=');
	var tempremaindingURL = newNavFrameURL.substr(indexOfCT, newNavFrameURL.length);
	var remaindingURL = tempremaindingURL.substr(tempremaindingURL.indexOf('&'), tempremaindingURL.length);
	var finalURL = newNavFrameURL.substr(0 , indexOfCT);
	finalURL += param;
	finalURL += remaindingURL;
	return finalURL;
}

//
// Utility method to get a parameter value from the url
//
function getURLParam(strParamName, strHref){
	var strReturn = "";
	if ( strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
			if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return unescape(strReturn);
} 


