//console.log(); 
$(document).ready(function() {

   
   
   /*******
   **
   ** Login Code
   **
   *******/
   
   if ( $("div.ntb_logindiv").length > 0 ) {
   		$("div.ntb_logindiv").html('<div class="login_div" style="display: none;"><form class="form-login" name="login" method="post" action="">\r\n<span class="usernametext">\r\n<label for="username">Username </label> <input size="18" alt="username" class="inputbox" name="username" type="text">\r\n</span>\r\n<span class="passwordtext">\r\n<label for="password">Password </label>\r\n <input alt="password" size="18" class="inputbox" name="password" type="password"></span><input alt="Remember Me" value="yes" class="inputbox" name="remember" type="checkbox">\r\n<span class="modlgn_remember_text"><label for="remember">Remember Me</label></span>\r\n		<input value="Login" name="Submit" class="login_submit" type="submit">\r\n		<input value="Cancel" name="Submit" class="cancel_submit" type="submit">	\r\n	<a href="/component/comprofiler/lostpassword">Forgot Login?</a></form>\r\n </div><div class="login_right"><a href="#" class="login_link">Login</a> | <a href="/component/comprofiler/registers" mce_href="/component/comprofiler/registers">Register</a></div>');
   		
   
   $.post("/",{option:'com_ntblogin',format:'raw', view:'login_check'},
   		function(data){
		if(data.id != 0){
			$(".moduletable_login").hide();
			$(".moduletable_login").before('<div class="moduletable_login2" align="right"><form class="logout_form" action="" method="post"><input type="submit" value="Logout" class="logout_submit" name="Submit"/></form><div class="logout_right">Welcome, '+data.username+'  | <a href="/component/comprofiler/userdetails">Edit Account</a></div></div>');											
			
			if ( $(".ntb_loginbox_on").length > 0 ) {
				$(".ntb_loginbox_on").show("slow");
			}
			$(".logout_form").submit(function(){

  							rebindLogout();
							return false;
  		});
		}else{
		$(".login_right").show("slow");
		if ( $(".ntb_loginbox_off").length > 0 ) {
				$(".ntb_loginbox_off").show("slow");
			}
		}}, 'json');
		
	}	
		
  $(".login_link").click(function(){
  	$(".error").hide();
  	$(".login_div").show("slow");
  	$(".login_link").hide();
  	return false;

  		});
   $(".cancel_submit").click(function(){
   
  	$(".login_div").hide("slow");
  	$(".login_link").show();
  	$(".login_div").removeAttr("style");
  	return false;

  		});

   $(".login_submit").click(function(){
   

	$(".error").hide();
		var hasError = false;


		var usernameField = $(this).siblings(".usernametext").children("input").val();
		//var usernameField = $(".form-login input:eq(0)").val();
		
		if(usernameField == '') {
		$(".moduletable_login .login_div").css("left","30px");
			if ( $(".error").length > 0 ) {
				$(".error").replaceWith('<span class="error">Invalid Login</span>');
			}else {
				$(".usernametext").before('<span class="error">Invalid Login</span>');
			}
			hasError = true;
		}
		
		var passwordField = $(this).siblings(".passwordtext").children("input").val();
		// var passwordField = $(".form-login input:eq(1)").val();
		if(passwordField == '') {
			$(".moduletable_login .login_div").css("left","30px");

			if ( $(".error").length > 0 ) {
				$(".error").replaceWith('<span class="error">Invalid Login</span>');
			}else {
				$(".usernametext").before('<span class="error">Invalid Login</span>');
			}			hasError = true;
		}
		
		//var rememberField = $(".form-login input:eq(2)").val();
		var rememberField = $(this).siblings(".remember").children("input").val();
		
		if(hasError == false) {
			$(".login_div").hide("slow");
			if ( $(".temp").length > 0 ) {
				$(".temp").replaceWith('<div class="temp"><img src="/images/loadingAnimation.gif" alt="Loading" id="loading" /></div>');
			}else {
				$(".login_div").before('<div class="temp"><img src="/images/loadingAnimation.gif" alt="Loading" id="loading" /></div>');
			}
			$(".temp").show("slow");
			$.post("/",
   				{option:'com_ntblogin',format:'raw', view:'login', remeber: rememberField, username: usernameField, password: passwordField},
   					function(data){	
   						if(data.success == '1'){
   							$(".moduletable_login").hide();
							if ( $("div.moduletable_login2").length > 0 ) {
								$(".moduletable_login2").replaceWith('<div class="moduletable_login2" align="right"><form class="logout_form" action="" method="post"><input type="submit" value="Logout" class="logout_submit" name="Submit"/></form><div class="logout_right">Welcome, '+data.username+'   | <a href="/component/comprofiler/userdetails">Edit Account</a></div></div>');
								
							}else {
								$(".moduletable_login").before('<div class="moduletable_login2" align="right"><form class="logout_form" action="" method="post"><input type="submit" value="Logout" class="logout_submit" name="Submit"/></form><div class="logout_right">Welcome, '+data.username+'   | <a href="/component/comprofiler/userdetails">Edit Account</a></div></div>');		
								}
							if ( $(".ntb_loginbox_on").length > 0 ) {
									$(".ntb_loginbox_off").hide();
									$(".ntb_loginbox_on").show("slow");
								}
							$(".logout_form").submit(function(){
   								rebindLogout();
								return false;
  							});
  						}else{
  							$(".moduletable_login2").hide();
							$(".temp").hide("slow");
							$(".moduletable_login  .login_div").css("left","30px");
							$(".login_div").show("slow");
  							$(".login_link").hide();
							$(".usernametext").before('<span class="error">Invalid Login</span>');
  						}
  							
  		}, 'json');
		}
		
		return false;
   	});


	function rebindLogout(){
   		$.post("/",{option:'com_ntblogin',format:'raw', view:'logout'},
   			function(data){
   				if(location.pathname == '/component/comprofiler/userdetails'){
   					window.location = '/'
   					return false;
   				}
				$(".moduletable_login2").hide();
				$(".temp").hide();
				$(".login_link").show();
				$(".moduletable_login").show();
				$(".login_right").show("slow");
				if ( $(".ntb_loginbox_on").length > 0 ) {
									$(".ntb_loginbox_on").hide();
									$(".ntb_loginbox_off").show("slow");
								}
				return false;

				}, 'json');
  	};


/*****************
*******  
******* Adslide code.
******* This a bad way of doing this but there is no choice.
******* Basically in order to the the cick event to turn the adbot back on. I need to check every secod to see if the div containing highslide-image exist. once it exist i rebind the div to show the adbot again
*******
*******/
var int_ID = null;
$(".highslide").click(function(){
     $(".adbot").hide();
      $(".adbot").before("<div class='blank_ad'> advertisement:<br/></div>");

     int_ID = setInterval(function(){
       if(!!$('.highslide-image').length) {
         $(".highslide-image").click(function(){
            $('.blank_ad').remove(); 
            $('.adbot').show(); 
         });
          $(".close").click(function(){
            $('.blank_ad').remove(); 
            $('.adbot').show(); 
         });
         clearInterval(int_ID);
       }
     }, 1000);
});



//Remove all inline stlying on the blogs. 

  $(".entry-body p").removeAttr("style");
$('#left_container > .menu li:first').addClass('firstChild');



//alert($(document).height());
var newWidth = $(window).width() - 340;
var newheight = $(window).height() - 144;
	if (jQuery.browser.msie ) {
		var newWidth = $(window).width() - 360;
		var newheight = $(window).height() - 144;
}
if ($(document).width() <= '1024'){
	var newWidth = $(window).width();
	$("#zmagsSidebar").hide();
		var newheight = $(window).height();

}

if (newheight >= newWidth){
	newheight = newWidth;
}
	$("#myViewerContent").width(newWidth);
	$("#myViewerContent").height($(window).height());




 $(window).resize(function() {
	 //$("#zmagsSidebar").hide();
		//alert($(document).height());
		var newWidth = $(document).width() - 340;
		var newheight = $(window).height() - 144;
			if (jQuery.browser.msie ) {
				var newWidth = $(window).width() - 360;
				var newheight = $(window).height() - 144;
		}
		if ($(document).width() <= '1024'){
			var newWidth = $(window).width();
			$("#zmagsSidebar").hide();
				var newheight = $(window).height();
		
		}else{
			$("#zmagsSidebar").show();
		}
		
		if (newheight >= newWidth){
			newheight = newWidth;
		}
			$("#myViewerContent").width(newWidth);
			$("#myViewerContent").height($(window).height());


	
	});



});





