var site = function() {
	this.navLi = $('#nav li').children('ul').hide().end();
	this.init();
};

site.prototype = {
 	init : function() {	this.setMenu();	},
 	// Enables the slidedown menu, and adds support for IE6
 	setMenu : function() {
		$.each(this.navLi, function() {
			if ($(this).children('ul')[0])
			{
				$(this).append('<span />').children('span').addClass('hasChildren')
			}
		});
		
		this.navLi.hover(function() {
			// mouseover
			$(this).find('> ul').stop(true, true).slideDown('slow', 'easeOutCubic');
		}, function() {
			// mouseout
			$(this).find('> ul').stop(true, true).hide(); 		
		});
 	}
}
new site();





$(document).ready(function() {
	
	
	
	
	// animowane logotypy klientów
	$(".client-logo").hover(function() {
		$(this).stop().animate({opacity: 0.7}, 300);		
	}, function() {
		$(this).stop().animate({opacity: 1.0}, 100);
	});
	
	
	
	
	
	
	
	$("#lp-telefon").keydown(function(event) {
        // Allow only backspace and delete
        if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 32 || event.keyCode == 9) {
            // let it happen, don't do anything
        }
        else {
            // Ensure that it is a number and stop the keypress
            if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
                event.preventDefault(); 
            }   
        }
    });

	
	
	
	
	
	
	
	
	
	
	
	
	
	// landing page
	$("#wyslij").click(function() {
		
		if($("#lp-telefon").val() == "" && $("#lp-email").val() == "")
		{
			document.getElementById("alert").innerHTML = 'Aby umozliwic nam kontakt<br />prosimy o podanie <strong>telefonu</strong><br />LUB <strong>adresu email</strong>.';
			$("#alert").stop().fadeIn(300);
		}
		if(!($("#lp-zgoda").is(':checked')))
		{
			$("#alert2").stop().fadeIn(300);
		}
		
		if(($("#lp-telefon").val() != "" || $("#lp-email").val() != "") && $("#lp-zgoda").is(':checked'))
		{
			contactuslanding('http://www.meatnet.pl', 'biuro@meatnet.pl');	
		}
	});
	$("#alert, #alert2").click(function() {
		$(this).fadeOut(300);
	});
	// landing page - portfolio button
	$("#portfolio-button-link").hover(function() {
		$("#portfolio-button").animate({left:470}, 200);	
	}, function() {
		$("#portfolio-button").animate({left:460}, 200);
	});
	
	
	
	
	//formularz kontaktowy
	$("#contact_form_success").hide();
	$("#contact_form_success a").click(function() {
		$("#contact_form_success").hide(500);
		document.contact_form.reset();
	});
	$("#cemail").click(function() {
		$(this).css("color", "#663333");
		$(this).css("text-transform", "lowercase");
	});
	$("#project-form-submit a").click(function() {
		document.form1.submit();									   
	});
	
	
	
	// animowane linki
	$(".animate_link").hover(function() {
		$(this).stop().animate({marginLeft:5}, 300);									   
	}, function() {
		$(this).stop().animate({marginLeft:0}, 300);
	});
	$("#document_footer a").hover(function() {
		$(this).stop().animate({marginLeft:5}, 300);									   
	}, function() {
		$(this).stop().animate({marginLeft:0}, 300);
	});
	$("#sitemap a").hover(function() {
		$(this).stop().animate({marginLeft:5}, 300);									   
	}, function() {
		$(this).stop().animate({marginLeft:0}, 300);
	});
	$("#nav li ul li a").hover(function() {
		$(this).stop().animate({marginLeft:5}, 300);									   
	}, function() {
		$(this).stop().animate({marginLeft:0}, 300);
	});
	$("#short_porfolio a").hover(function() {
		$(this).stop().animate({marginLeft:225}, 300);									   
	}, function() {
		$(this).stop().animate({marginLeft:220}, 300);
	});
	$("#wyslij").hover(function() {
		$(this).stop().animate({marginLeft:316}, 300);									   
	}, function() {
		$(this).stop().animate({marginLeft:310}, 300);
	});
	$("#wyslij2").hover(function() {
		$(this).stop().animate({marginLeft:40}, 300);									   
	}, function() {
		$(this).stop().animate({marginLeft:35}, 300);
	});
	
	
	
	
	
	
	
	
	
	// Short portfolio
	$(".short-porfolio-image-wrap").hover(function() {
		$(this).find(".short-porfolio-image-description").css({opacity: 0.0, visibility: "visible"}).stop().animate({opacity: 1.0}, 300);
		$(this).find(".short-porfolio-image-description-text").stop().animate({left:0}, 500, 'easeOutExpo');
	}, function() {
		$(this).find(".short-porfolio-image-description").css({opacity: 1.0, visibility: "visible"}).stop().animate({opacity: 0.0}, 500, 'easeInExpo');
		$(this).find(".short-porfolio-image-description-text").stop().animate({left:-254}, 500, 'easeInExpo');
	});
	
	
	
	
	
	
	// rozjasnienie portfolio po najechaniu kursorem
	var obj;
	$(".module-picture-list img").hover(function() {
		$(this).stop().animate({opacity:0.8}, "fast");
	}, function() {
		$(this).stop().animate({opacity:1.0}, "fast");
	});
	
	
	
	
	
	
	// clearing form fields values
	jQuery.fn.cleardefault = function() {
		return this.focus(function() {
			if( this.value == this.defaultValue ) {
				this.value = "";
			}
		}).blur(function() {
			if( !this.value.length ) {
				this.value = this.defaultValue;
			}
		});
	};
	$("#contact_form input, #contact_form textarea").cleardefault();
	
	
	
	
	
	
	
	// wyszukiwanie po ENTER
	$('#search').keyup(function(e) {
		//alert(e.keyCode);
		if(e.keyCode == 13) {
			document.location = '?'+document.getElementById('search').value+'/query';
		}
    });
	
	
	
	
	
	
	
	
	// colorbox
	$(".picture_pic").colorbox({rel:'portfolio_pic'});
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
	//Mapa strony
	var map_active = false;
	$("#sitemap_link, .close_site_map").click(function() {
		   if(!map_active)
		   {	   
				$("#sitemap").stop().animate({marginTop:0}, 500);
				map_active = true;
		   }
		   else
		   {
				$("#sitemap").stop().animate({marginTop:-300}, 500);
				map_active = false;
		   }
	});
	
	
	
	
	
	
	
	// FORMULARZ WYCENY - AKORDEON
	//Set default open/close settings
	$('.acc_container').hide(); //Hide/close all containers
	$('.acc_trigger:first').addClass('active').next().show(); //Add "active" class to first trigger, then show/open the immediate next container
	
	//On Click
	$('.acc_trigger').click(function(){
		if( $(this).next().is(':hidden') ) { //If immediate next container is closed...
			$('.acc_trigger').removeClass('active').next().slideUp(); //Remove all "active" state and slide up the immediate next container
			$(this).toggleClass('active').next().slideDown(); //Add "active" state to clicked trigger and slide down the immediate next container
		}
		return false; //Prevent the browser jump to the link anchor
	});
	
	
});




// AJAX
var tar;
var s_url;
function changelanguage(lang, target, site_url)
{
	s_url = site_url;
	tar = target;
	
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.responseText != "")
			setTimeout("reloadAJAX(1)", 200);
		else
			setTimeout("reloadAJAX(2)", 200);
	}
	
	var url = site_url + "/ajax/changelanguage.php?lang="+lang+"&target="+target+"&site_url="+site_url+"&key="+Math.random();
	xmlhttp.open("GET",url,true);
	xmlhttp.send();	
}

function reloadAJAX(target)
{
	if(target == 1)
		document.location = tar;
	else if(target == 2)
		document.location = s_url;
}

function contactus(site_url, site_email)
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.responseText == "ok")
			$("#contact_form_success").show(500);
		else if(xmlhttp.responseText == "email")
		{
			$("#cemail").css("color", "red");
			$("#cemail").css("text-transform", "uppercase");
		}
	}
	
	var cname = document.contact_form.cname.value;
	var cemail = document.contact_form.cemail.value;
	var cmessage = document.contact_form.cmessage.value;
	
	var url = site_url + "/ajax/contactus.php?cname="+cname+"&cemail="+cemail+"&cmessage="+cmessage+"&site_email="+site_email+"&key="+Math.random();
	xmlhttp.open("GET",url,true);
	xmlhttp.send();	
}


