﻿/// <reference path="jquery-1.3.2.min.js" />
$(document).ready(function() {

	// Function to allow an XHTML compliant way of making links open in a new window
	$("a[rel$='external']").click(function() {
		this.target = "_blank";
	});
	
	$.getJSON("../json/settings.txt", function(data) {
		var jsonObj = eval(data);
		var models = jsonObj.clients;
		initialiseColorboxes(models);
	});
	
	// Fields
	var isIE6 = false;
	var isIE = false;

	// Check for IE6 
	if (jQuery.browser.msie && jQuery.browser.version < 7) {
		isIE6 = true;
	}
	if (jQuery.browser.msie) {
		isIE = true;
	}
	
	// If browser is IE6, apply fix to any png images in the body content
	if (isIE6) {
		$("img[src$=.png]").ifixpng();
		$("h1").ifixpng();
	}
	
	$(".client").hover(
		function() {
			$(this).addClass("over");
		},
		function() {
			$(this).removeClass("over");
		}
	);
	
	function initialiseColorboxes(data) {
		$("a[rel='c1']").colorbox({ opacity:0.95, close: 'Close', clientData: data[0], overlayClose: false });
		$("a[rel='c2']").colorbox({ opacity:0.95, close: 'Close', clientData: data[1], overlayClose: false });
		$("a[rel='c3']").colorbox({ opacity:0.95, close: 'Close', clientData: data[2], overlayClose: false });
		$("a[rel='c4']").colorbox({ opacity:0.95, close: 'Close', clientData: data[3], overlayClose: false });
		$("a[rel='c5']").colorbox({ opacity:0.95, close: 'Close', clientData: data[4], overlayClose: false });
		$("a[rel='c6']").colorbox({ opacity:0.95, close: 'Close', clientData: data[5], overlayClose: false });
		$("a[rel='c7']").colorbox({ opacity:0.95, close: 'Close', clientData: data[6], overlayClose: false });
		$("a[rel='c8']").colorbox({ opacity:0.95, close: 'Close', clientData: data[7], overlayClose: false });
		$("a[rel='c9']").colorbox({ opacity:0.95, close: 'Close', clientData: data[8], overlayClose: false });
		$("a[rel='c10']").colorbox({ opacity:0.95, close: 'Close', clientData: data[9], overlayClose: false });
		$("a[rel='c11']").colorbox({ opacity:0.95, close: 'Close', clientData: data[10], overlayClose: false });
		$("a[rel='c12']").colorbox({ opacity:0.95, close: 'Close', clientData: data[11], overlayClose: false });
		$("a[rel='c13']").colorbox({ opacity:0.95, close: 'Close', clientData: data[12], overlayClose: false });
		$("a[rel='c14']").colorbox({ opacity:0.95, close: 'Close', clientData: data[13], overlayClose: false });
		$("a[rel='c15']").colorbox({ opacity:0.95, close: 'Close', clientData: data[14], overlayClose: false });
		$("a[rel='c16']").colorbox({ opacity:0.95, close: 'Close', clientData: data[15], overlayClose: false });
		$("a[rel='c17']").colorbox({ opacity:0.95, close: 'Close', clientData: data[16], overlayClose: false });
		$("a[rel='c18']").colorbox({ opacity:0.95, close: 'Close', clientData: data[17], overlayClose: false });
		$("a[rel='c19']").colorbox({ opacity:0.95, close: 'Close', clientData: data[18], overlayClose: false });
		$("a[rel='c20']").colorbox({ opacity:0.95, close: 'Close', clientData: data[19], overlayClose: false });
		$("a[rel='c21']").colorbox({ opacity:0.95, close: 'Close', clientData: data[20], overlayClose: false });
		$("a[rel='c22']").colorbox({ opacity:0.95, close: 'Close', clientData: data[21], overlayClose: false });
		$("a[rel='c23']").colorbox({ opacity:0.95, close: 'Close', clientData: data[22], overlayClose: false });
		$("a[rel='c24']").colorbox({ opacity:0.95, close: 'Close', clientData: data[23], overlayClose: false });
		$("a[rel='c25']").colorbox({ opacity:0.95, close: 'Close', clientData: data[24], overlayClose: false });
	}

	// Fields
	var isIE6 = false;
	var isIE = false;

	// Check for IE6 
	if (jQuery.browser.msie && jQuery.browser.version < 7) {
		isIE6 = true;
	}
	if (jQuery.browser.msie) {
		isIE = true;
	}
	
	$("#find_me_on li.first").hover(
		function() {
			$(this).addClass("open");
		}, 
		function() {
			$(this).removeClass("open");
		}
	);
	preloadImage("../images/bg_find_me_on_selected.gif");
	
	// Image preloader	
	function preloadImage(img) {
		jQuery("<img>").attr("src", img);
	}
	
	$("#leftnav li a").click(function() {
		// Reset any selected nav items
		$("#leftnav li").removeClass("current");
		
		$(".client_work .row .client").fadeTo("slow", 1.0);
		
		// Set the current item as selected
		var parentListItem = $(this).parent().parent().parent().parent().parent();
		parentListItem.addClass("current");
		
		// Get the tag id
		var tag = $(this).attr("rel");
		
		if (tag != "tag_all") {
			$(".client_work .row .client").each(function() {
				if (!$(this).hasClass(tag)) {
					$(this).fadeTo("slow", 0.2);
				}
			});
		}
		
		return false;
	});

	// Button rollovers
	$(".rollover").each(function() {
		var srcImage = $(this).attr("src");
		var rolloverImage = srcImage.replace(".gif", "_over.gif");
		jQuery("<img>").attr("src", rolloverImage);
		$(this).hover(
			function() {
				$(this).attr("src", rolloverImage);
			},
			function() {
				$(this).attr("src", srcImage);
			}
		);
	});

	// If browser is IE6, apply fix to any png images in the body content
	if (isIE6) {
		$("#content img[src$=.png]").ifixpng();
	}

	/**** DIV EQUAL HEIGHTS ****/
	var equalHeightRowCss = ".row_eq_height";
	var equalHeightColCss = ".col_eq_height";

	// Set the heights of columns within rows
	$(equalHeightRowCss).each(function() {
		setColumnHeights($(this));
	});

	function setColumnHeights(container) {
		equalHeight($(equalHeightColCss, container));
	}

	function equalHeight(group) {
		var tallest = 0;
		group.each(function() {
			thisHeight = $(this).height();
			if (thisHeight > tallest) {
				tallest = thisHeight;
			}
		});

		// IE6 does not support the min-height property so apply standard height
		if (isIE6) {
			group.css("height", tallest.toString() + "px");
		} else {
			group.css("min-height", tallest.toString() + "px");
		}
	}
});
