/*
 * jQuery hashchange event - v1.3 - 7/21/2010
 * http://benalman.com/projects/jquery-hashchange-plugin/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,e,b){var c="hashchange",h=document,f,g=$.event.special,i=h.documentMode,d="on"+c in e&&(i===b||i>7);function a(j){j=j||location.href;return"#"+j.replace(/^[^#]*#?(.*)$/,"$1")}$.fn[c]=function(j){return j?this.bind(c,j):this.trigger(c)};$.fn[c].delay=50;g[c]=$.extend(g[c],{setup:function(){if(d){return false}$(f.start)},teardown:function(){if(d){return false}$(f.stop)}});f=(function(){var j={},p,m=a(),k=function(q){return q},l=k,o=k;j.start=function(){p||n()};j.stop=function(){p&&clearTimeout(p);p=b};function n(){var r=a(),q=o(m);if(r!==m){l(m=r,q);$(e).trigger(c)}else{if(q!==m){location.href=location.href.replace(/#.*/,"")+q}}p=setTimeout(n,$.fn[c].delay)}$.browser.msie&&!d&&(function(){var q,r;j.start=function(){if(!q){r=$.fn[c].src;r=r&&r+a();q=$('<iframe tabindex="-1" title="empty"/>').hide().one("load",function(){r||l(a());n()}).attr("src",r||"javascript:0").insertAfter("body")[0].contentWindow;h.onpropertychange=function(){try{if(event.propertyName==="title"){q.document.title=h.title}}catch(s){}}}};j.stop=k;o=function(){return a(q.location.href)};l=function(v,s){var u=q.document,t=$.fn[c].domain;if(v!==s){u.title=h.title;u.open();t&&u.write('<script>document.domain="'+t+'"<\/script>');u.close();q.location.hash=v}}})();return j})()})(jQuery,this);
/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 * Copyright (c) 2008 George McGinley Smith and (c) 2001 Robert Penner
 * Open source under the BSD License. (http://www.opensource.org/licenses/bsd-license.php)
*/
jQuery.easing.jswing=jQuery.easing.swing;jQuery.extend(jQuery.easing,{def:"easeOutQuad",swing:function(e,f,a,h,g){return jQuery.easing[jQuery.easing.def](e,f,a,h,g)},easeInQuad:function(e,f,a,h,g){return h*(f/=g)*f+a},easeOutQuad:function(e,f,a,h,g){return -h*(f/=g)*(f-2)+a},easeInOutQuad:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f+a}return -h/2*((--f)*(f-2)-1)+a},easeInCubic:function(e,f,a,h,g){return h*(f/=g)*f*f+a},easeOutCubic:function(e,f,a,h,g){return h*((f=f/g-1)*f*f+1)+a},easeInOutCubic:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f*f+a}return h/2*((f-=2)*f*f+2)+a},easeInQuart:function(e,f,a,h,g){return h*(f/=g)*f*f*f+a},easeOutQuart:function(e,f,a,h,g){return -h*((f=f/g-1)*f*f*f-1)+a},easeInOutQuart:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f*f*f+a}return -h/2*((f-=2)*f*f*f-2)+a},easeInQuint:function(e,f,a,h,g){return h*(f/=g)*f*f*f*f+a},easeOutQuint:function(e,f,a,h,g){return h*((f=f/g-1)*f*f*f*f+1)+a},easeInOutQuint:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f*f*f*f+a}return h/2*((f-=2)*f*f*f*f+2)+a},easeInSine:function(e,f,a,h,g){return -h*Math.cos(f/g*(Math.PI/2))+h+a},easeOutSine:function(e,f,a,h,g){return h*Math.sin(f/g*(Math.PI/2))+a},easeInOutSine:function(e,f,a,h,g){return -h/2*(Math.cos(Math.PI*f/g)-1)+a},easeInExpo:function(e,f,a,h,g){return(f==0)?a:h*Math.pow(2,10*(f/g-1))+a},easeOutExpo:function(e,f,a,h,g){return(f==g)?a+h:h*(-Math.pow(2,-10*f/g)+1)+a},easeInOutExpo:function(e,f,a,h,g){if(f==0){return a}if(f==g){return a+h}if((f/=g/2)<1){return h/2*Math.pow(2,10*(f-1))+a}return h/2*(-Math.pow(2,-10*--f)+2)+a},easeInCirc:function(e,f,a,h,g){return -h*(Math.sqrt(1-(f/=g)*f)-1)+a},easeOutCirc:function(e,f,a,h,g){return h*Math.sqrt(1-(f=f/g-1)*f)+a},easeInOutCirc:function(e,f,a,h,g){if((f/=g/2)<1){return -h/2*(Math.sqrt(1-f*f)-1)+a}return h/2*(Math.sqrt(1-(f-=2)*f)+1)+a},easeInElastic:function(f,h,e,l,k){var i=1.70158;var j=0;var g=l;if(h==0){return e}if((h/=k)==1){return e+l}if(!j){j=k*0.3}if(g<Math.abs(l)){g=l;var i=j/4}else{var i=j/(2*Math.PI)*Math.asin(l/g)}return -(g*Math.pow(2,10*(h-=1))*Math.sin((h*k-i)*(2*Math.PI)/j))+e},easeOutElastic:function(f,h,e,l,k){var i=1.70158;var j=0;var g=l;if(h==0){return e}if((h/=k)==1){return e+l}if(!j){j=k*0.3}if(g<Math.abs(l)){g=l;var i=j/4}else{var i=j/(2*Math.PI)*Math.asin(l/g)}return g*Math.pow(2,-10*h)*Math.sin((h*k-i)*(2*Math.PI)/j)+l+e},easeInOutElastic:function(f,h,e,l,k){var i=1.70158;var j=0;var g=l;if(h==0){return e}if((h/=k/2)==2){return e+l}if(!j){j=k*(0.3*1.5)}if(g<Math.abs(l)){g=l;var i=j/4}else{var i=j/(2*Math.PI)*Math.asin(l/g)}if(h<1){return -0.5*(g*Math.pow(2,10*(h-=1))*Math.sin((h*k-i)*(2*Math.PI)/j))+e}return g*Math.pow(2,-10*(h-=1))*Math.sin((h*k-i)*(2*Math.PI)/j)*0.5+l+e},easeInBack:function(e,f,a,i,h,g){if(g==undefined){g=1.70158}return i*(f/=h)*f*((g+1)*f-g)+a},easeOutBack:function(e,f,a,i,h,g){if(g==undefined){g=1.70158}return i*((f=f/h-1)*f*((g+1)*f+g)+1)+a},easeInOutBack:function(e,f,a,i,h,g){if(g==undefined){g=1.70158}if((f/=h/2)<1){return i/2*(f*f*(((g*=(1.525))+1)*f-g))+a}return i/2*((f-=2)*f*(((g*=(1.525))+1)*f+g)+2)+a},easeInBounce:function(e,f,a,h,g){return h-jQuery.easing.easeOutBounce(e,g-f,0,h,g)+a},easeOutBounce:function(e,f,a,h,g){if((f/=g)<(1/2.75)){return h*(7.5625*f*f)+a}else{if(f<(2/2.75)){return h*(7.5625*(f-=(1.5/2.75))*f+0.75)+a}else{if(f<(2.5/2.75)){return h*(7.5625*(f-=(2.25/2.75))*f+0.9375)+a}else{return h*(7.5625*(f-=(2.625/2.75))*f+0.984375)+a}}}},easeInOutBounce:function(e,f,a,h,g){if(f<g/2){return jQuery.easing.easeInBounce(e,f*2,0,h,g)*0.5+a}return jQuery.easing.easeOutBounce(e,f*2-g,0,h,g)*0.5+h*0.5+a}});


function log(msg){
	try{
		console.log(msg);
	}catch(e){}
}

// start - ktos wszedl na strone, a nie zmienia kolejny raz
function filter_tags(start){
	var href = location.hash.split("#")[1];
	spd = 1000;
	spd_in = 1100;
	
	var $items = $("#filterable li");
	if(start ){
		$items.animate({opacity:0},0)
		if(href){
			$items = $items.filter("."+href)
		}
		$items.each(function(){
			var $i = $(this);
			setTimeout(function(){$i.rollIn(Math.random()*500 + 900)}, Math.round()*300)
		})
	}else{
		if (href){	// jest jakis hash
			$items.not("."+href).rollOut(spd);
			$items.filter("."+href).rollIn(spd_in);
		}else{		// kolejny klik bez hasha
			$items.rollIn(600);
		}
	}
}

$(function(){
	if($.browser.safari) $("body").addClass("safari");
	// animacja zwijania elementu portfolio -  Out i In
	$.fn.rollOut = function(speed){
		speed = speed || 1400;
		$(this).stop().animate({ width: 0, marginRight: '0px', opacity: 0, delay: Math.random()*600 }, speed, "easeOutCubic");
	}
	$.fn.rollIn = function(speed){
		speed = speed || 1300;
		$(this).stop().animate({ width: 230, marginRight: '10px', opacity: 1 }, speed, "easeInCubic");
	}
	
	// item hover
	function node_over(){
		var t = $(this);
		var top = $(".top", t).stop().animate({'top':'0px', 'opacity':1},300, "easeOutCubic")
		var bottom = $(".bottom", t).stop().animate({'bottom':'0px', 'opacity':1},300, "easeOutCubic")
		var glyph = $(".glyph", t).stop().animate({'opacity':1},400, "easeInCubic")
	}
	
	// item hout
	function node_out(){
		var t = $(this);
		var _top = $(".top", t);
		var _th = _top.outerHeight();
		_top.stop().animate({'top':'-'+_th+'px', 'opacity':0},300, "easeInCubic")
		var _bottom = $(".bottom", t);
		var _bh = _bottom.outerHeight();
		_bottom.stop().animate({'bottom':'-'+_bh+'px', 'opacity':0},300, "easeOutCubic")
		var glyph = $(".glyph", t).stop().animate({'opacity':0},300, "easeOutCubic")
	}
	
	// item hover event
	$("#filterable li").hover(node_over, node_out);
	$("#filterable li .glyph").animate({'opacity':0},0)
	
	// zmiana hasha
	$(window).hashchange(function(){
		filter_tags();	
	})
	filter_tags(true);
	
	
	
	// photo
	var images = $(".content #slider li");
	images.filter(":eq(0)").css("z-index",120).addClass("current").siblings(".file-type-image").fadeOut(0);
	
	images.filter(":eq(0) img").load(function(){
		var _h = images.height();
		if(_h > $("#slider").height()) $("#slider").css("min-height", images.height());
	})
	$("#img-next").click(function(e){
		e.preventDefault()
		var current = images.filter(".current");
		var next = current.next();
		if (next.length==0) next = images.filter(":first-child");
		switch_image(current, next)
	});
	$("#img-prev").click(function(e){
		e.preventDefault()
		var current = images.filter(".current");
		var next = current.prev();
		if (next.length==0) next = images.filter(":last-child");
		switch_image(current, next)
	});
	
	
	// clients slider
	var $clients = $("#clients-slider");
	if ($clients.length > 0){
		function clients_switch(cur, next){
			cur.removeClass("active").fadeOut(500, function(){next.fadeIn(500).addClass("active")});
		}
		var $c = $clients.children();
		$clients.children(":eq(0)").addClass("active").siblings().hide();
		setInterval(function(){
			var cur = $clients.children(".active")
			var next = cur.next();
			if (next.length == 0){
				next = $clients.children(":eq(0)");
			}
			clients_switch(cur, next);
		}, 3300)
	}
	/*
	 * oembed
	 */
	$(".oembed").oembed(null, {
		maxWidth: 630, 
		maxHeight: 450,
		greedy: true,
		vimeo: {player_id: "video_player", api:1, title:false, byline:false, portrait:false, color: "ffffff", fullscreen:1} ,
		beforeEmbed: function(data){
			$(this).parent().data("oembed_provider", data.provider_name);
		}
	})
})


function switch_image(current, next){
	// vimeo stop
	if (current.data("oembed_provider") == "Vimeo"){
		vimeoMessage("pause", null, current.find('iframe').get(0))
	}
	
	current.removeClass("current");
	var z = parseInt(current.css("z-index")) || 0
	if(z==0) current.css("z-index",z)
	next.fadeOut(0)
		.css("z-index", z+1)
		.fadeIn(500).addClass("current");
	current.fadeOut(400)
	
	var cur = next.index()+1;
	var total = $(".content #slider li").length
	$img_count = $("#img-count").text(cur + " / " + total)
	
}


/*
 * vimeo
 */
function vimeoMessage(method, params, target) {
    if (!target.contentWindow.postMessage) {
        return false;
    }

    var url = target.getAttribute('src').split('?')[0],
        data = JSON.stringify({
            method: method,
            value: params
        });

    target.contentWindow.postMessage(data, url);
}
function init_placeholders(){
	if(!Modernizr.input.placeholder) {
		$("input").each( function() {
			if($(this).val()=="" && $(this).attr("placeholder")!="") {
				$(this).val($(this).attr("placeholder"));
				$(this).focus( function() {
					if($(this).val()==$(this).attr("placeholder"))
						$(this).val("");
				});
				$(this).blur( function() {
					if($(this).val()=="")
						$(this).val($(this).attr("placeholder"));
				});
			}
		});
	}
}
var page_contact = {
	init: function(){
		$(".form-row label").each(function(){
			var _t = $(this);
			_t.siblings("#" + _t.attr("for")).attr("placeholder", _t.text())
		})
		init_placeholders();
	}
}

