/*$.tabs = function(containerId, content) {
    var ON_CLASS = 'selected';
    var id = $(containerId);
	
    id.find(content).each(function(){
	var parent = $(this).parent();
	if ($(parent).hasClass(ON_CLASS) == false) {
	    $(this).hide();
	}
    });

    id.find('a.tab').hover(function() {
		var target = $(this).attr('rel');
		var parent = $(target).parent();
		$.ChangeTabs(id, content, parent, target, ON_CLASS);
    });
	
    setInterval('socialslide()', 1000);
};*/

var tabtimer = null;
$.tabs = function(containerId, content) {
    var ON_CLASS = 'selected';
    var id = $(containerId);

    id.find(content).each(function(){
	var parent = $(this).parent();
	if ($(parent).hasClass(ON_CLASS) == false) {
	    $(this).hide();
	}
    });
	
	tabtimer = setInterval('socialslide()', 3600);
    id.find('a.tab').hover(function(){
		var target = $(this).attr('rel');
		var parent = $(target).parent();
		clearTimeout(tabtimer);
		tabtimer = null;
		$.ChangeTabs(id, content, parent, target, ON_CLASS);
    }, function(){
		tabtimer = setInterval('socialslide()', 3600);
	});
};

$.ChangeTabs = function(id, content, parent, target, active_class) {
    if (parent.hasClass(active_class) == false) {
		id.find(content).fadeOut('fast');
		id.find('.'+active_class).removeClass(active_class);
		$(parent).toggleClass(active_class);
		$(target).fadeIn();
    }
    return;
};

function socialslide() {
    var active = $('#services').find('.selected');
    var thenext = $(active).next();
	var target = $(thenext).find('.tab').attr('rel');

	if ($(active).is(':last-child')) {
		target = $('#services').find('.service:first').find('.tab').attr('rel');
		window.console.log("Ultima pestaņa: %d", target);
	}

    $.ChangeTabs($('#services'), '.service-content', $(target).parent(), target, 'selected');
};

(function($){
    $.fn.toggleTarget = function(){
		if ($(this).is('a')) {
			$(this).click(function(){
				var target = $($(this).attr('href'));
				target.fadeToggle('fast');
				return false;
			});
		} else if ($(this).is('input')) {
			$(this).change(function(){
				var target = $('#'+$(this).attr('rel'));
				target.fadeToggle('fast');
			}).trigger('change');
		}
    }
})(jQuery);

(function($){
    $.fn.fadeToggle = function(s, fn){
		return (this.is(":visible"))
			? this.fadeOut(s, fn)
			: this.fadeIn(s, fn);
    }
})(jQuery);

/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}(';(5(d){6 k=d.D=5(a,i,e){d(L).D(a,i,e)};k.O={H:\'1f\',Q:S(d.G.1g)>=1.3?0:1};k.L=5(a){7 d(L).F()};d.G.F=5(){7 8.1e(5(){6 a=8,i=!a.V||d.1d(a.V.I(),[\'1a\',\'#10\',\'C\',\'v\'])!=-1;4(!i)7 a;6 e=(a.1b||a).10||a.M||a;7 d.1c.1h||e.1i==\'19\'?e.v:e.X})};d.G.D=5(n,j,b){4(z j==\'J\'){b=j;j=0}4(z b==\'5\')b={R:b};4(n==\'w\')n=1n;b=d.1o({},k.O,b);j=j||b.1m||b.Q;b.A=b.A&&b.H.1l>1;4(b.A)j/=2;b.9=p(b.9);b.y=p(b.y);7 8.F().P(5(){6 q=8,r=d(q),f=n,s,g={},u=r.B(\'C,v\');1j(z f){E\'1k\':E\'1p\':4(/^([+-]=)?\\d+(\\.\\d+)?(16|%)?$/.T(f)){f=p(f);13}f=d(f,8);E\'J\':4(f.B||f.15)s=(f=d(f)).9()}d.P(b.H.14(\'\'),5(a,i){6 e=i==\'x\'?\'18\':\'17\',h=e.I(),c=\'Z\'+e,l=q[c],m=k.w(q,i);4(s){g[c]=s[h]+(u?0:l-r.9()[h]);4(b.N){g[c]-=U(f.12(\'N\'+e))||0;g[c]-=U(f.12(\'1s\'+e+\'11\'))||0}g[c]+=b.9[h]||0;4(b.y[h])g[c]+=f[i==\'x\'?\'1D\':\'1C\']()*b.y[h]}1F{6 o=f[h];g[c]=o.W&&o.W(-1)==\'%\'?S(o)/1q*m:o}4(/^\\d+$/.T(g[c]))g[c]=g[c]<=0?0:K.Y(g[c],m);4(!a&&b.A){4(l!=g[c])t(b.1A);1u g[c]}});t(b.R);5 t(a){r.1t(g,j,b.1B,a&&5(){a.1r(8,n,b)})}}).1v()};k.w=5(a,i){6 e=i==\'x\'?\'11\':\'1w\',h=\'Z\'+e;4(!d(a).B(\'C,v\'))7 a[h]-d(a)[e.I()]();6 c=\'1z\'+e,l=a.M.X,m=a.M.v;7 K.w(l[h],m[h])-K.Y(l[c],m[c])};5 p(a){7 z a==\'J\'?a:{1y:a,1x:a}}})(1E);',62,104,'||||if|function|var|return|this|offset||||||||||||||||||||||body|max||over|typeof|queue|is|html|scrollTo|case|_scrollable|fn|axis|toLowerCase|object|Math|window|ownerDocument|margin|defaults|each|duration|onAfter|parseFloat|test|parseInt|nodeName|slice|documentElement|min|scroll|document|Width|css|break|split|style|px|Top|Left|BackCompat|iframe|contentWindow|browser|inArray|map|xy|jquery|safari|compatMode|switch|number|length|speed|9e9|extend|string|100|call|border|animate|delete|end|Height|left|top|client|onAfterFirst|easing|height|width|jQuery|else'.split('|'),0,{}))

/**
 * jQuery.LocalScroll - Animated scrolling navigation, using anchors.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 3/11/2009
 * @author Ariel Flesler
 * @version 1.2.7
 */
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}(';(2($){6 l=m.s.A(/#.*/,\'\');6 g=$.t=2(a){$(\'G\').t(a)};g.p={8:U,17:\'y\',r:\'N\',n:o,7:q,v:o};g.5=2(a){3(m.5){a=$.B({},g.p,a);a.5=x;3(a.v){6 e=a.8;H a.8;$(a.7).F(0,a);a.8=e}i(0,m,a)}};$.O.t=2(b){b=$.B({},g.p,b);9 b.Q?4.w(b.r,2(a){6 e=$([a.7,a.7.S]).c(d)[0];3(e)i(a,e,b)}):4.T(\'a,V\').c(d).w(b.r,2(a){i(a,4,b)}).D().D();2 d(){9!!4.s&&!!4.5&&4.s.A(4.5,\'\')==l&&(!b.c||$(4).C(b.c))}};2 i(a,e,b){6 d=e.5.M(1),f=z.X(d)||z.1b(d)[0];3(!f)9;3(a)a.18();6 h=$(b.7);3(b.1a&&h.C(\':15\')||b.E&&b.E.Z(b,a,f,h)===x)9;3(b.n)h.n(o);3(b.5){6 j=f.u==d?\'u\':\'K\',k=$(\'<a> </a>\').W(j,d).13({14:\'12\',11:$(q).Y(),16:$(q).19()});f[j]=\'\';$(\'G\').L(k);m=e.5;k.I();f[j]=d}h.F(f,b).R(\'P.J\',[f])}})(10);',62,74,'||function|if|this|hash|var|target|duration|return|||filter||||||||||location|stop|true|defaults|window|event|href|localScroll|id|reset|bind|false||document|replace|extend|is|end|onBefore|scrollTo|body|delete|remove|serialScroll|name|prepend|slice|click|fn|notify|lazy|trigger|parentNode|find|1e3|area|attr|getElementById|scrollTop|call|jQuery|top|absolute|css|position|animated|left|axis|preventDefault|scrollLeft|lock|getElementsByName'.split('|'),0,{}))

// code for making inline labels which then move above form inputs when the inputs have content
eval(function(p,a,c,k,e,d){e=function(c){return c.toString(36)};if(!''.replace(/^/,String)){while(c--){d[c.toString(a)]=k[c]||c.toString(a)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('j 0={5:e,n:3(2){$(2).o(3(){0.d(8);$(8).q(3(){$(\'#\'+$(8).1(\'7\')).5()})});$(\'.f\').5(3(){0.5=$(8);0.c($(8))}).p(3(){0.5=e;0.d($(\'2[7=\'+$(8).1(\'9\')+\']\'))})},d:3(2){j 6=$(\'#\'+$(2).1(\'7\'));b(!6){r}b(0.5!==e&&0.5.1(\'9\')==6.1(\'9\')){0.c(6)}h b(6.u()===\'\'){0.l(6)}h{0.c(6)}},c:3(4){$(4).a(\'f\');$(\'2[7=\'+$(4).1(\'9\')+\']\').g(\'k\').g(\'m\').a(\'i\')},l:3(4){$(4).a(\'f\');b($.v.s&&$(4).1(\'t\')){$(\'2[7=\'+$(4).1(\'9\')+\']\').a(\'m\')}h{$(\'2[7=\'+$(4).1(\'9\')+\']\').a(\'k\').g(\'i\')}}};',32,32,'labeler|attr|label|function|el|focus|target|for|this|id|addClass|if|aboveLabel|check|null|islabeled|removeClass|else|abovecontent|var|overcontent|overLabel|hidden|init|each|blur|click|return|safari|placeholder|val|browser'.split('|'),0,{}))

function isValidEmail(address) {
	var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
	return pattern.test(address);
}

function isValidUrl(address) {
	var pattern = new RegExp(/^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i);
	return pattern.test(address);
}

(function($){
    $.fn.ajaxify = function(add_msg, redirect) {
		var form = $(this);
		var ret = false;

		if (add_msg)
			form.before('<div class="msg notice">'+SocialSnack.sending+'</div>');

		var data = $(this).serialize();
		$(':input', form).attr('disabled', 'disabled');
		form.find('.ajaxed').val('true');
		$.ajax({
			url: form.attr('action'),
			type: 'post',
			data: data,
			dataType: 'json',
			success: function(data) {
				$(':input', form).removeAttr('disabled');
				if (data.status == 'success') {
					if (add_msg) {
						form.prev('.msg').remove();
						form.before('<div class="msg success">'+SocialSnack.success+'</div>');
					}
					$(':input', form).not(':button, :submit, :reset, :hidden')
						.val('')
						.removeAttr('checked')
						.removeAttr('selected');
					
					if (redirect) {
						window.location.replace(redirect);
					}
					ret = true;
				} else {
					if (add_msg) {
						form.prev('.msg').remove();
						form.before('<div class="msg error">'+SocialSnack.error+'</div>');
					}
					ret = false;
				}
			}
		});
		return ret;
    }
})(jQuery);

(function($){
    $.fn.isValid = function(){
		var ret = true;
		$(this).find('.required').each(function(){
			var element = $(this);
			if ((element.val() == '')
				|| (element.hasClass('email') && !isValidEmail(element.val()))
				|| (element.hasClass('url') && !isValidUrl(element.val()))) {
				element.addClass("empty");
				ret = false;
			} else {
			}
		});

		return ret;
    }
})(jQuery);

(function($){
    $.fn.addMessaje = function(title, messaje, theme, show_on){
		var target = this;

		if (!theme) {
			theme = 'cream';
		}

		if (show_on) {
			show_on = false;
		}

		$(target).qtip({
			content: {
				title: {
					text: title,
					button: ''
				},
				text: messaje
			},
			position: {
				target: $(target),
				adjust: {
					screen: true,
					scroll: false
				},
				corner: {
					target: 'topMiddle',
					tooltip: 'bottomMiddle'
				}
				//corner: 'bottomMiddle' // ...at the center of the viewport
			},
			show: {
				solo: true,
				delay: 0,
				when: { target: false }
			},
			hide: {
				delay: 1000,
				when: { target: false }
			},
			style: {
				width: {
					max: 350
				},
				padding: '10px',
				border: {
					width: 5,
					radius: 5
				},
				tip: { // Now an object instead of a string
					corner: 'bottomMiddle',
					size: {
						x: 20,
						y: 8
					}
				},
				name: theme
			}/*,
			api: {
				beforeShow: function() {
					// Fade in the modal "blanket" using the defined show speed
					$('#qtip-blanket').fadeIn(this.options.show.effect.length);
				},
				beforeHide: function(){
					// Fade out the modal "blanket" using the defined hide speed
					$('#qtip-blanket').fadeOut(this.options.hide.effect.length);
				}
			}*/
		});
    }
})(jQuery);

$(document).ready(function(){
    /* Tabs */
	if ($('body').hasClass('home'))
	    $.tabs("#services", 'div.service-content');

    /* Blockquotes */
    $('blockquote').prepend('<span class="quote open">&ldquo;</span>');
    $('blockquote p').append('<span class="quote close">&rdquo;</span>');
    $('q').prepend('<span class="quote open">&ldquo;</span>');
    $('q').append('<span class="quote close">&rdquo;</span>');

	$.easing.elasout = function(x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	};

	$('body.home #navigation a').click(function(e){
		e.preventDefault();
		var target = 'div#'+$(this).attr('rel');
		$.scrollTo(target, 3000);
		
	});

	/* Tooltips */
	$('.tip[title]').qtip({
		style: {
			background: '#ECE9D3',
		    border: {
				width: 1,
				radius: 5,
				color: '#9b9671'
			},
			tip: { // Now an object instead of a string
				corner: 'topLeft', // We declare our corner within the object using the corner sub-option
				color: '#9b9671',
				size: {
					x: 10, // Be careful that the x and y values refer to coordinates on screen, not height or width.
					y : 8 // Depending on which corner your tooltip is at, x and y could mean either height or width!
				}
			},
			name: 'light' // Inherit from preset style
		},
		position: {
			target: 'mouse',
			adjust: {
				screen: true,
				scroll: false
			},
			corner: {
				target: 'bottomRight',
				tooltip: 'topLeft'
			}
		}
    });

    /* Some Tweeks */
    $('.toggle').toggleTarget();
    $('.scroll').localScroll();
    $('#random-question a').click(function(){
		$('#random-question').fadeToggle('fast');
	});
	labeler.init('#scontact label');

	$('.work-screens').each(function(){
		var parent = $(this).parent();
		var slider = $(this);
		slider.cycle({
			fx: 'turnDown',
			speed: 'fast',
			timeout: 0,
			pager: parent.find('.work-screens-nav')
		});
	});

    Cufon('.dinn');
    Cufon.replace('.dinn-shadow, .entry-title', {
		textShadow: '#fff 1px 1px'
    });

	$("a.fancy, .image-item a").fancybox({
		'overlayOpacity': .5,
		'overlayColor': '#000'
	});

	$(".in-steps").formToWizard({submitButton:'#buttons'});
	$('#contact-form, #contact-div form, #bugs-form, .ajaxed').submit(function(e){
		var form = $(this);

		form.prev('.msg').remove();
		$(':input', form).removeClass('empty');

		if ($(this).isValid()) {
			if (form.attr('id') == 'quote') {
				var ret = form.ajaxify(false, SocialSnack.tnxpage);
			} else {
				form.ajaxify(true);
			}
		} else {
			form.before('<div class="msg error">'+SocialSnack.incomplete+'</div>');
			$(form).find('.empty').stop()
				.animate({ left: "-10px" }, 100).animate({ left: "10px" }, 100)
				.animate({ left: "-10px" }, 100).animate({ left: "10px" }, 100)
				.animate({ left: "0px" }, 0);
			return false;
		}
		e.preventDefault();
		return true;
	});

	$('form.simple-ajax').submit(function(e){
		e.preventDefault();
		var form = $(this);
		var value = form.children('textarea').val();
		if (value != '') {
			form.ajaxify();
			$(':input', form).not(':button, :submit, :reset, :hidden')
				.val('')
				.removeAttr('checked')
				.removeAttr('selected');
		}
	});
});