$(document).ready(function() {
		$('table tr').hover(
			function(){$(this).toggleClass('hover')},
			function(){$(this).toggleClass('hover')}
		);
		
		$('#gestionarOfertas tbody tr, table#contactos tbody tr').css({'cursor':'pointer'}).click(function(){
			window.location = $(this).children('td').find('a').attr('href');
		});
		$('#chooseEmailRecipients table .firstCheckbox').mouseup(function(){
			var chk = $(this).attr('checked');
			var nchk=(chk == true) ? '' : 'checked';
			$(this).parents('form').find('.sendCheckboxMaster, .sendCheckbox').attr('checked',nchk);
			///check this	
		});
		$('#chooseEmailRecipients table .sendCheckboxMaster').mouseup(function(){
			var chk = $(this).attr('checked');
			var nchk=(chk == true) ? '' : 'checked';
			$(this).parents('table').find('.sendCheckbox').attr('checked',nchk);
			///check this	
		});
		$('.sendCheckbox').mouseup(function(){
			if ($(this).hasClass('userCheck')){
				// alert('ok');
			}else{
				var chk = $(this).attr('checked');
				
				var nchk=(chk == true) ? '' : 'checked';
				$(this).attr('checked',nchk);
			}
			///check this
		});
        $('#type').change(function(e){
           // alert('changed');
            $(this).parents().find('form').submit();
        
        });

		$('.userCheck').mouseup(function(){
			var chk = $(this).attr('checked');
			var cls=$(this).attr('class').replace('userCheck sendCheckbox ','');
			//alert(cls);
			var nchk=(chk == true) ? '' : 'checked';
			$('.' + cls).attr('checked',nchk);
			///check this
		});

		$('#chooseEmailRecipients table tbody tr.user').css({'cursor':'pointer'}).click(function(){
			var chk = $(this).children('td').find('.sendCheckbox').attr('checked');
			var cls=$(this).children('td').find('.sendCheckbox').attr('class').replace('userCheck sendCheckbox ','');
			//alert(cls);
			var nchk=(chk == true) ? '' : 'checked';
			$('.' + cls).attr('checked',nchk);
			return false;
			///check this	
		});
		
		$('#chooseEmailRecipients table tbody tr').css({'cursor':'pointer'}).click(function(){
			if ($(this).hasClass('user')){
				//alert('ok');
			}else{
				var chk = $(this).children('td').find('.sendCheckbox').attr('checked');
				var nchk=(chk == true) ? '' : 'checked';
				$(this).children('td').find('.sendCheckbox').attr('checked',nchk);
				return false;
			}
			///check this	
		});

		$('#help, #partners').css({'cursor':'pointer'}).click(function(){
				window.location = $(this).children('h3').find('a').attr('href');
		});
		
    $("a[rel='blank']").click(function() { window.open(this.href); return false; });
    $("#search_button").hide();
    

    $("input.input-text").each(function (type) {

 
		$(window).bind('load', function () {
			
			setTimeout(function(){
				$("input.input-text").each(function (type) {
					//alert($(this).val());
					if ($(this).val()!='') {
						$(this).prev("label.inlined").addClass('has-text');
						//$(this).prev("label.inlined").fadeTo(0,0);
						$(this).prev("label.inlined").hide();
						//$(this).prev("label.inlined").css({'margin':'9999px 0 0 0'});
					}
				});
			}, 200);
			
		});
		
		$(this).focus(function () {
			//$(this).parent().find("label.inlined").addClass("focus");
			//$(this).parent().find("label.inlined").hide();
			$(this).prev("label.inlined").fadeTo(200,0.35);
			//$(this).prev("label.inlined").css({'margin':'0px'});
		});
				 
		$(this).keypress(function () {
			//$(this).parent().find("label.inlined").addClass("has-text");
			//$(this).parent().find("label.inlined").removeClass("focus");
			//$(this).parent().find("label.inlined").hide();
			$(this).prev("label.inlined").fadeTo(200,0);
			//$(this).prev("label.inlined").css({'margin':'9999px 0 0 0'});
		});
				 
		$(this).blur(function () {
			if($(this).val() == "") {
				//$(this).prev("label.inlined").removeClass("has-text");
				//$(this).prev("label.inlined").removeClass("focus");
				$(this).prev("label.inlined").fadeTo(200,1);
				//$(this).prev("label.inlined").css({'margin':'0px'});
			}
		});
	});

/*--------------*/
/*  CONDITIONS  */
/*--------------*/
	$('ul.conditionsList li dl dd').hide();
	
	$('ul.conditionsList li dl dt').click(function(e){
		var thisdd=$(this).next('dd');
		thisdd.slideToggle();
		$(this).parents('div.top').css('height','auto');
		e.preventDefault();
		return false;
	});
	/*
	var maxh=0;
   $("ul.conditionsList div.top").each(function (type) {
		//alert ($(this).height());
		if ($(this).height() > maxh)
			maxh=$(this).height();
	});
	$("ul.conditionsList div.top").height(maxh);
	* */
	
	/*=====================*/
	/*=== SEND A FRIEND ===*/
	/*=====================*/

    $('input#friend_name').keyup(function(e){
        var name=$(this).attr('value');
        var textarea = $(this).parents().find('form textarea#message');
        var orig_text=$("div#orig_text");
        //alert(orig_text.html());
        var friend_text=orig_text.text().replace(/\[friend_name\]/gi,name);

        textarea.text(friend_text);
    });

        /*=====================*/
        /*==== MAKE OFFER =====*/
        /*=====================*/

        var textarea = $('input#net_price').parents().find('form textarea#message');
        //$('input#net_price').parents().find('form').append('<div id="orig_text" />');

        var orig_text=textarea.val();
        //alert(orig_text);
        //$('#orig_text').text(orig_text);

    $('input#net_price, input#volume').keyup(function(e){
        var net_price=$('input#net_price').attr('value');
        var volume=$('input#volume').attr('value');
        //alert(orig_text.html());
        //alert(orig_text);
        var friend_text=orig_text.replace(/\[NET_PRICE\]/gi,net_price).replace(/\[UNITS\]/gi,volume);
        //alert(friend_text)
        //textarea.text("");
        textarea.val(friend_text);
    });
    if($('.rte-zone').length){
        $('.rte-zone').rte("/assets/css/rte.css", '/assets/img/');
    }

    if($('#recoverForm').hasClass('hasError')){
        $('#loginForm').css({display:'none'});
    }else{
        $('#recoverForm').css({display:'none'});
    }


    $('#loginForm .recover a').click(function(e){
        e.preventDefault();
        $('#recoverForm').slideDown()
        $('#loginForm').slideUp();
    });

});

