var __uu__loginCallback = null;

function download(sid,path) {
    if( logged ) {
        $.get("/portal/permission?type=csv", function(data) { 
            if( data.indexOf("True") >= 0 ) {
                $("body").append("<iframe width=\"0\" height=\"0\" style=\"border:0;\" src=\"" + path + "\"></iframe>");
                try { var pageTracker = _gat._getTracker("UA-2789836-1"); pageTracker._trackPageview('path'); } catch(e) {} 
            } else { 
                alert( "Your free account has exceeded the allowed number of CSV downloads. You can unlock unlimited CSV downloads by purchasing the Pro version of UUorld."); 
            }});
    } else { 
        showLogin( function() { download( sid, path ); } );
        
    }
}

function appGet(sid,path) {
    if( logged ) {
        try { 
            UUWebViewGetObj.get( path );
            $("#"+sid).find("input").attr("disabled","true");
        } catch(e) {
            alert( e );
        }
    } else {
        showLogin( function() { appGet(sid,path); } );
    }
}

function showLogin( callback ) {
    $("#login").jqmShow(); 
    __uu__loginCallback = callback;
}

function configLogin(h) {
    $("#login_msg").hide();
    $("#login_form").ajaxForm({
        target: "#login_msg",
        success: function( responseText, statusText ) {
            $("#login_msg").show().addClass("message");
            if( responseText.indexOf( "Success" ) != -1 ) {
                $("#login_form").hide();
                logged = true;
                try { 
                    UUWebAuth.checkAuth( logged );
                } catch(e) { }
                re = /uid(\d+)/
                a = responseText.match( re );
                if( a.length > 1 ) { u = a[ 1 ]; }
                if( __uu__loginCallback && u > 0 ) {
                    setTimeout( "$(\"#login\").jqmHide();__uu__loginCallback()", 1500 );
                } else {
                    window.location.reload();
                }
            }
        }
    });
    
    $("#signup_msg").hide();
    $("#signup_form").ajaxForm({ 
        target: "#signup_msg",
        success: function( responseText, statusText ) {
            $("#signup_msg").show().addClass("message");
            if( responseText.indexOf( "Success" ) != -1 ) { 
                $("#signup_form").hide(); 
                logged = true;
                try { 
                    UUWebAuth.checkAuth( logged );
                } catch(e) { }
                re = /uid(\d+)/
                a = responseText.match( re );
                if( a.length > 1 ) { u = a[ 1 ]; }
                if( __uu__loginCallback && u > 0 ) {
                    setTimeout( "$(\"#login\").jqmHide();__uu__loginCallback()", 1500 );
                } else {
                    window.location.reload();
                }
            }
        }
    });
}


// login form
$().ready(function() {
    $("body").append('<div id="login" class="jqmAlert"><div class="jqmAlertWindow"><div class="jqmAlertTitle clearfix"><a href="#" class="jqmClose jqm"><em>Close</em></a></div><div class="jqmAlertContent"><p>Please wait...</p></div></div></div>');
    $("#login").jqm( { 
        trigger: false, 
        modal:true, 
        ajax:'/login?ajax=True', 
        onLoad:configLogin,
        target: 'div.jqmAlertContent'   
      } ).hide(); 
    try {
        $(".study").each(function(i) {
            if( UUWebViewGetObj.checkOwned( $(this).attr("id") ) ) {  
                $(this).find("input").attr("disabled","true"); 
                $(this).find(".uuxdownload").append("&nbsp;&nbsp;&nbsp;&nbsp;<em style='color:#666;'>Already downloaded</em>");
            } 
        });
    } catch(e) { }
});

