$(document).ready( function(){
    //$('ul.horisontal li.active').corner('8px');
    
    //$('.btns').corner('5px');
    
    //$('#rup').corner('top 8px');
    //$('#rdown').corner('bottom 8px');
    
    /*
    $("div.body").hover(
		function () {
			$('a',this).css({color:'#999999'});
		}, 
		function () {
			$('a',this).css({color:'white'});
		}
	);
    */
    // Select all
    $("A[href='#select_all']").click( function() {
        $("#" + $(this).attr('rel') + " INPUT[type='checkbox']").attr('checked', true);
        return false;
    });
   
    // Select none
    $("A[href='#select_none']").click( function() {
        $("#" + $(this).attr('rel') + " INPUT[type='checkbox']").attr('checked', false);
        return false;
    });
   
    // Invert selection
    $("A[href='#invert_selection']").click( function() {
        $("#" + $(this).attr('rel') + " INPUT[type='checkbox']").each( function() {
            $(this).attr('checked', !$(this).attr('checked'));
        });
        return false;
    }); 
    
    
    
    $('input[type=text]').focus(function(){ 
        if($(this).val() == $(this).attr('placeholder'))
        {
          $(this).val('');
        }
      });
      
      $('input[type=text]').blur(function(){
        if($(this).val() == '')
        {
          $(this).val($(this).attr('placeholder'));
        } 
      });

      $(function(){
    	    $('form#quick_login input').keydown(function(e){
    	        if (e.keyCode == 13) {
    	            $(this).parents('form').submit();
    	            return false;
    	        }
    	    });
    	});

      
      $('#passrecover').submit(function() { 
    	  $('#passrecover').ajaxSubmit({
		        target:        '#pass_recovery',
		        url:            'hook/et/siteuser/passrecover',
		        beforeSubmit:  function(){ $('#loading').show() },
		        success:       function(){ $('#loading').hide() }
    	  });
    	  return false;
      });
      
      
      resizeStartPageBlocks();
      
      
      $('#comment').keyup(function(){
   		limitChars('comment', 1000, 'charlimitinfo');
   	  });
      
      $('#title').keyup(function(){
    	  limitCharsSimple('title', 130, 'snap_title');
      });
      
      $('#body').keyup(function(){
    	  limitCharsSimple('body', 400, 'snap_body');
      });
});




function resizeStartPageBlocks(){
	var a = $('div#lBooks').innerHeight(); 
	var b = $('div#lMovies').innerHeight(); 
	var c = $('div#lMusic').innerHeight(); 
	
	
	if( a > b ) {
		
		if ( a > c ) {
			var h = a;
		}
		else {
			var h = c;
		}
	}
	else {
		if( b > c ){
			var h = b;
		}
		else {
			var h = c;
		}
	}
	
	$('div.column').css('height', h);
}



function limitChars(textid, limit, infodiv){

	var text = $('#'+textid).val(); 
	var textlength = text.length;

	if(textlength > limit){
		$('#' + infodiv).html('On jäänud '+ (limit - textlength) +' tähemärki.');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else{
		$('#' + infodiv).html('On jäänud '+ (limit - textlength) +' tähemärki.');
		return true;
	}
}
function limitCharsSimple(textid, limit, infodiv){

	var text = $('#'+textid).val(); 
	var textlength = text.length;

	if(textlength > limit){
		$('#' + infodiv).html(''+ (limit - textlength) +'');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else{
		$('#' + infodiv).html(''+ (limit - textlength) +'');
		return true;
	}
}



function msgs_delete(){
	var r=confirm("Kas oled kindel, et tahad kustutada valitud kirjad?");
	if (r==true)
  	{
		document.getElementById('fdelete').value=1; 
		$('#mcb').submit();
  	}
}


/*
$(function() {
	$('#body').wysiwyg();						
});
*/
function renderResult()
{
    var content = $('#body').val();
    console.log(content);
    $('#result').html(content.replace('<img src="/js/jwysiwyg/letter.gif" alt="">',''));
}


// entry ajax functions

function addfriend(id, url, data, load, target){ // used by: add contact, add friend
	$.ajax({
		url: url,
		cache: false,
		type: "POST",
		data: data,

		success: function(html){
			$(target).load(load);
		}
	});
}
function af(id, url, data, load, target){ // used by: add contact, add friend
	$.ajax({
		url: url,
		cache: false,
		type: "POST",
		data: data,

		success: function(html){
			$(target).load(load);
		}
	});
}
function dm(msg){ // dummy for functional message
	$('div#msg').empty().append('<div>' + msg + '</div>');
	setTimeout(function() { $('div#msg div').fadeOut(); }, 3000);
}
function ajaxFileUpload( l_id )
{
	$("#pr_image_loading").ajaxStart(function(){
		$(this).show();
	}).ajaxComplete(function(){
		$(this).hide();
		$('div#images').load('/hook/et/look/fileuploadresult', { look_id:  '' + l_id+ '' } );
	});

	$.ajaxFileUpload
	(
		{
			url:'/hook/et/look/addprimarylook?look_id=' + l_id,
			secureuri:false,
			fileElementId:'upfile',
			dataType: 'json'
		}
	)
	return false;
}


function ajaxSnapUpload( s_id )
{
	$("#pr_image_loading").ajaxStart(function(){
		$(this).show();
	}).ajaxComplete(function(){
		$(this).hide();
		$('div#images').load('/hook/et/snap/fileuploadresult', { snap_id:  '' + s_id+ '' } );
	});

	$.ajaxFileUpload
	(
		{
			url:'/hook/et/snap/addprimarysnap?snap_id=' + s_id,
			secureuri:false,
			fileElementId:'upfile',
			dataType: 'json'
		}
	)
	return false;
}

function ajaxFileUploadDetail( l_id )
{
	$("#detail_loader").ajaxStart(function(){
		$(this).show();
	}).ajaxComplete(function(){
		$(this).hide();
		$('div#images').load('/hook/et/look/fileuploadresult', { look_id:  '' + l_id+ '' } );
	});

	$.ajaxFileUpload
	(
		{
			url:'/hook/et/look/addlookdetail?look_id=' + l_id,
			secureuri:false,
			fileElementId:'updetail',
			dataType: 'json'
		}
	)
	return false;
}

function ajaxSnapUploadDetail( s_id )
{
	$("#detail_loader").ajaxStart(function(){
		$(this).show();
	}).ajaxComplete(function(){
		$(this).hide();
		$('div#images').load('/hook/et/snap/fileuploadresult', { snap_id:  '' + s_id+ '' } );
	});

	$.ajaxFileUpload
	(
		{
			url:'/hook/et/snap/addsnapdetail?snap_id=' + s_id,
			secureuri:false,
			fileElementId:'updetail',
			dataType: 'json'
		}
	)
	return false;
}


function aform(form_id, target, url, loading_id){
	$( '#' + form_id ).ajaxSubmit({
        target:        '#' + target,
        url:            url,
        beforeSubmit:  function(){ $('#' + loading_id).show() },
        success:       function(){ $('#' + loading_id).hide() }
	});
	return false;
}