/*************************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
*  Copyright 2008 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any.  The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
*
* AdobePatentID="B564"
* AdobePatentID="B565"
*
**************************************************************************/

/**
 * Gets the relative path based on the ice.js includes.
 */
getSiteRoot = function() {
	var searchPath = "includes/ice/ice.js";
    var s = null;
    var scripts = document.getElementsByTagName("script");
    for (var i=0; i< scripts.length; i++) {
        var url = scripts[i].src;
        if (typeof url !== "undefined") {
            if (url.indexOf(searchPath) != -1) {
                s = scripts[i].src;
                break;
            }
        }
    }
	s = s.substring(0, s.length - searchPath.length);

	if (!/http/i.test(s)) {
		var m = s.match(/\.\.\//g);
		var deep = m?m.length:0;
		var loca = window.location;
		var path = loca.pathname.replace(/\/([^\/]*)$/, '/');
		var temp = path.split('/');
		temp.length = temp.length - deep - 1;
		path = temp.join('/') + '/';
		s = loca.protocol + '//'+ loca.hostname + (loca.port?':' + loca.port:'') + path;
	}

	return s;
}

var isNotInIde = parent == window || ( parent != window && !(/ide\.htm/i).test(parent.location.href) );
if (isNotInIde) {
	if (navigator.userAgent.indexOf('WebKit/') != -1) {
		redirectto = encodeURI(window.location+'');
	} else {
		redirectto = window.location.href;
	}
	var siteRoot = getSiteRoot();
	page = redirectto.substring(siteRoot.length);
	redirectto =  siteRoot + 'includes/ice/ide.html#page=' + page.replace(/\?.*$/i, '');
	$isLoggedIn = document.cookie.indexOf('iceLoggedIn=true') >= 0;

	if ($isLoggedIn) {
		//I am logged in, redirect to ide->view mode
		window.location.href = redirectto;
	} else {
		//I am not logged in, user must press CTRL+E to be redirected to ide->login dialog
		function ICE_onkeydown(e) {
			e = e || event;
			if (e.ctrlKey && e.keyCode == 69) {
				//stop CTRL+E and redirect to ide.php
				try{e.cancelBubble = true;}catch(err){}
				try{e.returnValue = false;}catch(err){}
				try{e.stopPropagation();}catch(err){}
				try{e.preventDefault();}catch(err){}
				window.location.href = redirectto;
				return false;
			}
		}
		if (document.addEventListener) {
			document.addEventListener('keydown', ICE_onkeydown, false);
		} else if (document.attachEvent) {
			document.attachEvent('onkeydown', ICE_onkeydown);
		} else {
			document.onkeydown = ICE_onkeydown;
		}
	}
}

writeFader = function () {
	//moved swf creation in prepareIde where it belongs
	//this is here for backward compatibility with pages still containing the inline writeFader JS call 
}
