

$(document).ready(function() {

    // this initialises the demo scollpanes on the page.
    if ($('#pane1').length > 0) {
       // $('#pane1').jScrollPane({ scrollbarWidth: 15, scrollbarMargin: 0 });
    };
    $('ul.sf-menu').superfish();

    // Tabs
    if ($('#tabs').length > 0) {
        $('#tabs').tabs();
    };

    $(".highlight").animate({ top: "0px" }, 500);
    $(".log").animate({ top: "0px" }, 500);
    $(".highlights").animate({ left: "8px" }, 500);
    $(".scenes").animate({ left: "2px" }, 500);
    $(".sideLeft").animate({ left: "0px" }, 500);
    $(".sideRight").animate({ left: "155px" }, 500);
    $(".bigSection").animate({ top: "4px" }, 500);

});

function myFocus(element) {
    if (element.value == element.defaultValue) {
        element.value = '';
    };
}
function myBlur(element) {
    if (element.value == '') {
        element.value = element.defaultValue;
    };
}


function UpdateFlashUser(userLogin) {

    //Actualizar o SWF
    var myFlashObject = document.getElementById('uailde');
    //alert('obj: ' + myFlashObject);
    //alert('iobj: ' + myFlashObject.UpdateUser);
    //alert('<%=UserNameGuid %>');
    //alert('userLogin:' + userLogin);

    if (myFlashObject != null) {
        myFlashObject.UpdateUser(userLogin);

        //actualizar o HEADER HTML
        //alert('actualizar header');
        //alert(window.location.toString());
        $("#MasterHeader").load(window.location.toString() + " #MasterHeaderContent"); //, null, "#MasterHeaderContent");
    }

    return false;
}

function OpenLogin(redirect, returnUrl) {
    //alert('abrir página LOGIN');

    $(function() {
        //alert('agora vai abrir');
        tb_show("", "LoginFlash.aspx?height=200&width=1800&modal=true&redirect=" + redirect + "&ReturnUrl=" + returnUrl, "");
    });

    //alert('fim');
    return false;
}


function Login(btn) {

 
    var qs = getQueryString();

    var username = $("#LoginPage1_TbLoginUserName").val(),
    password = $("#LoginPage1_TbLoginPassword").val(),
    remember = $("#LoginPage1_CbLoginRemember:checked").length > 0,
    returnUrl = qs.ReturnUrl;

    $.ajax({
            type: "POST",
            url: "/WS/UaildeUserWS.asmx/UserLogin",
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{ username : '"+username+"', password: '"+password+"', remember:'"+remember+"' }",
            dataType: "json",
            success: function(data, status){
                closeLogin(data);
                if(qs.ReturnUrl != null){
                    window.location = returnUrl;
                }else{
                    window.location = "/default.aspx";
                }
            },
            error: function(xmlRequest){
                $(".userLoginMsg").html("Ocorreu um erro no teu login.");
            }
        });
}

function getQueryString(){
    var urlParams = {};
    (function () {
        var e,
            a = /\+/g,  // Regex for replacing addition symbol with a space
            r = /([^&=]+)=?([^&]*)/g,
            d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
            q = window.location.search.substring(1);

        while (e = r.exec(q))
           urlParams[d(e[1])] = d(e[2]);
    })();
    return urlParams;
}


function closeLogin(userLogin) {
    UpdateFlashUser(userLogin);
    
    setTimeout(function() {
        tb_remove();
    }, 1000);
}

function UpdatePoints(points) {
    //header
    $(".userPoints").text(points);

    //home page
    $(".uaildePointsLogin").text(points);

    //carteira
    $(".uaildeTotalNumber").text(points);
}

function ClearMsg(divName) {
    if ($(divName).length > 0) {
        $(divName).delay(2000).fadeOut("slow");
    };
}


// Delay Plugin for jQuery
// - http://james.padolsey.com/javascript/jquery-delay-plugin/

$.fn.delay = function(time, callback) {
    // Empty function:
    jQuery.fx.step.delay = function() { };
    // Return meaningless animation, (will be added to queue)
    return this.animate({ delay: 1 }, time, callback);
}


var _search = document.location.search.substr(1, document.location.search.length);
var arr = _search.split ("&");
var finalQuery = new Array ();
var temp;

for(var i = 0; i < arr.length; i++)
{
	temp = arr[i].split ("=");
	finalQuery.push({chave:temp[0], valor:temp[1]});
}

function getValueByKey (key_in)
{
	for(var i = 0; i < finalQuery.length; i++)
	{
		if(finalQuery[i].chave == key_in)
		{
			return finalQuery[i].valor;
		}
	}
}

