//////////////////////////////////////////
//  Pseudo Loader
/////////////////////////////////////////

var i = 0;
var int = 0; // for IE

$(window).bind('load', function() {
	var int = setInterval('loadPic(i)', 100);
});
function loadPic() {

	var slides = $('div.slide img').length;
	if (i >= slides) {
		clearInterval(int);
	}
	$('img:hidden').eq(0).fadeIn(500).parents().removeClass('loading');
	i++;
}

////////////////////////////////////////
// Over Label
////////////////////////////////////////
jQuery.fn.overlabel = function() {
    this.each(function(index) {
        var label = $(this); var field;
        var id = this.htmlFor || label.attr('for');
        if (id && (field = document.getElementById(id))) {
            var control = $(field);
            label.addClass("overlabel-apply");
            if (field.value !== '') {
                label.css("display", "none");
            }
            control.focus(function () {label.css("display", "none");}).blur(function () {
                if (this.value === '') {
                    label.css("display", "block");
                }
            });
            label.click(function() {
                var label = $(this); var field;
                var id = this.htmlFor || label.attr('for');
                if (id && (field = document.getElementById(id))) {
                    field.focus();
                }
            });
        }
    });
};

////////////////////////////////////////
// Doc Ready
////////////////////////////////////////
$(document).ready(function(){

	////////////////////////////////////////
	// Home Slide
	////////////////////////////////////////
	// Loader Adjustment
	//$('#featuredProjects div.slide img').hide();
	
	if(jQuery.fn.cycle) {
		var sld = $('#featuredProjects div.slide div.feature');
		if (sld.length > 1) {
			$('#featuredProjects div.slide').after(
				'<ul class="slideControl">'+
				'<li class="prev"><a>Previous</a></li>'+
				'<li class="next"><a>Next</a></li>'+
				'</ul>'
			);
			$('#featuredProjects div.slide').cycle({ 
				fx:     'fade', 
				speed:  700, 
				timeout: 7000,
				prev: 'li.prev a', 	
				next: 'li.next a'
			});
		}	
	}
	
	$('.slideControl li a, #featuredProjects .feature').hover(function(){
		$('#featuredProjects').addClass('hover');
	}, function(){
		$('#featuredProjects').removeClass('hover');
	});
	
	////////////////////////////////////////
	// Home Feed
	////////////////////////////////////////
	$('#homeFeed li, #caseStudies li, .media-buyers #cases li').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});
	$('#homeFeed li, #caseStudies li').click(function(){
		window.location=$(this).find("a").attr("href");return false;
	});
	$('#featuredProjects .info').click(function(){
		window.location=$('#featuredProjects .link').attr("href");return false;
	});

	////////////////////////////////////////
	// Client Slider
	////////////////////////////////////////
	// Loader Setup
	$('#clients div.slide img').hide().parents('a').addClass('loading');
	/*
	if(jQuery.fn.cycle) {
		var clients = $('#clients div.slide ul.grid');
		if (clients.length > 1) {
			$('#clients div.slide').after(
				'<ul class="slideControl">'+
				'<li class="prev"><a>Previous</a></li>'+
				'<li class="next"><a>Next</a></li>'+
				'</ul>'
			);
			$('#clients div.slide').cycle({ 
				fx:     'fade', 
				speed:  700, 
				timeout: 0,
				prev: '#clients li.prev a', 	
				next: '#clients li.next a'
			});
		}
	}
	*/
	////////////////////////////////////////
	// Project Slider
	////////////////////////////////////////
	// Loader Setup
	//$('#projects div.slide img').hide().parents('a').addClass('loading');
	if(jQuery.fn.cycle) {
		var proj = $('#projects div.slide ul.grid');
		var vid = $('#videos div.slide ul.grid');
		if (proj.length > 1) {
			$('#projects div.slide').after(
				'<ul class="slideControl">'+
				'<li class="prev"><a>Previous</a></li>'+
				'<li class="next"><a>Next</a></li>'+
				'</ul>'
			);
			$('#projects div.slide').cycle({ 
				fx:     'fade', 
				speed:  700, 
				timeout: 0,
				prev: '#projects li.prev a', 	
				next: '#projects li.next a'
			});
		}
		if (vid.length > 1) {
			$('#videos div.slide').after(
				'<ul class="slideControl">'+
				'<li class="prev"><a>Previous</a></li>'+
				'<li class="next"><a>Next</a></li>'+
				'</ul>'
			);
			$('#videos div.slide').cycle({ 
				fx:     'fade', 
				speed:  700, 
				timeout: 0,
				prev: '#videos li.prev a', 	
				next: '#videos li.next a'
			});
		}
	}
	// Lightbox
	if(jQuery.fn.fancybox) {
		$("#projects .slide a, #videos .slide a, #clients a.box, #homeFeed a.box").fancybox({
			padding : 20,
			frameWidth : 810,
			frameHeight : 500
		});
	}

	////////////////////////////////////////
	// Case Study
	////////////////////////////////////////

	//$('#projectImages .slide img').hide().parents('.slide').addClass('loading');

	if(jQuery.fn.columnize) {
		$('#projectInfo').columnize({
			columns: 2,
			buildOnce : false
		})
	}

	if(jQuery.fn.cycle) {
		var pimg = $('#projectImages .slide img');
		if (pimg.length > 1) {
			$('#projectImages .slide').after(
				'<ul class="slideControl">'+
				'<li class="prev"><a>Previous</a></li>'+
				'<li class="next"><a>Next</a></li>'+
				'</ul>'
			);
			$('#projectImages .slide').cycle({ 
				fx:     'fade', 
				speed:  700, 
				timeout: 7000,
				prev: 'li.prev a', 	
				next: 'li.next a, #projectImages .slide img'
			});
		}
	}
	// Case Study Video
	if(jQuery.fn.fancybox) {
		$("a.vidLink").fancybox({
			padding : 20,
			frameWidth : 810,
			frameHeight : 500
		});
	}

	////////////////////////////////////////
	// Press Feed
	////////////////////////////////////////
	if(jQuery.fn.cycle) {
		var prs = $('#pressFeed .slide ul');
		if (prs.length > 1) {
			$('#pressFeed .slide').after(
				'<ul class="slideControl">'+
				'<li class="prev"><a>Previous</a></li>'+
				'<li class="next"><a>Next</a></li>'+
				'</ul>'
			);
			$('#pressFeed .slide').cycle({ 
				fx:     'fade', 
				speed:  700, 
				timeout: 0,
				prev: 'li.prev a', 	
				next: 'li.next a'
			});
		}
	}
	if(jQuery.fn.fancybox) {
		$("#pressFeed .slide a").fancybox({
			padding : 20,
			frameWidth : 810,
			frameHeight : 500
		});
	}
	
	////////////////////////////////////////
	// Contact Form
	////////////////////////////////////////
	$('#cForm label').overlabel();
	if(jQuery.fn.validate) {
		$('#cForm').validate();
	}
	
	$('#footer label').overlabel();
	$('#login label').overlabel();
	

	// External Links
	$('a[rel*="external"]').live('click', function(){
		window.open(this.href);
		return false;
	});
  	
});
