﻿// --------- Global Vars -------------------

var appSettings = [];
var intervalCheckForSWFObject;
var viewerId = "viewID"; // just give this a default value
var viewerName = "";
var ownerId = "ownerID"; // just give this a default value
var appInstalled = "0";
var surface = gadgets.views.getCurrentView().getName();

// --------- Get Current User ----------------

function getUserIds() {
    try{
        var os = opensocial.Container.get();
        var dataReqObj = os.newDataRequest();
        var viewerReq = os.newFetchPersonRequest(opensocial.DataRequest.PersonId.VIEWER);
        var ownerReq = os.newFetchPersonRequest(opensocial.DataRequest.PersonId.OWNER);
        dataReqObj.add(viewerReq);
        dataReqObj.add(ownerReq);
        dataReqObj.send(getUserIds_response);
    } catch (e) {
        // something failed so the best thing we can do
        // at this point is at least draw the flash file using default values
        writeFlash();
    }
}

function getUserIds_response(data) {
    try{
        if (data.hadError()) {
            appInstalled == "0"
        }
        else {
            var viewer = data.get(opensocial.DataRequest.PersonId.VIEWER).getData();
            var owner = data.get(opensocial.DataRequest.PersonId.OWNER).getData();
            viewerId = viewer.getId();
			viewerName = viewer.getDisplayName();
            ownerId = owner.getId();
            appInstalled = "1";
        }
        
    }catch(ex){
        //do nothing as we can assume they don't have the application installed.
        // This would be a good place to notify them to install
    }
    writeFlash();
}

// --------- Write the Flash ---------------

function writeFlash() {
    
    var flashvars = {};
    flashvars.network = "myspace";
    flashvars.ownerId = ownerId;
    flashvars.viewerId = viewerId;
    flashvars.appInstalled = appInstalled;
    flashvars.surface = surface;

    var params = {};
    params.wmode = "opaque";
    params.allowFullScreen = "true";
    params.allowScriptAccess = "always";
    params.scale = "showAll";
    params.base = appSettings.swfBaseURL;
    params.wmode = "opaque";

    var attributes = {};
    attributes.id = "flashGadget";

    swfobject.embedSWF(appSettings.swfURL, "gadget", "430", "600", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
    swfobject.createCSS("#flashGadget", "outline:none"); 
}

// -------- Share the application ----------

function shareApplication(recipientID, messageText) {
    var message = opensocial.newMessage("[sender] would like you to install the [app]!.. Install Now");
    opensocial.requestShareApp(recipientID, message);
}

function tellFlashShareApp() {
    document.getElementById("flashGadget").shareThisApplication();
}

//Send an activity to the users activity stream
function postActivity(activityTitle, activityText){
	var recipient = opensocial.DataRequest.PersonId.VIEWER;

	var params = [];
	params[opensocial.Message.Field.TITLE] = activityTitle;
	params[opensocial.Message.Field.TYPE] = opensocial.Message.Type.NOTIFICATION;
	
	var body = viewerName + ' ' + activityText;
	var message = opensocial.newMessage(body, params);

	opensocial.requestSendMessage(recipient, message);
}

// --------- Write the HTML ----------------

function writeHTML() {

    var html = '';
    html += '<div style="width:100%; text-align:center; margin:auto; padding:0; margin:0;">';
    html += '   <div id="contentContainer" style="height:600px; width:600px; display:inline-block;">';
    html += '       <div id="gadgetContainer" style="height:600px; float:left;"><div id="gadget">LOADING...</div></div>';
    html += '       <div id="foxProductContainer" style="float:right; width:160px; ">';
    html += '           <h1 style="font:bold 14px Arial; color:#EE0000">FOX PRODUCTS</h1>';
    html += '           <ul id="foxProducts" style="list-style:none; padding:0px 0px 0px 10px; margin:10px 0x 10px 0px; border-left:solid 2px #ccc; color:#666666;">';
    html += '               <li>';
    html += '                 <a href="http://foxshop.seenon.com/detail.php?p=72377" target="_top">';
    html += '                        <img src="' + appSettings.domain + '/img/fgi_marley_plush.jpg" alt="thumbnail" style="border:none;"/>';
    html += '                    </a>';
    html += '                    <h2 style="font:normal 12px Arial; padding:0px 0px 20px 0px; margin:0;">Limited Edition Marley Plush from "Marley and Me"</h2>';
    html += '               </li>';
    html += '                <li>';
    html += '                    <a href="http://foxshop.seenon.com/detail.php?p=70831" target="_top">';
    html += '                        <img src="' + appSettings.domain + '/img/fgi_homer_slippers.jpg" alt="thumbnail" style="border:none;"/>';
    html += '                    </a>';
    html += '                 <h2 style="font:normal 12px Arial; padding:0px 0px 20px 0px; margin:0;">Homer Slippers</h2>';
    html += '                </li>';
    html += '                <li>';
    html += '                    <a href="http://foxshop.seenon.com/detail.php?p=65263&v=foxshop-24" target="_top">';
    html += '                        <img src="' + appSettings.domain + '/img/fgi_jack_bauer.jpg" alt="thumbnail" style="border:none;"/>';
    html += '                  </a>';
    html += '                    <h2 style="font:normal 12px Arial; padding:0px 0px 20px 0px; margin:0;">I Heart Jack Bauer Women\'s Tee</h2>';
    html += '                </li>';
    html += '           </ul>';
    html += '      </div>';
    html += '      <div style="clear:both;"></div>';
    html += '   </div>';
    html += '   <div id="footerContainer">';
    html += '       <img src="' + appSettings.domain + '/myspace/images/footer_legal.gif" alt="" usemap="#footer_legal" style="border:none;" />';
    html += '   </div>';
    html += '   <map name="footer_legal" id="footer_legal">';
    html += '       <area shape="rect" coords="314, 22, 431, 35" href="http://foxus.rsc03.net/servlet/website/ResponseForm?MQNlE1zwkpOHJRwhspJR" target="_blank" alt="" />';
    html += '       <area shape="rect" coords="730, 4, 815, 14" href="http://foxus.rsc03.net/servlet/website/ResponseForm?MQNlE1zwkpO " target="_blank" alt="" />';
    html += '       <area shape="rect" coords="393, 67, 469, 82" href="http://filmratings.com" target="_blank" title="filmratings.com" alt="filmratings.com" />';
    html += '       <area shape="rect" coords="295, 66, 383, 82" href="http://parentalguide.org" target="_blank" title="parentalguide.org" alt="parentalguide.org" />';
    html += '       <area shape="rect" coords="239, 65, 286, 82" href="http://mpaa.org" target="_blank" title="mpaa.org" alt="mpaa.org" />';
    html += '   </map>';
    //html += unescape("%3Cscript src=\'" + (("https:" == document.location.protocol) ? "https://ssl." : "http://www.") + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E");
    //html +=   unescape("%3Cscript type=\'text/javascript\'%3E var pageTracker = _gat._getTracker(\"UA-3401987-8\"); pageTracker._setDomainName(\".marleysgifts.com\"); pageTracker._trackPageview(); %3C/script%3E");
    html += '</div>';
    

    document.body.innerHTML+=html;

}



function proxyRequest(reqURL, callbackID) {

    var resultHandler = function(data) {
        document.getElementById('flashGadget')[callbackID](data.text);
    }
    
    var req_params = new Object();
    req_params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
    req_params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
    var req = gadgets.io.makeRequest(reqURL, resultHandler, req_params);
}


//getProfile();

// --------- Initialization ----------------

function init(environment) {
    
    // Setup all enviroment dependent variabls
    appSettings.environment = environment;    
    appSettings.domain_LOCAL = "http://local.marleysgifts.com";
    appSettings.domain_STAGE = "http://stage.marleysgifts.com";
    appSettings.domain_PREPROD = "http://preprod.marleysgifts.com";
    appSettings.domain_PROD = "http://www.marleysgifts.com";
    appSettings.domain = appSettings["domain_" + appSettings.environment];
    alert(appSettings.domain);
    appSettings.swfBaseURL = appSettings.domain + "/flash/gadget/";
    appSettings.swfURL = appSettings.swfBaseURL + "fetch.swf";
    
    // Write the html elements
    writeHTML();
    
    // Get the current user id
    getUserIds();
}