//var disqus_developer = 1;

function initPage()
{
	var navRoot = document.getElementById("nav");
	var lis = navRoot.getElementsByTagName("li");
	for (var i=0; i<lis.length; i++)
	{
		lis[i].onmouseover = function()
		{
			this.className += " hover";
                }
		lis[i].onmouseout = function()
		{
			this.className = this.className.replace(new RegExp("hover"),"");
		}
	}
}

if (window.attachEvent && !window.opera)
	attachEvent("onload", initPage);
	
// on document ready do the following:
$j(document).ready(function(){
    InitEvents();
});

function InitEvents()
{
    // add smart text box functionality
    $j('.smart-txtbox').focus(function(){
        if($j(this).val() == $j(this).attr('defaultValue'))
        {
          $j(this).val('');
        }
    }); // search-term focus

    $j('.smart-txtbox').blur(function(){
        if($j(this).val() == '')
        {
          $j(this).val($j(this).attr('defaultValue'));
        } 
    }); // search-term blur
    
     // tool tips
    // $j('div.galleries-box-content > ul > li > a').tipsy({gravity: 's'});  // $j.fn.tipsy.autoNS

    // update signed in status
    RefreshLogin();

    // make forms with class submit on enter
    var $btn = $j('.form_submit'); 
    var $form = $btn.parents('.form'); 

    $form.keypress(function(e) 
    { 
        if (e.which == 13 && e.target.type != 'textarea') 
        { 
            var $tmpBtn = $j(this).find('.form_submit');
            if ($tmpBtn[0].type == 'button') 
                $tmpBtn[0].click(); 
            else 
                eval($tmpBtn[0].href); 
            return false; 
        } 
    });
    
    $j("#search-term").keypress(function (e) {
		if ( $j('#search-term').val() !='' && ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13))) {
			$j('#submit_search_gsa').click();
			return false;
		} else {
			return true;
		}
    });
    
    // remove the advertisement legends if no ad present
    $j("div[class^=oas-ad]").each(function(){
        if ($j(this).html().indexOf('empty.gif') > -1)
            $j(this).hide();
    });
    
    // attach login event to login href
    // attachLoginEvent();
    
    dynamic_logout();
}

function attachLoginEvent()
{
    $j('.lnk-profile-go').click(function () {
        if (!IsUserFullyRegistered())
        {    
            $j('.lnk-profile-pop').colorbox({width:"80%", height:"80%", iframe:true, open:true});
            return false;
        }
    });
}

function close_colorbox()
{
    // closes the login overlay
     jQuery(function($) { 
        $.fn.colorbox.close();
     })
}

function dynamic_logout()
{
    
    jQuery(function($) { 
        $('.log-out').click(function () {
            
            if (IsUserSignedIn())
            {    
                
                 // Put an animated GIF image insight of content
                $("#AuthInfo").empty().html('<img src="/images/ajax-loader.gif" />');

                // make logout call
                var nt=String(Math.random()).substr(2,10);
                try {
                    $.ajax({
                      type: "GET",
                      url: "/logout.aspx?nt="+nt
                    });
                 } catch(err){}
                 
                 // refresh login legend
                 setTimeout(function() { 
                     $("#AuthInfo").empty().html('&nbsp;');
                     // redirect to home page if on profile page
                     if (location.href.toLowerCase().indexOf("profile.aspx")>=0)
                        location.href = "/";
                     else
window.location.href=window.location.href; // RefreshLogin();
                 }, 1000);
            }

            return false;
        });
    });
    
}

function RefreshLogin()
{
    jQuery(function($) { 
        // check if the user is already signed in
        if ($("#AuthInfo").html() != "&nbsp;" && $("#AuthInfo").html().indexOf('Login') == -1)
            return;
            
        // user not signed in, let's refresh their status
        var nt=String(Math.random()).substr(2,10);
        var status = "";
        try {
        status = $.ajax({
          url: "/controls/membership/callback/login_handler.ashx?nt="+nt,
          async: false
         }).responseText;
        } catch(err){}
        
        $("#AuthInfo").html(status);
        
    }); // safe jQuery
    
    dynamic_logout();
}

function IsUserSignedIn()
{
     var bolSignedIn = false;
    //jQuery(function($) {
        var nt=String(Math.random()).substr(2,10);
        var member_status;
        try {
        member_status = $j.ajax({
          url: "/controls/membership/callback/login_handler.ashx?status=1&nt="+nt,
          async: false
         }).responseText;

        } catch(err){member_status = "0";}
        
        bolSignedIn = (parseInt(member_status) == 1);
     //}); // safe jQuery
    return bolSignedIn;
}

function IsUserFullyRegistrered()
{
    var bolSignedIn = false;
    // jQuery(function($) {
            var nt=String(Math.random()).substr(2,10);
            var member_status;
            try {
            member_status = $j.ajax({
              url: "/controls/membership/callback/login_handler.ashx?status=full-reg&nt="+nt,
              async: false
             }).responseText;

            } catch(err){member_status = "0";}
    //  }); // safe jQuery
    bolSignedIn = (parseInt(member_status) == 1);
    return bolSignedIn;
}
function IsUserFullyRegistered()
{
    var bolSignedIn = false;
    // jQuery(function($) {
			var m = $j('input.site_member').val();
            var nt=String(Math.random()).substr(2,10);
            var member_status;
            try {
            member_status = $j.ajax({
              url: '/controls/membership/callback/login_handler.ashx?status=full-reg&m='+m+'&nt='+nt,
              async: false
             }).responseText;

            } catch(err){member_status = "0";}
    //  }); // safe jQuery
    bolSignedIn = (parseInt(member_status) == 1);
    return bolSignedIn;
}

 $j(function() {          
          $j(".content-type-img img").lazyload({
             placeholder : "/images/grey.gif",
             effect      : "fadeIn"
          });
      });
