jQuery.log = function(message) { if(window.console) { console.debug(message); } };


$(function(){

	$('a').click(function(){$(this).blur()});
	$("#accordion").accordion({
		autoHeight: false,
		collapsible: true,
		navigation: true
	});

	var hash = window.location.hash;
	var thash = hash.substring(hash.lastIndexOf('#'), hash.length);
	if(thash){
	 		$('#accordion').find('a[href*='+ thash + ']').closest('h3').trigger('click');
	}


    $("#accordion_nd").accordion({
		header: "h3",
        autoHeight: false,
		collapsible: true,
		navigation: true,
        active: -1,
        clearStyle: true
	});

/*
|--------------------------------------------------------------------------
|  Sting event
|--------------------------------------------------------------------------
*/
    $sting = [];
	//$sting = $('#image_sting_home');

	if($sting.length > 0)
	{


		$sting.bind({

			click: function(){
				 var $link = $(this).find('a');
				 window.open($link.attr('href'),'_self');
				 //$.log($link);
				 //$link.click();//ff crashes
				 return false;

			},
			mouseenter: function() {

				//return;

				$(this).find('.tag_medium_neg').hide();
				$(this).stop().css({
					'z-index':'9999'
				})
				.addClass('image_sting_home_big')
				.animate({
				    width: '754',
				    height: '500',
                    left: -18,
				    bottom:'24px'
				  }, {
				    duration: 200,
				    complete: function() {
				      //$.log('Animation complete.');
				    }
			    });
	    	},
			mouseleave: function() {

				//return;
				$(this)

				.animate({
				    width: '240',
				    height: '200',
				    left: '240',
				    bottom:'24px'
				  }, {
				    duration: 200,
				    complete: function() {
				     	$(this).removeClass('image_sting_home_big').find('.tag_medium_neg').fadeIn();
				    }
			    });
			}
		});
	}
/*
|--------------------------------------------------------------------------
|  End sting event
|--------------------------------------------------------------------------
*/

	$('#image_oferte_home,#image_produse_home,#image_solicita_home,#image_packet_home,#image_plan_personal,#image_sting_home').bind({
		click: function() {
			// do something on click
            //if ($(this).attr('id') != 'image_plan_personal') {
    			var url = $(this).find('a.more').attr('href');
    			window.location.href = url;
            //}
		},
		mouseenter: function() {
			$(this).find('.tag_medium_neg').addClass("box_hover");
		},
		mouseleave: function() {
			$(this).find('.tag_medium_neg').removeClass("box_hover");
		}
	});





	$("#formNewsletter label").inFieldLabels();


	/*	$('#subMenuTgr').toggle(function() {
	//$('#sub_menu').slideDown();
	}, function() {
	//$('#sub_menu').slideUp();
	});*/



	var $modal = $('<div id="modal"><a href="javascript:;" id="close" class="absolute replace"  onclick="$(\'#modal\').dialog(\'close\');">x</a><p></p></div>');

	$('.modal_tgr').live("click", function(){
		var sB = $(this).parent().next('.bancheri').html();


		$modal.find('p').html(sB);

		$modal.dialog({
			modal: false,
			title:'',
			height: 285,
			width: 580,
			closeText: 'X' ,

			dialogClass: 'modal'
		});

		return false;
	});


	var
	$solicitaForm = $('#formSolicita'),
	$step_1 = $('#step1',$solicitaForm),
	$step_2 = $('#step2',$solicitaForm);
	var  $data_s = $('#ds');
	//var  $data_m = $('#md');
	var  $data_no = $('#no_data');
	var  $data_no = $('#no_data');
	var  $ora = $('input[name="interval_time"]');
	var  $contact_opt = $('input[name="contact_opt"]');
	var  $location = $('#location');
	var  $no_location = $('#no_location');
	var $txt_confirm = $('#txt_confim');


	$step_2.hide();

	$('#no_location').click(function(){
		if($(this).is(':checked')) $('#location').val('');
	});


	$('#next').live('click' , function(){

		//$.log(Vanadium);

		if( $('input#name').val() == '' ||  $('input#phone').val() == ''  || ($data_s.val() == '' && $data_no.is(':checked') == false) || $ora.is(':checked') == false || $contact_opt.is(':checked') == false || ($location.val() == '' && $no_location.is(':checked') == false)){
			//if($location == '' && $no_location.length == 0)
			//$.log($no_location.length)
			alert('Te rugam sa introduci campurile obligatorii.');
			return false;
		}else{
			var loc = ($no_location.is(':checked')) ? $no_location.next().text() : $location.val();

			if($data_s.val() != ''){
				var ds = $data_s.val();


			}else{
				var ds =  $('input[name="data"]:checked').next().text();
			}

			//$.log($data_m.val());

			var nume_clear = strip_tags($('input#name').val());
			var email_clear = strip_tags($('input#email').val());
			var tel_clear = strip_tags($('input#phone').val());
			var messaj_clear = strip_tags($('#message').val());

			var txt_confirm  = '<p>Data solicitare : <strong class="bold yellow">'+ds+'</strong></p>';
			txt_confirm += '<p>Interval orar : <strong class="bold yellow">'+$('input[name="interval_time"]:checked').next().text()+'</strong></p>';
			txt_confirm += '<p>Locatia : <strong class="bold yellow">'+loc+'</strong></p>';
			txt_confirm += '<p>Vei fi contactat : <strong class="bold yellow">'+$('input[name="contact_opt"]:checked').next().text()+'</strong></p>';
			txt_confirm += '<p class="bold">Mesajul tau este:</p><p class="bold yellow">'+messaj_clear+'</p>';
			txt_confirm += '<div class="height20 clear"></div>';

			txt_confirm += '<p class="bold">Date de contact:</p>';
			txt_confirm += '<p>Nume : <strong class="bold yellow">'+nume_clear+'</strong></p>';
			txt_confirm += '<p>Email : <strong class="bold yellow">'+email_clear+'</strong></p>';
			txt_confirm += '<p>Telefon: <strong class="bold yellow">'+tel_clear+'</strong></p>';

			$txt_confirm.empty().html(txt_confirm);
			$step_1.hide();
			$step_2.fadeIn();

		}


	})
	$('#prev').click(function(){
		$step_2.hide();
		$step_1.fadeIn();
	});



	$('#formSolicita').ajaxForm({
		dataType: 'json',
		beforeSubmit:  function(){
			$('#loader').show();
			$('#send').hide();
		},
		success: function(data) {
			var d = new Date();
			if (typeof data.errors != 'undefined') {

				if(data.errors.captcha == 'undefined'){
					$step_2.hide();
					$step_1.fadeIn();
				}else{
					$('#captcha').attr('src','captcha?t='+d.getTime())
				}

				Vanadium.decorate(data.errors);

				$('#send').show();
				$('#loader').fadeOut('fast');
			}else{

				$('#main').empty().html(data.response)

			}


			return false;
		}
	});

	$('#formNewsletter').ajaxForm({
		dataType: 'json',
		beforeSubmit:  function(){

			$('#send').attr('disable',true);
		},
		success: function(data) {
			if (typeof data.errors != 'undefined') {
				Vanadium.decorate(data.errors);
				$('#send').attr('disable',false);
			}else{

				$('#formNewsletter').empty().html(data.response).parent().delay(1500).slideUp();

			}


			return false;
		}
	});




	/*$("#dialog-nl").dialog({
	autoOpen: false,
	modal: true,
	dialogClass: 'newslwtter_class',
	position: [350,100],
	buttons: {
	'Aboneaza-te': function() {


	},
	'Renunta': function() {
	$(this).dialog('close');
	}
	},
	close: function() {
	return false;
	}
	});

	*/

	$('#subscBtn').click(function() {
		$('#dialog-nl').slideToggle('fast');
		return false;
	});



});

function strip_tags(input, allowed) {
	   allowed = (((allowed || "") + "")
	      .toLowerCase()
	      .match(/<[a-z][a-z0-9]*>/g) || [])
	      .join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
	   var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
	       commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
	   return input.replace(commentsAndPhpTags, '').replace(tags, function($0, $1){
	      return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
	   });
	}
