/**
 * 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: 3/9/2009
 * @author Ariel Flesler
 * @version 1.4.1
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function($){var m=$.scrollTo=function(b,h,f){$(window).scrollTo(b,h,f)};m.defaults={axis:'xy',duration:parseFloat($.fn.jquery)>=1.3?0:1};m.window=function(b){return $(window).scrollable()};$.fn.scrollable=function(){return this.map(function(){var b=this,h=!b.nodeName||$.inArray(b.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!h)return b;var f=(b.contentWindow||b).document||b.ownerDocument||b;return $.browser.safari||f.compatMode=='BackCompat'?f.body:f.documentElement})};$.fn.scrollTo=function(l,j,a){if(typeof j=='object'){a=j;j=0}if(typeof a=='function')a={onAfter:a};if(l=='max')l=9e9;a=$.extend({},m.defaults,a);j=j||a.speed||a.duration;a.queue=a.queue&&a.axis.length>1;if(a.queue)j/=2;a.offset=n(a.offset);a.over=n(a.over);return this.scrollable().each(function(){var k=this,o=$(k),d=l,p,g={},q=o.is('html,body');switch(typeof d){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px)?$/.test(d)){d=n(d);break}d=$(d,this);case'object':if(d.is||d.style)p=(d=$(d)).offset()}$.each(a.axis.split(''),function(b,h){var f=h=='x'?'Left':'Top',i=f.toLowerCase(),c='scroll'+f,r=k[c],s=h=='x'?'Width':'Height';if(p){g[c]=p[i]+(q?0:r-o.offset()[i]);if(a.margin){g[c]-=parseInt(d.css('margin'+f))||0;g[c]-=parseInt(d.css('border'+f+'Width'))||0}g[c]+=a.offset[i]||0;if(a.over[i])g[c]+=d[s.toLowerCase()]()*a.over[i]}else g[c]=d[i];if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],u(s));if(!b&&a.queue){if(r!=g[c])t(a.onAfterFirst);delete g[c]}});t(a.onAfter);function t(b){o.animate(g,j,a.easing,b&&function(){b.call(this,l,a)})};function u(b){var h='scroll'+b;if(!q)return k[h];var f='client'+b,i=k.ownerDocument.documentElement,c=k.ownerDocument.body;return Math.max(i[h],c[h])-Math.min(i[f],c[f])}}).end()};function n(b){return typeof b=='object'?b:{top:b,left:b}}})(jQuery);



/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);



// equal height divs
(function($) {
	$.fn.equalHeights = function(o) {
		var maxHeight = 0;
		var maxElement = null;
	
		$(this).each(function(i) {
			if (($(this).height() + parseInt($(this).css("padding-bottom")) + parseInt($(this).css("padding-top"))) > maxHeight) {
				maxHeight = $(this).height() + parseInt($(this).css("padding-top")) + parseInt($(this).css("padding-bottom"));
				maxElement = $(this);
			}
		});
	
		$(this).not($(maxElement)).each(function() {
			$(this).height(maxHeight - parseInt($(this).css("padding-top")) - parseInt($(this).css("padding-bottom")));
		});
	};
})(jQuery);

function theRotator() {
	$("li", "#serviceList").css("opacity", 0);
	$("li:first", "#serviceList").css("opacity", 1);
		
	setInterval("rotate()", 5000);
}

function rotate() {	
	var current = ($("li.show", "#serviceList") ? $("li.show", "#serviceList") : $("li:first", "#serviceList"));
	var next = ((current.next().length) ? ((current.next().hasClass("show")) ? $("li:first", "#serviceList") : current.next()) : $("li:first", "#serviceList"));	

	current.animate({opacity: 0}, 2000).removeClass("show");
	next.css("opacity", 0).addClass("show").animate({opacity: 1.0}, 2000);
}



$(document).ready(function() {
	if ($.browser.opera) {
		$.support.opacity = true;
	}

	theRotator();

	// set form field styles on focus and blur
	$("input[type='text'], textarea").focus(function() { // IE doesn't like this on 'select' elements
		$(this).removeClass("idleField").addClass("focusField");
	}).blur(function() {
		$(this).removeClass("focusField").addClass("idleField");
	});

	// default form field values on focus and blur
	$(".defaultCheck").focus(function() {
		if (this.value == this.defaultValue) {
			this.value = "";
		}
	}).blur(function() {
		if ($.trim(this.value) == "") {
			this.value = (this.defaultValue ? this.defaultValue : "");
		}
	});

	// button styles on hover
	$("button").hover(function() {
		$(this).css("background-position", "0 bottom");
	}, function() {
		$(this).css("background-position", "0 top");
	});

	// box styles on hover
	$(".box").hover(function() {
		$(this).addClass("on");
	}, function() {
		$(this).removeClass("on");
	});

	// clients logos carousel
	if ($("#logos", "#clientsBox").length) {
		$("#logos").jCarouselLite({
			auto: 10000,
			speed: 500,
			visible: 6,
			scroll: 3,
			vertical: true,
			circular: true
		});
	}

	// media logos carousel
	if ($("#logos", "body#news").length) {
		$("#logos").jCarouselLite({
			auto: 10000,
			speed: 500,
			visible: 4,
			scroll: 3,
			vertical: true,
			circular: true
		});
	}

	// privacy policy dialog
	if ($("#privacyDialog").length) {
		$("#privacyDialog").dialog({
			autoOpen: false,
			closeOnEscape: true,
			modal: true,
			width: 450,
			height: 220,
			open: function() {
				$(this).parents(".ui-dialog").attr("tabindex", -1)[0].focus(); // remove focus from first tabbable element
			}
		});
	}

	$("a[rel~='privacy']").click(function() {
		$("#privacyDialog").dialog("open");

		return false;
	});

	if ($("#twitter").length) {
		$("#twitter").getTwitter({
			userName: "cosmedia",
			numTweets: 1,
			loaderText: "Loading tweet...",
			slideIn: false,
			slideDuration: 750,
			showHeading: false,
			headingText: "Latest Tweets",
			showProfileLink: false,
			showTimestamp: true,
			scrubList: true,
			targetExternalLinks: false
		});
	}

	// get viewport height
	window.viewportHeight = window.innerHeight ? window.innerHeight : $(window).height();

	// contact link redirects
	$("a[rel~='contact']").click(function() {
		$(document).scrollTo(viewportHeight, 500, { onAfter:function() {
			$("#name2").focus();
			$("h3 > a", "#getintouch").stop().animate({backgroundColor: "#ac0007"}, 500);
			$("p.intro", "#getintouch").stop().animate({color: "#333"}, 500);
		} });

		return false;
	});

	// add comment redirect
	/*$("a[rel~='respond']").click(function() {
		$(document).scrollTo("#respond", 500, { onAfter:function() {
			$("#author").focus();
			$("h3 > a", "#respond").stop().animate({backgroundColor: "#ac0007"}, 500);
			$("p.intro", "#respond").stop().animate({color: "#333"}, 500);
		} });

		return false;
	});*/

	// share link
	/*$("a:first", "li.shareLink").click(function() {
		$(this).next().toggle();
		$(this).blur();
		return false;
	});*/

	// equal column heights on home page
	if ($("body#home").length) {
		$("#servicesBox, #projectsBox, #clientsBox").equalHeights();
	}

	// preload images
	var image1 = $("<img />").attr("src", "/img/global/nav/glow_red.png");
	var image2 = $("<img />").attr("src", "/img/global/nav/home.png");
});


$(window).bind("load", function() {
	// equal column heights on home page
	if ($("body#home").length) {
		$("#blogBox, #articlesBox, #twitterBox").equalHeights();
	}

	if ($.browser.msie6 = $.browser.msie && parseInt($.browser.version) == 6 && !window["XMLHttpRequest"]) {
		$.getScript("/js/DD_belatedPNG_0.0.8a-min.js", function() {
			DD_belatedPNG.fix("h1, #navigation, #navigation li.on a, #navHome a, #socialPrimer, button, img.cover, div#twitterBox div.cta, #serviceList img, div#socialIcons a");
		});
	}
});


// Google Analytics
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-10330315-1']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();