/*
	Saforian.com scripts (utilizing jQuery 1.3.2)
*/

/* --- Initialize page --- */
$(document).ready(function(){

	// Stripe row colors
	$("table.data tr:nth-child(even)").not(".even").not(".odd").addClass("even");

	// Insert document icons
	$("a[href$=pdf]:not(:has(img))").append('<img class="icon" src="/images/icon_pdf.gif" width="14" height="14" alt=" (PDF)">');
	$("a[href$=doc]:not(:has(img)), a[href$=docx]:not(:has(img))").append('<img class="icon" src="/images/icon_word.gif" width="14" height="14" alt=" (Word Document)">');
	$("a[href$=xls]:not(:has(img)), a[href$=xlsx]:not(:has(img))").append('<img class="icon" src="/images/icon_excel.gif" width="14" height="14" alt=" (Excel Spreadsheet)">');
	$("a[href$=ppt]:not(:has(img)), a[href$=pptx]:not(:has(img))").append('<img class="icon" src="/images/icon_powerpoint.gif" width="14" height="14" alt=" (Powerpoint Presentation)">');

	// Column childs CSS3 fix
	$(".columns>.col:last-child").addClass("last-child");
	$(".columns>.col:first-child").addClass("first-child");
	$(".columns.three>.col:nth-child(2)").addClass("middle-child");

	// IE6 only helper functions
	if (document.all && navigator.appName == "Microsoft Internet Explorer" && (navigator.userAgent.indexOf("MSIE 5.5") >= 0 || navigator.userAgent.indexOf("MSIE 6.") >= 0)) {

		// Navigation hover
		$("#navigation>li").hover(function(){
			$(this).addClass("hover");
		}, function(){
			$(this).removeClass("hover");
		});

		// PNG fix
		iepngfix();
	}



	/* --- More link --- */

	// Hide extra content
	$("#content div.showmore").hide();

	var newlink = document.createElement("a");
	newlink.href = "#";
	newlink.className = "showmore";
	newlink.innerHTML = "More&nbsp;&rarr;";

	$(newlink).click(function(){
		$(this).hide().parent().next("div.showmore").show("normal");
		return false;
	});

	$("#content div.showmore").prev().append(newlink);



	/* --- Portfolio client filter --- */

	if (document.getElementById("portfolio")) {
		var x = true;

		$("#filter a").click(function(){
			$(this).toggleClass("on");

			if (x) {
				$("#clients li").fadeTo("normal", .4);
			} else {
				$("#clients li").fadeTo("normal", 1);
			}
			x = !x;

			return false;
		});


	}




	/* --- Portfolio image gallery --- */

	if (document.getElementById("portfolio")) {

		var $gallery = $("ul#gallery");

		// Create extra elements & navigation links
		$gallery
			.wrap('<div id="gallery-wrapper"></div>')
			.before('<a href="#" id="gallery-next"><img src="/images/arrow_right.png" alt="Next"></a><a href="#" id="gallery-previous"><img src="/images/arrow_left.png" alt="Previous"></a><p id="gallery-caption"></p>')
			.wrap('<div id="gallery-innerwrapper"></div>');

		// Remove IE's alt tooltip
		if ($.browser.msie) { $gallery.find("img").attr("title", ""); }

		// Settings
		var currentimage = 0;
		var imagecount = $gallery.children("li").size();

		// Image width
		var $firstimage = $gallery.find("img:first");
		var gallerywidth = 517; // $firstimage.width();  // IE miscalculates?
		var galleryheight = 392; // $firstimage.height();

		// Set the strip's actual width to be safe
		$gallery.width(gallerywidth * imagecount);

		// Set initial caption
		
/*
disabled due to lack of content for captions all occurances of any caption action have been commented out

		if ($firstimage.attr("alt")) {
			$("#gallery-caption").html($firstimage.attr("alt"));
			$("#gallery-caption").show();
		} else {
			$("#gallery-caption").hide();
		}
*/

		// Movement links
		$("#gallery-next").click(function(){

			galleryspeed = 1000;
			currentimage++;
			if (currentimage >= imagecount) { currentimage = 0; galleryspeed = 500; }

			
			// Update caption
			/*
			var caption = $gallery.find("img:eq(" + currentimage + ")").attr("alt");
			$("#gallery-caption").fadeOut("60", function(){
				$("#gallery-caption").html(caption);
			});
			*/
			// Slide image
			$gallery.animate({left: -gallerywidth*currentimage}, galleryspeed, "swing", function(){
				// Show next caption
				/*
				if (caption) { $("#gallery-caption:not(:empty)").fadeIn("900"); }
				*/
			});


			return false;
		});

		$("#gallery-previous").click(function(){

			galleryspeed = 1000;
			currentimage--;
			if (currentimage < 0) { currentimage = imagecount-1; galleryspeed = 500; }

			// Update caption
			/*
			var caption = $gallery.find("img:eq(" + currentimage + ")").attr("alt");
			$("#gallery-caption").fadeOut("60", function(){
				$("#gallery-caption").html(caption);
			});
			*/

			// Slide image
			$gallery.animate({left: -gallerywidth*currentimage}, galleryspeed, "swing", function(){
				// Show next caption
				/*
				if (caption) { $("#gallery-caption").fadeIn("900"); }
				*/
			});

			return false;
		});


		// Enlarged images
		$("#gallery.enlarge img").click(function(){

			// Show overlay
			return false;
			//alert("I'm working on it");


		});
	}

/*
pORTFOLIO mANIPULATION
*/

	$('#featuredprojects .project p')
			.wrap('<div class="projectwrap"></div>')
			.wrap('<div class="projectwrapbtm"></div>');

	$('#featuredprojects .project:nth-child(4), #featuredprojects .project:nth-child(8), #featuredprojects .project:nth-child(12)').after('<div class="portline"></div>');


});




/* --- IE6 PNG fix --- */
function iepngfix() {

	// IE 5.5 and 6.0 PNG filter support (derived from youngpup.net)
	$("img[src$=png]").each(function(){
		var src = this.src;
		var div = document.createElement("div");

		// Set replacement div properties
		div.id = this.id;
		div.className = this.className;
		div.title = this.title || this.alt;
		div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "', sizing='scale')";
		div.style.width = this.width + "px";
		div.style.height = this.height + "px";

		// Replace image with transparent div
		this.replaceNode(div);
	});
}