$(function () {
	if ($.browser.msie && parseInt($.browser.version) <= 6) {
		$("th:first-child").addClass("first-child")
	}
	$("#menu img").pngFixImage()
	$("th:last-child").addClass("last-child")
});

$(function () {
	$("a.zoom").fancybox({
		zoomOpacity: true,
		// overlayShow: false,
		zoomSpeedIn: 500,
		zoomSpeedOut: 500,
		titlePosition: 'over'
	});

	$("#slides").shuffle().slideshow({
		elems: "> div.slide",
		speed: 1000,
		interval: 3500,
		prev: "> a.prev",
		next: "> a.next"
	});

	$("div.project div.photos").slideshow({
		elems: "> div.photo",
		speed: 1000,
		interval: 3500,
		prev: "> a.prev",
		next: "> a.next"
	});
	
	$("#sidebar div.our-projects").shuffle().slideshow({
		elems: "> div.project",
		speed: 1000,
		interval: 3500
	});
	
	$("#content div.about-us div.links ul").each(function () {
		var all = $(this).find("li").remove().show().get()
		for (var i = 0; i < 3 && all.length > 0; i++) {
			var r = Math.floor(Math.random()*all.length)
			$(this).append(all[r])
			all.splice(r, 1)
		};
	})

	$("#content div.did-you-know ul").each(function () {
		var all = $(this).find("li")
		all.hide().eq(Math.floor(Math.random()*all.length)).show()
	})
});

/**
 *
 * jQuery-Plugin "pngFix"
 * Version: 1.2, 09.03.2009
 * by Andreas Eberhard, andreas.eberhard@gmail.com
 *                      http://jquery.andreaseberhard.de/
 *
 * Copyright (c) 2007 Andreas Eberhard
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Modified by Jan Dudek, Oct 2009
 */

(function ($) {
	var fixNeeded = $.browser.msie && parseInt($.browser.version) <= 6

	$.fn.pngFixImage = function () {
		if (!fixNeeded) return this
		return this.each(function () {
			var img = this
			var loader = new Image()
			loader.onload = function () {

				var w = loader.width
				var h = loader.height

				var prevStyle = '';
				var strNewHTML = '';
				var imgId = ($(img).attr('id')) ? 'id="' + $(img).attr('id') + '" ' : '';
				var imgClass = ($(img).attr('class')) ? 'class="' + $(img).attr('class') + '" ' : '';
				var imgTitle = ($(img).attr('title')) ? 'title="' + $(img).attr('title') + '" ' : '';
				var imgAlt = ($(img).attr('alt')) ? 'alt="' + $(img).attr('alt') + '" ' : '';
				var imgAlign = ($(img).attr('align')) ? 'float:' + $(img).attr('align') + ';' : '';
				var imgHand = ($(img).parent().attr('href')) ? 'cursor:hand;' : '';
				if (img.style.border) {
					prevStyle += 'border:'+img.style.border+';';
					img.style.border = '';
				}
				if (img.style.padding) {
					prevStyle += 'padding:'+img.style.padding+';';
					img.style.padding = '';
				}
				if (img.style.margin) {
					prevStyle += 'margin:'+img.style.margin+';';
					img.style.margin = '';
				}
				var imgStyle = (img.style.cssText);

				strNewHTML += '<span '+imgId+imgClass+imgTitle+imgAlt;
				strNewHTML += 'style="white-space:pre-line;display:inline-block;background:transparent;'+imgAlign+imgHand;
				strNewHTML += 'width:' + w + 'px;' + 'height:' + h + 'px;';
				strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + $(img).attr('src') + '\', sizingMethod=\'scale\');';
				strNewHTML += imgStyle+'"></span>';
				if (prevStyle != ''){
					strNewHTML = '<span style="display:inline-block;'+prevStyle+imgHand+'width:' + $(img).width() + 'px;' + 'height:' + $(img).height() + 'px;'+'">' + strNewHTML + '</span>';
				}

				$(img).hide();
				$(img).after(strNewHTML);
			}
			loader.src = $(img).attr('src')
		})
	}
	
	$.fn.pngFixInput = function (settings) {
		if (!fixNeeded) return this
		return this.each(function () {
			var bgIMG = $(this).attr('src');
			$(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'scale\');';
	   		$(this).attr('src', settings.blank)
		})
	}
	
	$.fn.pngFix = function (settings) {
		if (!fixNeeded) return this
		settings = $.extend({ blank: 'blank.gif' }, settings);
		return this.each(function () {
			$(this).find("img[src$=.png]").pngFixImage()
			$(this).find("input[src$=.png]").pngFixInput(settings)
		})
	}

})(jQuery);


/*
 * Copyright (c) 2010, Jan Dudek
 * Licensed under MIT license.
 *
 * Usage example:
 * $(function () { $("input[type=text]").placeholder(); })
 */
(function ($) {
	var html5 = ("placeholder" in document.createElement("input"))
	$.fn.placeholder = function () {
		return this.each(function () {
			if (!html5) {
				if ($(this).val() == "") $(this).val($(this).attr("placeholder"))
				$(this).focus(function () {
					if ($(this).val() == $(this).attr("placeholder")) $(this).val("")
				})
				$(this).blur(function () {
					if ($(this).val() == "") $(this).val($(this).attr("placeholder"))
				})
			}
		})
	}
})(jQuery);

/*
 * Copyright (c) 2010, Jan Dudek
 * Licensed under MIT license.
 *
 * Usage example:
 *		$("#slides").slideshow({
 *			elems: "> .slide",
 *			nums: "> ul.n",
 *			speed: 1000,
 *			interval: 2500
 *		})
 */
(function($) {
	$.fn.slideshow = function (opt) {
		return this.each(function () {
			var interval = null
			var $this = $(this);
			var elems = $this.find(opt.elems);
			var nums = $this.find(opt.nums);

			if (elems.length < 2) return;
			elems.hide().css({ position: "absolute", top: 0, left: 0 }).addClass("slide").
				eq(0).show().addClass("current");
			nums.find("li:first a").addClass("current");

			function switchTo(elem) {
				elems.filter(".current").fadeOut(opt.speed);
				elems.removeClass("current")
				elem.addClass("current").fadeIn(opt.speed);
				nums.find("li a")
					.removeClass("current")
					.filter(function() {
						return $(this).attr("href") == "#" + elem.attr("id")
					})
					.addClass("current")
			}
			function next() {
				var e = elems.filter(".current");
				if (e.next().is(".slide"))
					switchTo(e.next());
				else
					switchTo(elems.filter(":first"));
			}
			function prev() {
				var e = elems.filter(":visible");
				if (e.prev().is(".slide"))
					switchTo(e.prev());
				else
					switchTo(elems.filter(":last"));
			}
			nums.find("li a").click(function() {
				if (interval) clearInterval(interval)
				switchTo($($(this).attr("href")))
				return false
			});
			if (opt.prev) {
				$this.find(opt.prev).show().click(function () {
					prev()
					if (interval) clearInterval(interval)
					return false
				})
			};
			if (opt.next) {
				$this.find(opt.next).show().click(function () {
					next()
					if (interval) clearInterval(interval)
					return false
				})
			};
			interval = setInterval(next, opt.interval)
		})
	}
})(jQuery);

/*
 * jQuery shuffle
 *
 * Copyright (c) 2008 Ca-Phun Ung <caphun at yelotofu dot com>
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://yelotofu.com/labs/jquery/snippets/shuffle/
 *
 * Shuffles an array or the children of a element container.
 * This uses the Fisher-Yates shuffle algorithm <http://jsfromhell.com/array/shuffle [v1.0]>
 */
 
(function($){

	$.fn.shuffle = function() {
		return this.each(function(){
			var items = $(this).children().clone(true);
			return (items.length) ? $(this).html($.shuffle(items)) : this;
		});
	}
	
	$.shuffle = function(arr) {
		for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
		return arr;
	}
	
})(jQuery);

