var submit = true;

$(document).ready(function()
{
	/*INPUT TIP*/
	$("input[type=text]").inputTip({ });

	
	/*make sure dubbel click whil not result in a dubbel post*/
	$(".form").submit(function() {
	 
	  if(submit){
		  submit = false;
		  return true;
	  }else{
		  return false;
	  }
	});
	
	//do not display can not in css du to jQuery
	if($("input[@name='language']:checked").val() != 'pl'){
		$(".doNotShow").css({
	   		'display'	:   'none'
	   	});
	}
	
	$(".checkPL").click(function(el) {
		if($(this).attr('value') == 'pl'){
			$(".doNotShow").css({
           		'display'	:   ''
           	});
			

		}else{
			$(".doNotShow").css({
           		'display'	:   'none'
           	});
		}
	});
	
	/*GMAPS*/
	if($("#gmapTest").length > 0 )
	{
		points[0].zoom = 10;
		
		$("body").gmaps({
			canvas : 'gmapTest',
			errors: 'gmapError',
	   		key: '',
	   		mapPoints: points,
	   		mapCenter: points[0]
		});
	}
	
	
	/*One Click Upload*/
	if($("#profileChangeImg").length > 0 )
	{
		new AjaxUpload('#profileChangeImg',
		{
			action : '/profile',
			data :{
				userId : $("#userId").attr('value'),
				action : $("#action").attr('value')
			},
			onSubmit: function(file, extension)
			{
			    if (! (extension && /^(jpg|png|jpeg|gif)$/i.test(extension))){
                        // extension is not allowed
			        	$("body").lightbox({
							dialogeBackground : 'transparent',
							dialogeHeight: '111',
							dialogeHtml: "" +
								"<div class=\"dialogeContent\">" +
									"<img src=\"gfx/dialoge/warning.png\" alt=\"error\" />" +
									" 	Error: invalid file" +
									"<div class=\"exit\">" +
								"</div>"
									
						});
                        return false;
                }
				$('#loader').fadeIn('slow');
			},

			onComplete : function(file, response)
			{
				$('#debug').html(response);
				var url = 'siteuser/get_image';

				if($("#action").attr('value') == 'uploadCompanyLogo')
				{
					url = 'siteuser/get_logo';
				}
				
				$('#profileImgContaner').load(url, {'id' : $("#userId").attr('value')}, function() {
					 // alert('Load was performed.');
					  addRoundCorner();
				});
				$('#loader').fadeOut('slow');
			}
		});
	}

	/*Simpel Accordion*/
	$(".vacancyReactionOverview .item").click(function() {
		var that = $(this);
		
		if(!that.hasClass("active")){
			//console.log('have to close');
			
			$(".vacancyReactionOverview .active").animate({
				height: '17px'
			  	}, 500, function() {
			  //   Animation complete.
			});
			$(".vacancyReactionOverview .active").removeClass("active");
		}
		
		child = that.children('.content');
		that.addClass("active");
		that.animate({
			height: (child.innerHeight()+50)+'px'
		  }, 500, function() {
		    // Animation complete.
		});
		//$(this).next().slideDown();
	})
	
	/*Toggle down extend regester*/
	$('.show_extended_record').click(function() {
		if($('#'+$(this).attr('value')).css("display") == "none"){ 
			$('.specialization').slideUp('slow');
			$('#'+$(this).attr('value')).slideDown('slow');
		}
	});
	
	$('.datepicker').datepicker();
	/* NOT WORKING STUPID JQuery ;)
	$('#datepicker').datepicker('option', {dateFormat: dd/mm/yy});
	*/

	/* dont show HOVER EFFECT */
	/*
	$(".eventSelected").each(function(){
		var obj = $(this);
		var childEvent =$('.event', obj);

		obj.mouseenter(function() {
			childEvent.fadeIn();
			//childEvent.css("display", "block");
	   });

		childEvent.mouseleave(function(){
			childEvent.fadeOut();
			//childEvent.css("display", "none");
		});
	});
	*/

	$(window).resize(function()
	{
		$('div.contentArea').css('min-height', $('html').height() - $('div.footer').height());
	});
	$('div.contentArea').css('min-height', $('html').height() - $('div.footer').height());

	showTimer();

	$('form[name=zlotyEuro]').each(function()
	{
		var $form = $(this);
		var $inputs = $form.find('input.input');

		$inputs.keyup(function()
		{
			var $input = $(this);
			$input.val(getDecimal($input.val()));

			if ($input.val().length > 0)
			{
				$(this).prevAll('input.input').val('');
				$(this).nextAll('input.input').val('');
			}
		});

		$form.children('input[name=calc]').click(function()
		{
			getZlotyEuro();
			return false;
		});

		$form.submit(function()
		{
			getZlotyEuro();
			return false;
		});
	});
	addRoundCorner();
});


function addRoundCorner()
{
	$('.rounded').each(function(){
		var $rounded = $(this);
		if ($rounded.children('div.topLeft, div.topRight, div.bottomLeft, div.bottomRight').length == 0)
		{
			$rounded.append(
				'<div class="topLeft"></div>' +
				'<div class="topRight"></div>' +
				'<div class="bottomLeft"></div>' +
				'<div class="bottomRight"></div>'
			);
		}
	});
}

__timerId = 0;
function showTimer()
{
	clearTimeout(__timerId);
	var oTime = new Date;
	var aTime = [
		oTime.getDate(),
		(oTime.getMonth()+1),
		oTime.getFullYear(),

		oTime.getHours(),
		oTime.getMinutes()
	];
	for (var i = 0; i < aTime.length; i++)
	{
		if (aTime[i] < 10)
		{
			aTime[i] = '0' + aTime[i].toString();
		}
		else
		{
			aTime[i] = aTime[i].toString();
		}
	}

	$("div#timer").html(aTime[0] + '/' + aTime[1] + '/' + aTime[2] + ' &bull; ' + aTime[3] + '.' + aTime[4] + ' godz.').fadeIn('slow');
	__timerId = setTimeout('showTimer()' , 5000);
}

function getDecimal(str)
{
	if (str.indexOf(',') > -1)
	{
		return str.match(/.*\,|.*/)[0].replace(/[^\d]/g, '') + str.match(/(?:\,[^\,]*)?$/)[0].replace(/[^0-9\,]/g, '');
	}

	return str.match(/.*\.|.*/)[0].replace(/[^\d]/g, '') + str.match(/(?:\.[^\.]*)?$/)[0].replace(/[^0-9\.]/g, '');
}

function getZlotyEuro()
{
	var $form = $('form[name=zlotyEuro]');
	var oValues = {
		'zloty':    $form.find('input.zloty').val(),
		'euro':     $form.find('input.euro').val()
	};

	$.getJSON(
		'/ajax/getZlotyEuro.php',
		{
			action:	'convert',
			values:	oValues
		},
		function(data)
		{
			var $form = $('form[name=zlotyEuro]');
			if (typeof(data.error) == 'string')
			{
				//alert(data.error);
			}
			else if (typeof(data.euro) == 'string')
			{
				$form.find('input.euro').val(data.euro);
			}
			else if (typeof(data.zloty) == 'string')
			{
				$form.find('input.zloty').val(data.zloty);
			}
		}
	);

	return false;
}