




function isEmpty(ob){
	  if (typeof ob === 'undefind'){return false;}
          for(var i in ob){if(ob.hasOwnProperty(i)){return false;}}
	  return true;
	}
$(function(){

var user_loged = $.cookie('user_is_loged_in');
var user_email = $.cookie('user_email');
var user_id = $.cookie('user_id');

if (user_loged === 'true'){
//console.log(user_email);
//console.log(user_id);
$('#login_link').css('display','none');
$('#profile_link').css('display','block');
$('#logout_link').css('display','block');
// modfiy the login link to be my profile


$.ajax({
		   type: "POST",
		   url: 'admin/index.php/site/orders/ajax',
		   data: "user="+user_id,
		   success: function(msg){
                          try{
                          ajax_data = eval('('+msg+')');


                          if(!isEmpty(ajax_data)){
                             $('#not_form_text').text(ajax_data.note);
                              if (ajax_data.success === false){
                                                $('#login_error').text('sorry wrong Email OR passowrd');
                                            }else{
                                            $.cookie('user_id',ajax_data.result.user_id);
                                            $.cookie('user_email',ajax_data.result.email);
                                            $.cookie('user_is_loged_in',true);

                                            }

                          //console.log(ajax_data);
                                        } else {


                         // console.log('ajax_data empty');

				 }
                          }
                          catch(e){

            $('#profiledata').html(msg);
            $('a.view_not_link').colorbox({width:"650",height:"400", inline:true, href:"#view_not_data",title:" "});

                    $('a.view_not_link').click(function(evt){

                            var orderid = evt.currentTarget.id.replace('not-','');
                           // console.log(orderid);
                            $('#view_not_data').css('display','block');
                             $.ajax({
                                       type: "POST",
                                       url: 'admin/index.php/site/ajax_get/',
                                       data: "id="+orderid,
                                       success: function(msg){

                                             ajax_data = eval('('+msg+')');
                                             if(!isEmpty(ajax_data)){
                                                 $('#not_form_text').text(ajax_data.note);


                                              console.log(ajax_data);
                                                            } else {
                                                                //loading tinymce
                                              console.log('ajax_data empty');

                                                     }
                                  }
                                     });//end $.ajax
                    });//end of $('a.edit_not_link').click

                          }


	      }
		 });//end $.ajax



}else{
  //console.log(user_loged);




}

$('#login_link').colorbox({width:"650",height:"400", inline:true, href:"#view_login_box",title:" "});

$('#login_button').click(function(evt){

         email= $('#email').val();
	 password= $('#password').val();

       // console.log(orderid);
        $('#view_login_box').css('display','block');
         $.ajax({
		   type: "POST",
		   url: 'admin/index.php/site/login/ajax',
		   data: "email="+email+'&password='+password,
		   success: function(msg){

			 ajax_data = eval('('+msg+')');
			 if(!isEmpty(ajax_data)){
                             $('#not_form_text').text(ajax_data.note);
                              if (ajax_data.success === false){
                                                $('#login_error').text('sorry wrong Email OR passowrd');
                                            }else{
                                            $.cookie('user_id',ajax_data.result.user_id);
                                            $.cookie('user_email',ajax_data.result.email);
                                            $.cookie('user_is_loged_in',true);
                                            $('#view_login_box').fadeOut(500, function(){});
                                            window.location = 'profile.html';
                                            }


                                        } else {




				 }
	      }
		 });//end $.ajax
                 return false;
});//end of $('#login_button').click

$('#logout_link').click(function(evt){

         
       // console.log(orderid);
        $('#view_login_box').css('display','block');


         $.cookie('user_id','');
         $.cookie('user_email','');
         $.cookie('user_is_loged_in','');

         $.ajax({
		   type: "POST",
		   url: 'admin/index.php/site/logout/ajax',
		   data: "",
		   success: function(msg){

                                    
                                     //$.cookie('user_is_loged_in',null);
				 }
	      
		 });//end $.ajax
                 window.location = 'index.html';
                 return false;
});//end of $('#login_button').click


$("#login_form").submit(function(e){
      e.preventDefault();

   $('#login_button').trigger("click");
})


$('#forgetpassword').click(function(){
			 $('#login_wrapper').fadeOut(400, function(){
  					$('#view_forget_box').fadeIn(400);
				  });


 })

 });
