﻿//----------------------------------------------------------
//--- Copyright 2010 Edulearn, All Rights Reserved.
//----------------------------------------------------------
//--- Edulearn, Inc. Site Wide Script File

// When page has loaded.
$(document).ready(onPageLoad);

// Handle page load event.
function onPageLoad()
{
	// Format sale price if necessary.
	formatPrice();
	// Initialize prettyPhoto.
	$("a[rel^='prettyPhoto']").prettyPhoto();
    //saveReferrer();
	// TODO: If Flash is not installed.
	// Ref: http://code.google.com/p/swfobject/wiki/api
	if (!swfobject.hasFlashPlayerVersion("9.0.0"))
	{
		// Do nothing...
	}
	// If Flash is installed.
	else
	{
		/* TEST CODE BLOCK:
		// Returns a JavaScript object
		var playerVersion = swfobject.getFlashPlayerVersion();
		// Access the major, minor and release version numbers via their respective properties
		var majorVersion = playerVersion.major;
		var minorVersion = playerVersion.minor;
		var releaseVersion = playerVersion.release;
		alert(majorVersion + '.' + minorVersion + '.' + releaseVersion);*/
		
		/*// Initialize browser detection object.
		BrowserDetect.init();		
        // If using an unsupported browser (IE 8 or previous).
        if (BrowserDetect.browser == "Explorer" && BrowserDetect.version <= 8)
        {
			// Do nothing...
		}
		// If using a supported browser.
		else
		{
			// Hide banner image.
			document.getElementById('bannerImg').style.display = 'none';		
			// Build Flash banner object.
			var fn = function()
			{
				var att = { data:"http://www.edulearn.com/resources/flash/edubanner_learn_new_skills.swf", width:"363", height:"58" };
				var par = { flashvars:"name=value", bgcolor: "#006699" };
				var id = "bannerFl";
				var flObject = swfobject.createSWF(att, par, id);
			};
			// Initialize flash when DOM has loaded.
			swfobject.addDomLoadEvent(fn);
			// Make flash container visible.
			document.getElementById('bannerFl').style.visibility = 'visible';
		}*/
	}
}

function saveReferrer()
{
    var referrer = document.referrer;
    var thisHost = self.location.hostname;
    if (thisHost.indexOf(".") < thisHost.lastIndexOf("."))
    {
        var hostOffset = thisHost.indexOf(".") + 1;
        thisHost = thisHost.substr(hostOffset);
    }
    if (document.referrer && document.referrer != "")
    {
        // If just landed\referred from external site.
        if (document.referrer.indexOf(thisHost) == -1 &&
            document.cookie.indexOf("referrer=") == -1)
        {
            // Save referrer info for this user session only.
            var name = "referrer";
            var value = escape(referrer);
            document.cookie = name + "=" + value + "; path=/; domain=" + thisHost;
        }
    }
}

function getReferrer()
{
    var cname = "referrer";
    if (document.cookie.length > 0)
    {
        start = document.cookie.indexOf(cname + "=");
        if (start != -1)
        {
            start = start + cname.length + 1;
            end = document.cookie.indexOf(";", start);
            if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(start, end));
        }
    }
    return "";
}

function formatPrice()
{
	// If salePrice field exists on page.
	if ($('#salePrice').length > 0)
	{
		// Get ref to salePrice field.
		var price = $('#salePrice');
		// Get value.
		var priceText = price.html();
		// If multi-purchase discount has been applied.
		if (priceText.indexOf(' ') > 0)
		{
			// Remove multi-purchase values from salePrice field.
			price.html(priceText.substring(0, priceText.indexOf(" ", 0)));
		}
	}
	// If salePriceRight field exists on page.
	if ($('#salePriceRight').length > 0)
	{
		// Get ref to salePriceRight field.
		var price = $('#salePriceRight');
		// Get value.
		var priceText = price.html();
		// If multi-purchase discount has been applied.
		if (priceText.indexOf(' ') > 0)
		{
			// Remove multi-purchase values from salePrice field.
			price.html(priceText.substring(0, priceText.indexOf(" ", 0)));
		}
	}
}

function PreviousPage(returnURL)
{
	
    var prevURL = document.referrer;
    var thisHost = self.location.hostname;
    if (thisHost.indexOf(".") < thisHost.lastIndexOf("."))
    {
        var hostOffset = thisHost.indexOf(".") + 1;
        thisHost = thisHost.substr(hostOffset);
    }
    /*if (document.referrer && document.referrer != "")
    {
        // if (prevURL.substr(0, 23) == siteURL) // If referred within site.
        if (document.referrer.indexOf(prevURL) > -1)
        {
            // Send to previous page.
            history.go(-1);
        }
        else // If referred from external site.
        {
            // Send to supplied return url.
            window.location = "http://" + thisHost + returnURL;
        }
    }
    else // If page is accessed directly.
    {
        // Send to supplied return url.
        window.location = "http://" + thisHost + returnURL;
    }*/
	
	// Only send to return URL.
	if (returnURL && returnURL != "")
	{
		window.location = returnURL;
	}
	else { window.location = "http://http://www.edulearn.com/training/index.html"; }
}

function MM_openBrWindow(url, name, features)
{
	// If a course outline is being opened.
	if (name == "outline")
	{
	    // Outline Params.
		features = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=620,height=580";
	}
	// If an IT demo video is being opened.
	else if (name == "itVideo")
	{
	    // IT Demo Video Params.
		features = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=550,height=455";
	}
	// If an ITC demo video is being opened.
	else if (name == "itcVideo")
	{
	    // ITC Demo Video Params.
		features = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=800,height=636";
	}
	// If a small sample clip is being opened.
	else if (name == "smallVideo")
	{
	    // Small Sample Clip Params.
		features = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=660,height=520";
	}
	// If a medium sample clip is being opened.
	else if (name == "mediumVideo")
	{
	    // Medium Sample Clip Params.
		features = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=822,height=640";
	}
	// If a large sample video is being opened.
	else if (name == "largeVideo")
	{
	    // Large Video Clip Params.
		features = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=1044,height=729";
	}
	// If a detail popup is being opened.
	else if (name == "detail")
	{
	    // Detail Params.
		features = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=580,height=530";
	}
	// If a form is being opened.
	else if (name == "form")
	{	
		// If contact us form is being opened.
		if (url.indexOf("contact_us.php") > -1)
		{
			// Contact Form Params.
			features = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=542,height=472";
			// Contact Form URL -- for testing.
			url = "http://www.edulearn.com/forms/contact_us.php";
		}
		// If contact request form is being opened.
		if (url.indexOf("contact_request.php") > -1)
		{
			// Contact Request Form Params.
			features = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=532,height=569";
			// Contact Request Form URL -- for testing.
			url = "http://www.edulearn.com/forms/contact_request.php";
		}
		// If email a friend form is being opened.
		else if (url.indexOf("email_friend.php") > -1)
		{
			// Email a Friend Form Params.
			features = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=532,height=415";
			// Email a Friend Form URL -- for testing.
			url = "http://www.edulearn.com/forms/email_friend.php";
		}
		// If newsletter form is being opened.
		else if (url.indexOf("email_signup.php") > -1)
		{
			// Newsletter Form Params.
			features = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=532,height=400";
		}
		// Append source page as query string to URL.
		url += "?sourcepage=" + window.location;
		// Append referrer as query string to URL.
		var referrer = getReferrer();
		if (referrer != "")
		{
			url += "&referrer=" + referrer;
		}
		// Remove invalid characters.
		url = url.replace("#", "");
	}
    // Open popup window.
    window.open(url, name, features);
}

/* 'BrowserDetect' is used to detect the user's browser. */
// Ref: http://www.quirksmode.org/js/detect.html
// Usage ---> BrowserDetect.init();
var BrowserDetect = {
    init: function()
    {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
        // Custom property for double checking failed versions (single line below [altVersion]).
        this.altVersion = this.searchVersion(navigator.appVersion) || "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function(data)
    {
        for (var i = 0; i < data.length; i++)
        {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString)
            {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function(dataString)
    {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    dataBrowser: [
		{
		    string: navigator.userAgent,
		    subString: "Chrome",
		    identity: "Chrome"
		},
		{ string: navigator.userAgent,
		    subString: "OmniWeb",
		    versionSearch: "OmniWeb/",
		    identity: "OmniWeb"
		},
		{
		    string: navigator.vendor,
		    subString: "Apple",
		    identity: "Safari",
		    versionSearch: "Version"
		},
		{
		    prop: window.opera,
		    identity: "Opera"
		},
		{
		    string: navigator.vendor,
		    subString: "iCab",
		    identity: "iCab"
		},
		{
		    string: navigator.vendor,
		    subString: "KDE",
		    identity: "Konqueror"
		},
		{
		    string: navigator.userAgent,
		    subString: "Firefox",
		    identity: "Firefox"
		},
		{
		    string: navigator.vendor,
		    subString: "Camino",
		    identity: "Camino"
		},
		{		// for newer Netscapes (6+)
		    string: navigator.userAgent,
		    subString: "Netscape",
		    identity: "Netscape"
		},
		{
		    string: navigator.userAgent,
		    subString: "MSIE",
		    identity: "Explorer",
		    versionSearch: "MSIE"
		},
		{
		    string: navigator.userAgent,
		    subString: "Gecko",
		    identity: "Mozilla",
		    versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
		    string: navigator.userAgent,
		    subString: "Mozilla",
		    identity: "Netscape",
		    versionSearch: "Mozilla"
		}
	],
    dataOS: [
		{
		    string: navigator.platform,
		    subString: "Win",
		    identity: "Windows"
		},
		{
		    string: navigator.platform,
		    subString: "Mac",
		    identity: "Mac"
		},
		{
		    string: navigator.userAgent,
		    subString: "iPhone",
		    identity: "iPhone/iPod"
		},
		{
		    string: navigator.platform,
		    subString: "Linux",
		    identity: "Linux"
		}
	]
};
