/*
*	TASL
*	Contains utility / interface functions for TASL
*	
*	Requires jQuery library (http://www.jquery.com),
*	SWFObject (http://blog.deconcept.com/swfobject/),
*	sIFR (http://www.mikeindustries.com/sifr/)
*	
*	Marcos Abreu (marcos@trapeze.com) - September 21, 2007
*	(Based on previous work of Taylan Prince)
*/

if (typeof trapeze == 'undefined') trapeze = new Object();

trapeze.Tasl = {
	
	assets_path : '/assets/',
	
	fixAlphaImgs : function(){
		this.display_alpha_img('SiteLogoImg', '/assets/img/site-logo', 220, 65, '', 'Trapeze Animation Studio');
		this.display_alpha_bg('#Header', '/assets/img/bg-page-content-top', 'no-repeat left bottom');
		this.display_alpha_bg('#Content', '/assets/img/bg-page-content', 'repeat-y left top');
		this.display_alpha_bg('#Footer', '/assets/img/bg-page-content-bottom', 'no-repeat left top');
	},
	
	section : function() {
		if ($("body").attr("class") && $("body").attr("class").indexOf("home") > -1) {
			return "home";
		} else {
			return "sub";
		}
	},
	
	sifr : function() {
		if (typeof sIFR == "function") {
			sIFR.replaceElement(named({sSelector:".page-title", sFlashSrc:this.assets_path + "fla/avenir-55.swf", sColor:"#8a1720", sLinkColor:"#8a1720", sBgColor:"#fcf8ec", sHoverColor:"#313131", sWmode:"transparent", nPaddingTop:0, nPaddingBottom:0}));
			sIFR.replaceElement(named({sSelector:".section-title", sFlashSrc:this.assets_path + "fla/avenir-55.swf", sColor:"#8a1720", sLinkColor:"#8a1720", sBgColor:"#fcf8ec", sHoverColor:"#313131", sWmode:"transparent", nPaddingTop:0, nPaddingBottom:4}));
			sIFR.replaceElement(named({sSelector:".sub-section-title", sFlashSrc:this.assets_path + "fla/avenir-55.swf", sColor:"#8a1720", sLinkColor:"#8a1720", sBgColor:"#fcf8ec", sHoverColor:"#313131", sWmode:"transparent", nPaddingTop:0, nPaddingBottom:4}));
			//sIFR.replaceElement(named({sSelector:".show-me-more", sFlashSrc:this.assets_path + "fla/avenir-65.swf", sColor:"#8a1720", sLinkColor:"#8a1720", sBgColor:"#fcf8ec", sHoverColor:"#313131", sWmode:"transparent", nPaddingTop:0, nPaddingBottom:0}));
		};
	},
	
	init_home_video : function(path) {
		if ($("#HomeVideo").size() > 0 && (document.player_path || path)) {
			var so = new SWFObject(this.assets_path + "fla/TASL_videoPlayer.swf", "HomeVideoPlayer", "515", "551", "8", "#fcf8ec");
			so.addParam("allowScriptAccess", "sameDomain");
			so.addParam("scale", "noscale");
			so.addParam("salign", "T");
			so.addParam("wmode", "transparent");
			so.addVariable("front", "false");
			so.addVariable("xmlPath", ((path) ? path : document.player_path));
			so.write("HomeVideo");
		}
	},

	init_project_video: function() {
		if ($("#ProjectMainVideos").size() > 0 ) {
			var path = $("#ProjectMainVideos").attr("href");
			var so = new SWFObject(this.assets_path + "fla/TASL_videoPlayer_small.swf", "ProjectVideoPlayer", "344", "264", "8", "#fcf8ec");
			so.addParam("allowScriptAccess", "sameDomain");
			so.addParam("scale", "noscale");
			so.addParam("salign", "T");
			so.addParam("wmode", "transparent");
			so.addVariable("front", "false");
			so.addVariable("xmlPath", path);
			so.write("ProjectVideo");
		}
	},
	mark_last : function() {
		$("li:last-child").addClass("last-child");
	},
	
	mark_first : function() {
		$("li:first-child").addClass("first-child");
		$("li:nth-child(2)").addClass("second-child");
	},
	
	clean_hr : function() {
		$("hr").wrap('<div class="hr"></div>');
	},
	
	navigation : function() {
		if ($("#SiteNav").size() > 0) {
			var xml = "<?xml version='1.0' encoding='utf-8'?>";
			xml += "<TASL>";
			$("#SiteNav").find("a").each(function() {
				xml += "<menuItem url='" + $(this).attr("href") + "'";
				if ($(this).attr("class") == "active") {
					xml += " selected='true'";
				}
				if ($("img", this).size() > 0) {
					xml += " ico='" + $("img", this)[0].src + "'";
				}
				xml += ">" + $(this).text() + "</menuItem>";
			});
			xml += "</TASL>";

			var so = new SWFObject(this.assets_path + "fla/menu.swf", "NavigationFlash", 640, 65, "8", "#2a0001");
			so.addVariable("xmlPath", xml);//this.assets_path + "xml/menu.xml");
			so.addParam("wmode", "transparent");
			so.write("SiteNav");
		}
	},
		
	init_gallery_pagination : function() {
		if ($("#PhotoGallery").size() > 0) {
			$("#PhotoGallery .pagination a").click(function(){
				var go_page = $(this).attr('href').substring($(this).attr('href').indexOf('page='));
				$.ajax({
					type: "GET",
					url: "gallery/",
					data: go_page,
					success: function(html){
						$("#PhotoGallery > ul, #PhotoGallery > div").fadeOut("slow",function(){
							$(this).remove()
							if ($("#PhotoGallery ul").size() <= 0) {
								$("#PhotoGallery").append(html);
								trapeze.Tasl.init_gallery_pagination();
								tb_init('a.thickbox');
							}
						});
					}
				});
				return false;
			});
		}
	},
	
	init : function() {
		this.navigation();
		this.mark_last();
		this.mark_first();
		this.clean_hr()
		this.sifr();
		this.init_home_video(this.assets_path + "xml/videos.xml");
		this.init_project_video();
		this.init_gallery_pagination();
	}
	
};

$(function() {
	trapeze.Tasl.init();
});

//////////////////// scroll ////////////////////////////////////////////////////////////////////////////
jQuery.getPos = function (e)
{
	var l = 0;
	var t  = 0;
	var w = jQuery.intval(jQuery.css(e,'width'));
	var h = jQuery.intval(jQuery.css(e,'height'));
	var wb = e.offsetWidth;
	var hb = e.offsetHeight;
	while (e.offsetParent){
		l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
		t += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
		e = e.offsetParent;
	}
	l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
	t  += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
	return {x:l, y:t, w:w, h:h, wb:wb, hb:hb};
};
jQuery.getClient = function(e)
{
	if (e) {
		w = e.clientWidth;
		h = e.clientHeight;
	} else {
		w = (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.offsetWidth;
		h = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;
	}
	return {w:w,h:h};
};
jQuery.getScroll = function (e) 
{
	if (e) {
		t = e.scrollTop;
		l = e.scrollLeft;
		w = e.scrollWidth;
		h = e.scrollHeight;
	} else  {
		if (document.documentElement && document.documentElement.scrollTop) {
			t = document.documentElement.scrollTop;
			l = document.documentElement.scrollLeft;
			w = document.documentElement.scrollWidth;
			h = document.documentElement.scrollHeight;
		} else if (document.body) {
			t = document.body.scrollTop;
			l = document.body.scrollLeft;
			w = document.body.scrollWidth;
			h = document.body.scrollHeight;
		}
	}
	return { t: t, l: l, w: w, h: h };
};

jQuery.intval = function (v)
{
	v = parseInt(v);
	return isNaN(v) ? 0 : v;
};

jQuery.fn.ScrollTo = function(s) {
	o = jQuery.speed(s);
	return this.each(function(){
		new jQuery.fx.ScrollTo(this, o);
	});
};

jQuery.fx.ScrollTo = function (e, o)
{
	var z = this;
	z.o = o;
	z.e = e;
	z.p = jQuery.getPos(e);
	z.s = jQuery.getScroll();
	z.clear = function(){clearInterval(z.timer);z.timer=null};
	z.t=(new Date).getTime();
	z.step = function(){
		var t = (new Date).getTime();
		var p = (t - z.t) / z.o.duration;
		if (t >= z.o.duration+z.t) {
			z.clear();
			setTimeout(function(){z.scroll(z.p.y, z.p.x)},13);
		} else {
			st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t;
			sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l;
			z.scroll(st, sl);
		}
	};
	z.scroll = function (t, l){window.scrollTo(l, t)};
	z.timer=setInterval(function(){z.step();},13);
};
