$(document).ready(function(){
$('#news_msg').hide();
var base_url = 'http://'+location.host;
$('#news_email').val('Twój email');
$('#remail').val('Twój email');
$('#name').val('imi?');
$('#phone').val('telefon');
$('#topic').val('Wybierz temat');
$('#cmf-skinned-text').val('Wybierz temat');
	high = $('#submenu').css('height');
		$('#submenu').hide();
		$('#submenu').css('height','0');
		$('#submenu').show();
		$('#submenu').animate({
			height: high
			}, 500, function(){
			// Animation complete.
   		});
  $("#topic").select_skin();
	
	$('#erclose').live('click',function(){
		$('#errormsg').hide();
		$('#blockpage').hide();
	});
	
  $('#send').live('click',function(){
	  var json = new Object();
	  json = {'json' : [{'name' : $('#name').val(), 'phone' : $('#phone').val(), 'topic' : $('#topic').val()}]};
	  if(isRequ($('#name').val()) == false){
		  $('#name').val('To pole jest wymagane');
		  $('#name').focus();
	  }
	  else{
		  if(isRequ($('#phone').val()) == false){
			  $('#phone').val('To pole jest wymagane');
			  $('#phone').focus();
		  }
		  else 
		  {
			  $.ajax({
				  url:base_url+'/index.php/main/sendform',
				  data: json,
				  type: 'POST',
				  success: function(data){
					  $('#name').val('imi?');
					  $('#phone').val('telefon');
					  $('#topic').val('Wybierz temat');
					  $('#cmf-skinned-text').val('Wybierz temat');
					  $('#blockpage').show();
					  $('#errormsg').show();
			  		  $('#msg').html(data);
				  }
			  });
		  }		
	  }
  });
  
  	$('#news').live('click',function(event){
	  	$(this).hide();
		$(this).css('cursor','text');
		$('#blockpage').fadeIn('slow');
	  	$(this).addClass('shownews').fadeTo('slow',1);
	});	
	$('#news').live('click',function(event){
		$(this).toggle(function(event){
			var target = event.target.id;
	  		if(target == 'close'){
				$('#blockpage').hide();
				$(this).hide();
				$(this).removeClass('shownews');
				$(this).css('cursor','pointer');
				$(this).fadeIn('slow');
			}
			if(event.target.tagName == 'A'){
				var adres = event.target.href;
				window.open(adres);
				$('#blockpage').hide();
				$(this).hide();
				$(this).removeClass('shownews');
				$(this).css('cursor','pointer');
				$(this).fadeIn('slow');
			}
		},
		function(event){
			$(this).hide();
			$(this).css('cursor','text');
			$('#blockpage').fadeIn('slow');
			$(this).addClass('shownews').fadeTo('slow',1);
		});
	});
	  
  
  $('#archive').live('click',function(event){
	  $.ajax({
		 url:base_url+'/index.php/pozycjonowanie/get_more_news',
		 success:function(data){
			$('#showfullnews').append(data); 
			$('#showfullnews').hide();
	  		$('#showfullnews').addClass('showfull');
	  		$('.top').fadeTo('slow',1);
	 		$('.bot').fadeTo('slow',1);
	 		$('#showfullnews').fadeTo('slow',1);
		 }
	  });
	  
  });
  
  $('.closear').live('click',function(){
	  $.ajax({
		 url: base_url+'/index.php/pozycjonowanie/get_news',
		 success: function(data){
			 $('#showfullnews').html(data);
			 $('#showfullnews').removeClass('showfull');
			 $('.top').hide();
			 $('.bot').hide();
			 $('#showfullnews').hide();
			 $('#showfullnews').fadeTo('slow',1);
		 }
	  });
	  
  });
  
	$('.top').hide();
	$('.bot').hide();
	
	$('#rsend').live('click',function(){
		var email = new Object();
		email = {'email' : $('#remail').val()};
		$.ajax({
				  url:base_url+'/index.php/main/raportmail',
				  data: email,
				  type: 'POST',
				  success: function(data){
					  $('#remail').val('Twój email');
					  $('#blockpage').show();
					  $('#errormsg').show();
			  		  $('#msg').html(data);
				  }
			  });
	});
	
	$('#news_send').live('click',function(){
		var email = new Object();
		email = {'email' : $('#news_email').val()};
		$.ajax({
				  url:base_url+'/index.php/main/subscribe',
				  data: email,
				  type: 'POST',
				  success: function(data){
					  $('#news_email').val('Twój email');
					  $('#news_msg').html(data);
					  $('#news_msg').slideDown('slow');
				  }
			  });
	});
	
	/*$(':input').live('focus',function(event){
		var vlu = event.target.value;
		var cel = '#'+event.target.id;
		$(cel).val('');
			myVal.assing(vlu, cel);
	});
	
	$('body').live('click',function(event){
		var myTarget = '#'+event.target.id;
		if(myVal.myId !== myTarget){
			$(myVal.myId).val(myVal.vlu);
		}
	});*/
	
	$('#formula').load(base_url+'/index.php/main/get_formula');
	
	$('#fsend').live('click',function(){
		var json = new Object();
		json = {'json':[{'subject' : $('#fsub').val(), 'email' : $('#fmail').val(), 'message' : $('#fcon').val()}]};
		adres = base_url+'/index.php/main/send_formula';
		$.ajax({
			url: adres,
			type: 'POST',
			data: json,
			success: function(data){
					  $('#msg_1').html(data);
					  $('#fsub').val('');
					  $('#fmail').val('');
					  $('#fcon').val('');
				  }
		});
	});
	
//---zamow oferte-------------------------------------------------------------------------------------	
	$('input[type=checkbox]').each(function() {
		$(this).wrap(function() {
			return ($(this).is(':checked')) ? '<div class="custom_checkbox selected" />' : '<div class="custom_checkbox" />';
		});
	});
	
	$('.custom_checkbox input[type=checkbox]').click(function () {
		$(this).parent().toggleClass('selected');
	});
	
	$('#send_offer').live('click',function(){
		var jform = new Object();
		jform = {'jform' : [{'url' : $('#site_url').val(), 'keywords' : $('#your_keywords').val(), 'email' : $('#your_mail').val(), 'name' : $('#your_id').val(), 'phone' : $('#your_phone').val(), 'subscrib' : $('#new_promo').is(':checked')}]};
		adres = base_url+'/index.php/oferta_form';
		$.ajax({
			url: adres,
			type: 'POST',
			data: jform,
			success: function(data){
				var datas = new Object();
				datas = jQuery.parseJSON(data);
				if(datas.stat == 'false'){
					$('#status').html(datas.message);
				}else{
					window.location.href = "http://www.eactive-pozycjonowanie.pl/zamow_oferte_final.html";
				}
			}
		});
	});
//---end--------------------------------------------------------------------------------------------
	
var myVal = new clearIn();
//Additional functions------------------------------------------------------------------------------
function validateName(data){
	str = data;
	pat = /[A-Za-z?óŝşŚź????????????]+/g;
	if(str == 'imi?'){
		return false;
	}
	else{
		if(str.match(pat))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

function isRequ(data){
	if(data == null || data =="")
	{
		return false;
	}
	else
	{
		return true;
	}
}

function validatePhone(data){
	str = data;
	pat = /[0-9]+/g;
	if(str == 'telefon'){
		return false;
	}
	else{
		if(str.match(pat)){
			return true;
		}
		else{
			return false;
		}
	}
}

function clearIn(){
	this.vlu;
	this.myId;
}

clearIn.prototype.assing = function(data, Idi){
	this.vlu = data;
	this.myId = Idi;
}


//facebook show
$('#facebox').live('mouseover',function(){
		//$(this).animate({left: '0px'},"slow");
		$(this).animate({left: '0px'}, { duration: 500, queue: false });
});

$('#facebox').live('mouseout',function(){
		$(this).animate({left: '-203px'}, { duration: 500, queue: false });
		//$(this).css('left','-204px');
});

});
