$(document).ready(function(){

	/*
		Submiting send msg form
	*/
    $('#submit').click(function() {
        $('#send_msg').ajaxSubmit({
            url: '/promo/message/check-params',
            dataType: 'json',
			beforeSubmit: function(){
				if ($('#wid').val() == '') {
 					$('#TB_window').css('height','100px');
					$('#TB_ajaxContent').css('height','85px');
                    $('#msg').html('Molimo odaberite častitku s klikom na pahulje. <a href="#" class="close" onclick="tb_remove()">x</a>');
                    $('#msg').show();					
					return false;
				}				
			},
            success: function(data){
                if (data.success == true) {
                    $('#receiver_msisdn2').val(data.msisdn);
                    $('#msg').hide();
                    $('#insert_pin').hide();
                    $('#send_pin').show();
                } else {
					$('#send_pin').hide();
					$('#TB_window').css('height','85px');
					$('#TB_ajaxContent').css('height','70px');
                    $('#msg').html(data.message + '<a href="#" class="close" onclick="tb_remove()">x</a>');
                    $('#msg').show();
                }
            }
        });
    });
	/*
		Submiting send pin form
	*/
	$('#send_pin').ajaxForm({
		dataType: 'json',
		beforeSubmit: function(){
			if ($('#name') == '')
				return false;
			$('#msg').hide();
			$('#loader').show();
			$('#TB_window').css('height','240px');
			$('#TB_ajaxContent').css('height','225px');
		},
		success: function(data){
			if (data.success == true || data.code == 1003) {
				if (data.operator_id == 11) {
					$('#TB_window').css('height','170px');
					$('#TB_ajaxContent').css('height','155px');
					$('#intro').show();
				} else {
					$('#TB_window').css('height','115px');
					$('#TB_ajaxContent').css('height','100px');
				}
				$('#insert_pin').show();
				$('#send_pin').hide();
				$('#msg').hide();
                $('#message2').val($('#message').val());
                $('#name2').val($('#name').val());
                $('#msisdn2').val($('#msisdn').val());
                $('#operator_id2').val($('#operator_id').val());
				$('#loader').hide();
			} else {
				$('#TB_window').css('height','255px');
				$('#TB_ajaxContent').css('height','240px');
				$('#msg').html(data.message);
				$('#msg').show();
				$('#loader').hide();
			}
		}
	});
	/*
		Submiting insert pin form
	*/
	$('#insert_pin').ajaxForm({
  		dataType: 'json',
		beforeSubmit: function(){
			$('#msg').hide();
			$('#TB_window').css('height','165px');
			$('#TB_ajaxContent').css('height','150px');
			$('#loader').show();
		},
		success: function(data){
			if (data.success == true) {
				$('#insert_pin').hide();
				$('#intro').hide();
				$('#loader').hide();
				$('#TB_window').css('height','85px');
				$('#TB_ajaxContent').css('height','70px');
				$('#msg').addClass('success');
				$('#msg').html('Poruka je uspješno poslana.<a href="#" class="close" onclick="tb_remove()">x</a>');
				$('#msg').show();
			} else {
				if ($('#operator_id2').val() == 11) {
					$('#TB_window').css('height','210px');
					$('#TB_ajaxContent').css('height','195px');
				} else {
					$('#TB_window').css('height','165px');
					$('#TB_ajaxContent').css('height','150px');
				}
				$('#msg').html(data.message);
				$('#msg').show();
				$('#loader').hide();
			}
		}
	});
	/*
		Hide error msg on close
	*/
	$('.cancel').click(function(){
		$('#msg').css('display','none');
	});
	/*
		Change selected image in phone
	*/
	$('.krog').click(function(){
		$('#preview').attr('src', 'http://www.ispace.si/getimage/w_id/' + $(this).attr('id') + '/width/186/ar/1/crop/1/anim/1');
		$('#preview').animate({opacity:0.1},100).animate({opacity:1},100).animate({opacity:0.5},100).animate({opacity:1},100);
		$('#wid').val($(this).attr('id'));
		return false;
	});
	/*
		Clear textarea
	*/
	$('textarea').focus(function(){
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	});
	$('textarea').blur(function(){
		if ($(this).val() == "") {
			$(this).val($(this).attr('title'));
		}
	});
	/*
		Clear input
	*/
	$('.text').focus(function(){
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	});
	$('.text').blur(function(){
		if ($(this).val() == "") {
			$(this).val($(this).attr('title'));
		}
	});
	/*
		Send PIN again
	*/
	$('#send-pin-again').click(function(){
		$('#insert_pin').hide();
		$('#send_pin').show();
	});
});
