$(document).ready(function(){
	//INPUTS 
	$("input.text").focus(function () {
		$(this).addClass('focus');  	
	});
	$("input.text").blur(function () {
		$(this).removeClass('focus');  	
	});
	$("textarea.text").focus(function () {
		$(this).addClass('focus');  	
	});
	$("textarea.text").blur(function () {
		$(this).removeClass('focus');  	
	});
	$("input.text_small").focus(function () {
		$(this).addClass('focus_small');  	
	});
	$("input.text_small").blur(function () {
		$(this).removeClass('focus');  	
	});
	//BASIC		
	$('.gosubmit').change(function () {
		$(this).parents("form:first").submit(); 
	});
	//SUBMIT		
	$('.onChangeSubmit').change(function () {
		$(this).parents("form:first").submit(); 
	});
	
	//CLEAR
	var old;
	$('.clear').click(function(){ 
		old = this.value;
		if( $(this).attr('title') != '1')
			this.value = '';
	}).blur(function () {
		if(this.value == '')
			this.value = old;
		else
			$(this).attr('title', '1');	
	});
	
	//PROPAGE CENTER
	$('.jCenter').each(function() {
		
		$(this).css({display:'none', position:'absolute'});
		$(this).parent().css({position:'relative'});
		var width = $(this).attr('width'); 
		var height = $(this).attr('height');
		
	 	if(width == '0')
			width = $(this).css('width');
		if(height == '0')
			height = $(this).css('height');
		
		$(this).css({left: '50%', marginLeft: -parseInt(width)/2});
		$(this).css({top: '50%',  marginTop : -parseInt(height)/2}); 
		$(this).fadeIn(500);
	}); 
		
	//ACTIVE 
	$(".listing li").hover(
      function () {
			$(this).addClass('active');
      }, function () {
	  		$(this).removeClass("active");
      }
    );
	
	$('.activetd').click(function() {
		$(this).parents('table:first').find('td').removeClass('active');
		$(this).parent().addClass('active');	
	});
	
	$('.activli').click(function() {
		$(this).parents('ul').find('li').removeClass('active');
		$(this).parents('li').addClass('active');	
	});
	
	//TRACE
 	$('.trace').click(function() {
	$.ajax({
		type: "GET",
		dataType: "html",
		url: jQuery.root+"/ajax/trace.php",
		data: { name: 'Kliknięcie', desc: $(this).attr("title") }
	});});
	
	$('a[target="_blank"]').click(function() {
	$.ajax({
        type: "GET",
		dataType: "html",
        url: jQuery.root+"/ajax/trace.php",
		data: { name: 'Zewnętrzny link' , desc: $(this).attr("href") }
    });});
	
	//AJAX
	$(".ajax_submit").live("click", function(){
		var href = $(this).attr('href');
		var form = $(this).parents('form:first');
		Boxy.load(form.attr('action'), {title: $(this).attr('title'), closeable: true, dataType: 'json', type: 'GET'},  form.serialize() , false);
		return false;
	});
	
	
	
	$(".ajax_get").live("click", function(){
		var href = $(this).attr('href'); 
			href = href.split("?");
	 	
		if(href[0].lastIndexOf(jQuery.root)==0)
		{
			href[0] = href[0].substring(jQuery.root.length+1,href[0].length);	
		}
		var jgn 		= false;
		var dataType 	= 'json';
		
		if($(this).hasClass('html')) 
			dataType = 'html';
			
		if($(this).hasClass('jgn')) 
			jgn = true; 	
		
		Boxy.load(href[0], {title: $(this).attr('title'), dataType: dataType, type:'GET', jgn:jgn, draggable:true}, href[1]);
		return false;
	});

	
	$(".ajax_get_html").live("click", function(){
		var href = $(this).attr('href');
			href = href.split("?");
		Boxy.load(href[0], {title: $(this).attr('title'), dataType: 'html', type:'GET'}, href[1]);
		return false;
	});
	
	//ZAGIEL
	function validate_Zagiel() {if (document.form_zamowienie.id_nazwy_pola_raty_zgoda.checked == false) {alert ("Zanim złożysz zamówienie, zapoznaj się z procedurą udzielenia kredytu ratalnego E-kredyt Żagiel.");	return false;} else {return true;}}	
	
	//INFO
	if($("#foobar").length > 0){	
		Boxy.loadText($("#foobar").html(), {title: $("#foobar_title").html()});
	}
	
	//LIVECHAT 
	$("#liveChat #input").live('keypress', function(e) {
		if( e.which == 13 ){
			$.ajax({
				type: "GET",
				url: jQuery.root +"/ajax/liveChat/send.php",
				data: { message: $(this).val()}
			});
		$(this).val('');
		heartBeat();	
			return false;
		}
	});
	
	//CART SHIPPING VALUE
    $('.costs').click(function(){
		var payment_id  = $('input[name="payment"]:checked').attr('value');
		var shipping_id = $('input[name="shipping"]:checked').attr('value');
		if(payment_id==2)
		{
			if(shipping_id==5)
				$("#shipping1").attr('checked', true); 
				
			$("input[value=5]").attr('disabled', true); 	
			
		}
		else
		{
			$("input[value=5]").attr('disabled', false); 
		}
		
		
		$.ajax({
		   type: "POST",
		   url: jQuery.root + "/ajax/shipping.php",
		   data:{platnosc: payment_id, wysylka: shipping_id},
		   dataType: "json",
		   beforeSend: function(data){
				loading = new Boxy('<div class="loading"></div>', {modal:false}); 
		   },
		   success: function(data){
				$("#ajax_shipping").html(data.selected);			
				$.each(data.shipping, function(key,value) {
					
					
					
					$("#ajax_shipping_"+key).html(base64_decode(value));
				});
				$("#ajax_total").html(data.total);
				$("#ajax_total_words").html(data.total_words);
				loading.hide();
		   }
		 });
	 });	
 });		 
