$(document).ready(init);
var handled = false;

function init() {
	$('#nav ul.main_nav li.main_nav_option span.over').css('display', 'none');
	$('#nav ul.main_nav li.main_nav_option').hover(nav_over, nav_out);
	$('#nav ul.main_nav li.main_nav_option').click(go);
	$('#nav div.sub_nav ul li').click(go);
	$('#nav div.sub_nav ul li').hover(sub_nav_over, sub_nav_out);
	
	// Homepage feeds
	$('.feed_item').click(go_feed);
	
	// Case Studies
	$('.cs_box').click(go);
	
	// Our Team
	$('.team_image').hover(team_over, team_out);
	
	// FancyBox Images
	$('.fancy').fancybox({'cyclic':true});
	$('.cs_image').click(swap_image);
	$('.snaps_image').click(swap_snap);
	
	// Search
	$('#search_button').click(search);
	$('#search').keyup(function(e) {
		//alert(e.keyCode);
		if(e.keyCode == 13) {
			search($(this));
		}
	});
	$('#search_button2').click(search);
	$('#search2').keyup(function(e) {
		//alert(e.keyCode);
		if(e.keyCode == 13) {
			search($(this));
		}
	});
}

function search(el) {
	var search;
	if ($(el).attr('id') == 'search_button' || $(el).attr('id') == 'search') {
		search = $('#search').val();
	} else {
		search = $('#search2').val();
	}
	location.href = '/sphider/search.php?search=1&query=' + search;
}

function swap_image() {
	$('#main_image').attr('src', '/images/cs/' + $(this).attr('id'));
}

function swap_snap() {
	$('#main_image').attr('src', '/images/snaps/' + $(this).attr('id'));
}

function go_feed(e) {
	arr = $(this).attr('id').split('_');
	if (arr[0] == 'news') {
		location.href = "/about/news.php#" + arr[1];
	} else {
		location.href = "http://www.twitter.com/asayocreative";
	}
}

function case_study_detail(e) {
	id = $(this).attr('id').substr(3, 1);
	location.href = "detail.php?cs=" + id;
}

function team_over(e) {
	src = $(this).attr('src');
	$(this).attr('src', src.substring(0, src.length-8) + '.jpg');
	console.info($(this).attr('src'));
}

function team_out(e) {
	src = $(this).attr('src');
	$(this).attr('src', src.substring(0, src.length-4) + '_off.jpg');
}

function nav_over(e) {
	//$(this).animate({color: "#0193c4", backgroundColor: "#333"}, 300);
	$(this).addClass('over');
	//$(this).find('.sub_nav').slideDown(300);
	$(this).find('.sub_nav').css('display','block');
}

function sub_nav_over(e) {
	//$(this).animate({backgroundColor: "#0193c4"}, 200);
	$(this).addClass('over');
}

function nav_out(e) {
	//$(this).animate({color: "white", backgroundColor: "#000"}, 300);
	$(this).removeClass('over');
	//$(this).find('.sub_nav').slideUp(300);
	$(this).find('.sub_nav').css('display','none');
}

function sub_nav_out(e) {
	//$(this).animate({backgroundColor: "#333"}, 200);
	$(this).removeClass('over');
}

function go(e) {
	if (!handled) {
		location.href = $(this).attr('id');
		handled = true;
	}
}