$(document).ready(function(){
	validTarget("a.blank");
	valideForm("form.validate-form");
	
	/*BALIK V POHYBE*/	
	balik = function(gift_no, seconds){
		var temp = $('.gift' + gift_no + ' img').css('top');
		temp = temp.substr(0, temp.length - 2);
		var toppos = parseInt(temp);
		$('.gift' + gift_no + ' img').stop(true,true).animate({top : toppos - 6 + 'px'}, seconds/2, function(){$('.gift' + gift_no + ' img').animate({top : toppos + 'px'}, seconds/2);});
	};   
	   	
	vlnky = function(gift_no, seconds){
		var temp = $('.gift' + gift_no + ' span').css('top');
		temp = temp.substr(0, temp.length - 2);
		var toppos = parseInt(temp);
		$('.gift' + gift_no + ' span').animate({top : toppos - 3 + 'px'}, seconds/4, function(){$('.gift' + gift_no + ' span').animate({top : toppos + 'px'}, seconds/2);});
	};
	
	gift_interval = function(gift_no, seconds){
		var origin_img = $('.gift' + gift_no + ' img').css('top');
		var origin_span = $('.gift' + gift_no + ' span').css('top');
		play = setInterval(function(){
			$('.gift' + gift_no + ' img').css('top', origin_img);
			balik(gift_no, seconds);			
		},seconds);             
		play2 = setInterval(function(){
			$('.gift' + gift_no + ' span').css('top', origin_span);
			vlnky(gift_no, seconds);			
		},seconds);
	};
	/*\ BALIK V POHYBE*/
	
	
	$('body').supersleight();
	$('.header input').supersleight();
	
	$("a.fancy").fancybox({
		'titleShow' : true,
		'titlePosition' : 'over',
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic'
	});
	
	$(".field input").focus(function(){
		$(this).parent('div').addClass('write');
		
		if (this.value==this.title){
			this.value='';
		}
		else{
			//this.select();
		}
	});
	$(".field input").blur(function(){
		$(this).parent('div').removeClass('write');   
		
		if (this.value!=this.title){
			$(this).parent('div').addClass('write');
		}

		if (this.value==''){
			this.value=this.title;
			$(this).parent('div').removeClass('write');
		}
	});
	
	$(".send-to-friend").fancybox({
		'titleShow'		: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});
	
	$(".referencie-list li").hover(function(){
		$(this).children('div').css('display','block');
		}, function() { 
		$(this).children('div').css('display','none');
	});

	$('.social-box .icon-Vybralisme').text('Vybrali.sme');	
	
	$('input.order_inp').addClass("idleField");
	$('input.order_inp').focus(function() {
		$(this).removeClass("idleField").addClass("focusField");
		if (this.value == this.title){
			this.value = '';
		}
		if(this.value != this.title){
			this.select();
		}
	});
	$('input.order_inp').blur(function() {
		if (this.value == ''){
			$(this).removeClass("focusField").addClass("idleField");
			this.value = this.title;
		} else if (this.value == this.title){
			$(this).removeClass("focusField").addClass("idleField");
		}
		else{
		}
	});
	
	$('#fix-return').children('input[name="RET"]').attr('value','http://marketing.krea.sk/dopyt-jednoduchy/odoslane/');
	$('#fix-return').attr('action','http://marketing.krea.sk/dopyt-jednoduchy/');
	
	$('#fix-return2').children('input[name="RET"]').attr('value','http://marketing.krea.sk/dopyt-podrobny/odoslane/');
	$('#fix-return2').attr('action','http://marketing.krea.sk/dopyt-podrobny/');
	
	$('#poslat_priatelovi_marketing').children('input[name="RET"]').attr('value','http://marketing.krea.sk/clanky/($(input[name="siteurl"]).attr("value");)/');
	$('#poslat_priatelovi_marketing').attr('action','http://marketing.krea.sk/clanky/');
	
	$('#kontakt-marketing').children('input[name="RET"]').attr('value','http://marketing.krea.sk/kontakt/odoslane/');
	$('#kontakt-marketing').attr('action','http://marketing.krea.sk/kontakt/');
	
	$('#poslat_priatelovi_krea').children('input[name="RET"]').attr('value','($(input[name="siteurl"]).attr("value");)/');
	$('#poslat_priatelovi_krea').attr('action','http://www.krea.sk/');
	$('#poslat_priatelovi_design').children('input[name="RET"]').attr('value','($(input[name="siteurl"]).attr("value");)/');
	$('#poslat_priatelovi_design').attr('action','http://design.krea.sk/');
	$('#poslat_priatelovi_eshopy').children('input[name="RET"]').attr('value','($(input[name="siteurl"]).attr("value");)/');
	$('#poslat_priatelovi_eshopy').attr('action','http://eshopy.krea.sk/');
	$('#poslat_priatelovi_marketing_referencie').children('input[name="RET"]').attr('value','($(input[name="siteurl"]).attr("value");)/');
	$('#poslat_priatelovi_marketing_referencie').attr('action','http://marketing.krea.sk/');
	$('#poslat_priatelovi_branding').children('input[name="RET"]').attr('value','($(input[name="siteurl"]).attr("value");)/');
	$('#poslat_priatelovi_branding').attr('action','http://branding.krea.sk/');			
	
	var $scrollingDiv = $(".position-fixed");
				
	$(window).scroll(function(){			
			var wrapheight = $('.wrap-in').height();
			
			if($(window).scrollTop()>160){
				
				if(wrapheight>($(window).scrollTop() + $('.position-fixed').height())) {
					$scrollingDiv
					.css("marginTop", ($(window).scrollTop() - 150) + "px");
				}
				else{
					$scrollingDiv
					.css("marginTop", wrapheight - $('.position-fixed').height() - 150 +"px");
					//alert('ddd');
				}
			} 
			else {
				$scrollingDiv
				.css("marginTop", "0px");
			}
			//alert(wrapheight);
	});

		
});

function validTarget(handler) {
    $(handler).click(function(){
        window.open(this.href);
        return false;
    });
}

function valideForm(handler) {
	/*jQuery.validator.addMethod("password", function( value, element ) {
		var result = this.optional(element) || value.length >= 6 && /\d/.test(value) && /[a-z]/i.test(value);
		if (!result) {
			element.value = "";
			var validator = this;
			setTimeout(function() {
				validator.blockFocusCleanup = true;
				element.focus();
				validator.blockFocusCleanup = false;
			}, 1);
		}
		return result;
	}, "Your password must be at least 6 characters long and contain at least one number and one character.");*/
	
	// a custom method making the default value for companyurl ("http://") invalid, without displaying the "invalid url" message
	jQuery.validator.addMethod("defaultInvalid", function(value, element) {
		return value != element.defaultValue;
	}, "");
	
	/*jQuery.validator.addMethod("billingRequired", function(value, element) {
		if ($("#bill_to_co").is(":checked"))
			return $(element).parents(".subTable").length;
		return !this.optional(element);
	}, "");*/
	
	jQuery.validator.messages.required = "";
	$(handler).validate({
		invalidHandler: function(e, validator) {
			/*var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted below'
					: 'You missed ' + errors + ' fields.  They have been highlighted below';
				$("div.error span").html(message);
				$("div.error").show();
			} else {$("div.error").hide();}*/
		},
		onkeyup: false,
		submitHandler: function() {
			form.submit();
			/*$("div.error").hide();
			alert("submit! use link below to go to the other step");*/
		}/*,
		messages: {
			password2: {
				required: " ",
		  		equalTo: "Please enter the same password as above"	
			},
			email: {
				required: " ",
				email: "Please enter a valid email address, example: you@yourdomain.com",
				remote: jQuery.validator.format("{0} is already taken, please enter a different address.")	
			}
		},
		debug:true*/
	});
}
