function addEvent(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}


addEvent(window, 'load', init, false);

function init() {
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];

        if (theInput.type == 'text' && theInput.className.match(/\bcleardefault\b/)) {
            /* Add event handlers */
            addEvent(theInput, 'focus', clearDefaultText, false);
            addEvent(theInput, 'blur', replaceDefaultText, false);
            /* Save the current value */
            if (theInput.value != '') {
                theInput.defaultText = theInput.value;
            }
        }
    }
}

function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;

    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;

    if (target.value == '' && target.defaultText) {
        target.value = target.defaultText;
    }
}




var conf = 
{
    APIKey: '2_jJ_5y8q2SMBhdz63GM90G2kZp0U2ioTNHLwOC1rE0KNXD_G1l8-5BV8GKkR9j38k'
};

function onLoad()
{
    // get user info
    gigya.services.socialize.getUserInfo(conf,{callback:renderUI});	    

    // register for connect status changes
    gigya.services.socialize.addEventHandlers(conf, { onConnect:renderUI, onDisconnect:renderUI}   ); 


}
addEvent(window, 'load', onLoad, false);

// Create and Publish Users Action
// This method is associated with the "btnPublishAction" click
function showShareUI() {

    // Constructing a UserAction Object
    var act = new gigya.services.socialize.UserAction();

    // Setting the default user message 
    // (will be presented as default text in the edit box on the Share UI)
    act.setUserMessage("I have just created a cool fake magazine cover on CoverDude.com!");

    // Setting the title and description 
    // (will be presented in the preview on the Share UI)
    act.setTitle("HOME movie (English with subtitles)");
    act.setDescription("We are living in exceptional times."
	+ " Scientists tell us that we have 10 years to change the way we live," 
	+ " avert the depletion of natural resources and the catastrophic evolution"
	+ " of the Earth's climate. The stakes are high for us and our children."
	+ " Everyone should take part in the effort, and HOME has been conceived"
	+ " to take a message of mobilization out to every human being.");

// Setting a link back to the publishing source
    act.setLinkBack("http://www.coverdude.com");

    // Adding Action Link
    act.addActionLink("View my magazine", 
	"http://www.youtube.com/watch?v=jqxENMKaeCU&feature=channel_page");

	    // Adding an image (will be presented in the preview on the Share UI)
	var image = {
	    src: 'http://i4.ytimg.com/vi/G8IozVfph7I/default.jpg',
	    href: 'http://www.youtube.com/watch?v=G8IozVfph7I&feature=channel_page',
	    type: 'image'
	}
	act.addMediaItem(image);

	// Parameters for the showShareUI method, including the UserAction object
	var params = 
	{
	    userAction: act,  // The UserAction object enfolding the newsfeed data.			                                  
	    onError: onError,  // onError method will be summoned if an error occurs. 
	    onSendDone: onSendDone // onError method will be summoned after 
				// Gigya finishes the publishing process.
	};

	// Show the "Share" dialog
	gigya.services.socialize.showShareUI(conf, params);

}

// onError event handler
function onError(event) {
    alert('An error has occured' + ': ' + event.status + '; ' + event.statusMessage);
}

// onSendDone event handler. 
// Displays in the status field, the list of providers to which the newsfeed has been 
// successfully published.
function onSendDone(event)
{
    document.getElementById('status').style.color = "green";
    document.getElementById('status').innerHTML = 
	    'The newsfeed has been posted to: ' 
	    + event.providers;
}    