/*------------------------

  Required Functions
 
------------------------*/

//test vaiables - DELETE
//isLoggedIn = 1;
//end test variables


//needed for the contact links funtion to work properly
function getDisplayFriendID() { return MySpace.ClientContext.DisplayFriendId; }


//required for add this javascript
addthis_offset_top      = 0;
addthis_offset_top_low  = 0;
addthis_offset_left     = 0;


// DOM ready function
Object.extend(Event, {
  _domReady : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;

    if (this._timer)  clearInterval(this._timer);
    
    this._readyCallbacks.each(function(f) { f() });
    this._readyCallbacks = null;
},
  onDOMReady : function(f) {
    if (!this._readyCallbacks) {
      var domReady = this._domReady.bind(this);
      
      if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", domReady, false);
        
        /*@cc_on @*/
        /*@if (@_win32)
            document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
            document.getElementById("__ie_onload").onreadystatechange = function() {
                if (this.readyState == "complete") domReady(); 
            };
        /*@end @*/
        
        if (/WebKit/i.test(navigator.userAgent)) { 
          this._timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState)) domReady(); 
          }, 10);
        }
        
        Event.observe(window, 'load', domReady);
        Event._readyCallbacks =  [];
    }
    Event._readyCallbacks.push(f);
  }
});

/*------------------------

  People Browser Flash
 
------------------------*/

function popUp(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=523,height=485');");
}


//function that is used by the Flash   
function showStory(videoID) {
    Dialog.alert($("pwc_iframe").innerHTML, {className: "alphacube",  width:523, height:485, okLabel: "Close", ok:closeAllModalWindows, id: "d" + index})
    
    
    //dynamically swap out src of iframe
    $("iframeContent").width = 523;
    $("iframeContent").height = 485;
    $("iframeContent").src = "http://stage-creative.myspacecdn.com/Client/UniversityOfPhoenix/iaap/videos/default.htm?v=" + videoID;
}

/*------------------------

  Tabs
 
------------------------*/

function ShowTab(sender, tabContentToShow)
{

    var tabbedDivs = $$('div#module-uoptabs div.tabContent');
    // hide all content
    for (i=0; i<tabbedDivs.length; i++)
    {
        tabbedDivs[i].style.display = 'none';
    }
    
    // set all tabs to unselected
    var selectedTabs = $$('#uoptabs a.selectedTab');
    for (i=0; i<selectedTabs.length; i++)
    {
        selectedTabs[i].className = 'unselectedTab';
    }
    
    // select the tab and show it's content
    $(tabContentToShow).style.display = 'block';    
    sender.className = 'selectedTab';
}

var ran_number=Math.floor(Math.random()*3);


// show this tab when the DOM has loaded
Event.onDOMReady(   function() 
                    { 
                        ShowTab($('tabNav' + ran_number), $('tabContent' + ran_number));  
                    }
                 
                 );



/*------------------------

  PWC Functions
 
------------------------*/
var win = null;
var index = 6000;

function openDialog(id,w,h) {
    
    //initialize pwc
    Dialog.alert($(id).innerHTML, {width:w, height:h, okLabel: "Close", ok:closeAllModalWindows, id: "d" + index})
    index++;
}

function openIframe(id,w,h,urlPath) {
    
    //initialize pwc
    Dialog.alert($(id).innerHTML, {width:w, height:h, okLabel: "Close", ok:closeAllModalWindows, id: "d" + index})
    index++;
    
    //dynamically swap out src of iframe
    $("iframeContent").width = w;
    $("iframeContent").height = h;
    $("iframeContent").src = urlPath;
}

function openIframeLoggedIn(id,w,h,urlPath) {
    
    if (isLoggedIn < 0) {
         window.alert("You Must Be Logged In");
    }  else {        
        //initialize pwc
        Dialog.alert($(id).innerHTML, {width:w, height:h, okLabel: "Close", ok:closeAllModalWindows, id: "d" + index})
        index++;
        
        //dynamically swap out src of iframe
        $("iframeContent").width = w;
        $("iframeContent").height = h;
        $("iframeContent").src = urlPath;
    }
}

function openWallpaper(id,w,h, wallpaperFilename) {
    //initialize pwc
    Dialog.alert($(id).innerHTML, {width:w, height:h, okLabel: "Close", ok:closeAllModalWindows, id: "d" + index})
    index++;
    
    //displays the thumbnail of the wallpaper
     $("wallpaperHolder").innerHTML = '<img src="http://stage-creative.myspacecdn.com/Client/UniversityOfPhoenix/iaap/downloads/wallpapers/' + wallpaperFilename + '_thm.jpg" />';
     
     //insert wallpaper links
     $("wallpaperLinks").innerHTML = '<h3>Download University of Phoenix Wallpaper</h3>' +
        '<a href="http://stage-creative.myspacecdn.com/Client/UniversityOfPhoenix/iaap/downloads/wallpapers/' + wallpaperFilename + '_1024x768.jpg" target="_blank" name="&lid='+ wallpaperFilename +'_1024x768" id="'+ wallpaperFilename +'_1024x768" title="Download the 1024x768 University of Phoenix Wallpaper" target="_blank">1024x768</a> |' +
        '<a href="http://stage-creative.myspacecdn.com/Client/UniversityOfPhoenix/iaap/downloads/wallpapers/' + wallpaperFilename + '_1280x800.jpg" target="_blank" name="&lid='+ wallpaperFilename +'_1280x800" id="'+ wallpaperFilename +'_1280x1024" title="Download the 1280x1024 University of Phoenix Wallpaper" target="_blank">1280x1024</a> |' +
        '<a href="http://stage-creative.myspacecdn.com/Client/UniversityOfPhoenix/iaap/downloads/wallpapers/' + wallpaperFilename + '_1280x1024.jpg" target="_blank" name="&lid='+ wallpaperFilename +'_1280x1024" id="'+ wallpaperFilename +'" title="Download the 1280x1024 University of Phoenix Wallpaper" target="_blank">1280x1024</a> |'  + 
        '<a href="http://stage-creative.myspacecdn.com/Client/UniversityOfPhoenix/iaap/downloads/wallpapers/' + wallpaperFilename + '_1440x900.jpg" target="_blank" name="&lid='+ wallpaperFilename +'_1440x900" id="'+ wallpaperFilename +'_1440x900" title="Download the 1440x900 University of Phoenix Wallpaper" target="_blank">1440x900</a> | ' + 
        '<a href="http://stage-creative.myspacecdn.com/Client/UniversityOfPhoenix/iaap/downloads/wallpapers/' + wallpaperFilename + '_1600x1200.jpg" target="_blank" name="&lid='+ wallpaperFilename +'_1600x1200" id="'+ wallpaperFilename +'_1600x1200" title="Download the 1600x1200 University of Phoenix Wallpaper" target="_blank">1600x1200</a>';
}

function openBadge(id,w,h,badgeFilename) {
    //initialize pwc
    Dialog.alert($(id).innerHTML, {width:w, height:h, okLabel: "Close", ok:closeAllModalWindows, id: "d" + index})
    index++;
    
    //displays the badge image
    $("badgeHolder").innerHTML = '<img src="http://stage-creative.myspacecdn.com/Client/UniversityOfPhoenix/iaap/downloads/badges/' + badgeFilename + '.jpg" />';
    
    //insert embed code for users badge
    $("badgeCode").innerHTML = '<form name="badge"><textarea name="badgeCode" id="badgeCode" cols="40" rows="7" onClick="this.select();"><a href="http://www.myspace.com/224210837" name="&lid=badge'+ badgeFilename +'" id="badge'+ badgeFilename +'" target="_blank"><img border="0" src="http://stage-creative.myspacecdn.com/Client/UniversityOfPhoenix/iaap/downloads/badges/'+ badgeFilename +'.jpg" alt="University of Phoenix - I Am A Phoenix Profile Badge"></a></textarea></form>'
}

function openTheme(id,w,h,themeFilename) {
    //initialize pwc
    Dialog.alert($(id).innerHTML, {width:w, height:h, okLabel: "Close", ok:closeAllModalWindows, id: "d" + index})
    index++;
    
    //displays the badge image
    $("themeHolder").innerHTML = '<img src="http://stage-creative.myspacecdn.com/Client/UniversityOfPhoenix/iaap/images/' + themeFilename + '.jpg" />';
    
    //insert embed code for users badge
    $("themeCode").innerHTML = '<a href="http://stage-creative.myspacecdn.com/Client/UniversityOfPhoenix/iaap/themes/' + themeFilename + '.css" target="_blank" id="viewCSS_' + themeFilename+ '" name="&lid=viewCSS_' + themeFilename+ '">View CSS file</a>';
}



function lastDialog(id) {
    Dialog.confirm($(id).innerHTML, { width:250, okLabel: "Close", ok: closeAllModalWindows})
}

function closeAllModalWindows() {
    Windows.closeAllModalWindows();
    return true;
}


function Enter() {
        closeAllModalWindows();
		document.getElementById('b').style.display = 'block';
		document.getElementById('c').style.display = 'block';
		document.getElementById('header').style.display = 'block';
		document.getElementById('topnav').style.display = 'block';
}

/*------------------------

  Custom Badge JS
 
------------------------*/

window.onload = ConstrainRankingImages;


//THIS FUNCTION MAKES SURE THE DEFAULT PIC THAT IS BEING USED IS THE LARGER VERSION RATHER THAN THE MEDIUM OR SMALL VERSION - TO AVOID PIXELATION

function GetSize(size) 
{
    if (imageUrl == "http://x.myspace.com/images/no_pic.gif")
        return imageUrl="http://creative.myspace.com/CodeLibrary/Global/Samples/CustomIdBadge/images/noPhoto.png";
        
	if (imageUrl.indexOf("_s") > -1)
		return imageUrl.replace(/_s/,"_" + size);
	
	if (imageUrl.indexOf("s_") > -1)
		return imageUrl.replace(/s_"/, size + "_" );

	if (imageUrl.indexOf("_m") > -1)
		return imageUrl.replace(/_m/,"_" + size);
	
	if (imageUrl.indexOf("m_") > -1)
		return imageUrl.replace(/m_/, size + "_" );

	else
		//alert('The file name is missing either s_, _s, m_ , _m');	
		
	return null;
}
	var imageUrl = GetSize('l');
	document.write('<div id="profileImage"><img name="profileImg" src="'+imageUrl+'"></div>');



//IMAGE CONSTRAINT FUNCTION 

var RANKING_MAX_WIDTH = 184; //change this to match your specific movie clip
var RANKING_MAX_HEIGHT = 195; //change this to match the dimensions of your movie clip
var RANKING_CONTAINER_ID = 'profileImage'


function ConstrainImageSize(img, maxHeight, maxWidth)
{
    var changeFactor;
    
    // if both width and height are too big, constrain off the biggest one
    if (img.height > maxHeight && img.width > maxWidth)
    {
        // find what factor to constrain by
        if (img.height > img.width)
            changeFactor = maxHeight / img.height;
        if (img.height <= img.width)
         {
		    //changeFactor = maxWidth / img.width;
			changeFactor = 1;
			return {height:maxHeight, width: maxWidth};
		}
    }
    // if width is too big constrain off it
    else if (img.width > maxWidth)
        changeFactor = maxWidth / img.width;
    // if height is too big constrain off it	
    else if (img.height > maxHeight)
        changeFactor = maxHeight / img.height;
    
    // otherwise everything is fine
    else
        changeFactor = 1;


    // now set the new size is
    return {height:img.height * changeFactor, width: img.width * changeFactor};

}

function ConstrainRankingImages()
{
	var akamaiPath="http://stage-creative.myspacecdn.com/Client/UniversityOfPhoenix/iaap/swf/";
 
    if (isLoggedIn < 0)
    {
        document.getElementById('customBadge').innerHTML = "You must be logged-in to view this content <br /><a href =\"http://www.myspace.com\" name=\"&lid=badgeLogin\">Login</a>";
        return false;
    }
    
	var images = document.getElementById(RANKING_CONTAINER_ID).getElementsByTagName('img');
    for (i=0;i<images.length;i++)
    {
        var imageDimensions = ConstrainImageSize(images[i], RANKING_MAX_HEIGHT, RANKING_MAX_WIDTH);
        images[i].style.width = imageDimensions.width + 'px';
        images[i].style.height = imageDimensions.height + 'px';
    }
	document.getElementById('customBadge').innerHTML = '<embed src="'+akamaiPath+'badge.swf?image='+imageUrl+'&imageWidth='+imageDimensions.width+'&imageHeight='+imageDimensions.height+'&maxHeight='+RANKING_MAX_HEIGHT+'&maxWidth='+RANKING_MAX_WIDTH+'&imageWidth='+imageDimensions.width+'&imageHeight='+imageDimensions.height+'&age='+age+'" width="500" height="344" wmode="transparent" allowScriptAccess="always" />';

}

