$(function()
{
	
	$("a#video_play").click(function() {
		$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'			: 640,
			'height'		: 385,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
			'wmode'				: 'transparent',
			'allowfullscreen'	: 'true'
			}
		});

		return false;
	});

	
	fix_height();
	contentScrollTo('home', true);
	dragMove();
	linkMove();
	video();
	$('#pictures a').lightBox({
		maxHeight: 500,
		maxWidth: 700
	});


	// jplayer

	// Local copy of jQuery selectors, for performance.
	var jpPlayInfo = $("#play-info");
	var jpStatus = $("#demo_status"); // For displaying information about jPlayer's status in the demo page

	$("#jquery_jplayer").jPlayer({
		ready: function () {
			$("#track-1").trigger("click");
			demoInstanceInfo(this.element, $("#demo_info")); // This displays information about jPlayer's configuration in the demo page
		},
		swfPath: "../js",
		errorAlerts: true,
   nativeSupport: true,
   oggSupport: false 
	})
	.jPlayer("cssId", "play", "play")
	.jPlayer("cssId", "pause", "pause")
	.jPlayer("cssId", "next", "next")
	.jPlayer("cssId", "stop", "stop")
	.jPlayer("cssId", "volumeMin", "vmin")
	.jPlayer("cssId", "volumeMax", "vmax")
	.jPlayer("cssId", "volumeBar", "vbar")
	.jPlayer("onProgressChange", function(lp,ppr,ppa,pt,tt) {
		jpPlayInfo.text();
		demoStatusInfo(this.element, jpStatus); // This displays information about jPlayer's status in the demo page
	});

	$("#track-1").click( changeTrack );
	$("#track-2").click( changeTrack );
	$("#track-3").click( changeTrack );
	$("#track-4").click( changeTrack );
	$("#track-5").click( changeTrack );
	$("#track-6").click( changeTrack );
	$("#track-7").click( changeTrack );
	$("#track-8").click( changeTrack );
	$("#track-9").click( changeTrack );
	$("#track-10").click( changeTrack );
	$("#track-11").click( changeTrack );
	$("#track-12").click( changeTrack );
	$("#track-13").click( changeTrack );
	$("#track-14").click( changeTrack );
	$("#track-15").click( changeTrack );
	$("#track-16").click( changeTrack );
	$("#track-17").click( changeTrack );
	$("#track-18").click( changeTrack );
	$("#track-19").click( changeTrack );
	$("#track-20").click( changeTrack );

	function changeTrack(e) {
		$("#trackname").text($(this).text());
		$("#jquery_jplayer").jPlayer("setFile", $(this).attr("href")).jPlayer("play");
		$(this).blur();
		return false;
	}

	$.jPlayer.timeFormat.padMin = false;
	$.jPlayer.timeFormat.padSec = false;
	$.jPlayer.timeFormat.sepMin = "min ";
	$.jPlayer.timeFormat.sepSec = "sec";

});

function fix_height()
{
	if($.browser.msie && $.browser.version=="8.0" )
	{
		//var height = $(document).height() - $('#menu').height() - 5;
		$('#content-i').height(0);
		var height = $('#website tr').eq(1).height();
		$('#content-i').height(height);
	}
}

function dragMove()
{
	$('#content-i').dragscrollable({dragSelector: '*', acceptPropagatedEvent: false});
	$('#content-i, #content-i *').mousedown(function()
	{
		$('#content-i').css({'cursor':'pointer'});
	})
	.mouseup(function()
	{
		$('#content-i').css({'cursor':'default'});
	});
}

function linkMove()
{
	$('a[class^="scrollTo"]').click(function()
	{
		var id = this.className.match(/scrollTo-(\w+)/)[1];
		contentScrollTo(id);
	});
}



function contentScrollTo(id, tohome)
{
	if (typeof tohome == "undefined") {
		tohome = false;
	}
	var website = $('#content-i');
	var offset = $('#'+id).positionAncestor('#content-i');
	offset.left += parseInt($('#'+id).css('padding-left'));
	var scrollTop = website.scrollTop();
	var scrollLeft = website.scrollLeft();
	var toLeft = (offset.left+scrollLeft) - ((website.width() - $('#'+id).width()) / 2);
	var toTop = (offset.top+scrollTop) - 20;
	if(id=="hometwo")
	{
		$("#video").css("display", "none");
		toTop = (offset.top+scrollTop) - ((website.height() - $('#'+id).height()) / 2) + 40;
	}
	if(id=="video")
	{
		var _width = $(document).width();
		var _height = $(document).height() - 60;
		var _left = 3903 + (942/2) - (_width/2);
		var _top = 3324 + (70/2) - (_height/2);
		$("#video").css("display", "block");
		$('#video').css({'width': _width+'px', 'height': _height+'px', 'left': _left+'px', 'top': _top+'px'});
		toTop = (offset.top+scrollTop) - ((website.height() - $('#'+id).height()) / 2);
	}
	if(tohome)
	{
		$("#video").css("display", "none");
		toTop = (offset.top+scrollTop) - ((website.height() - $('#'+id).height()) / 2) + 40;
	}
	if(toLeft < 0) toLeft = 0;
	if(toTop < 0) toTop = 0;
	if(!tohome)
	{
		website.scrollTo({left:toLeft+'px', top:toTop+'px'}, 1000);
	}
	else
	{
		website.scrollTo({left:toLeft+'px', top:toTop+'px'}, 0);
	}
}

function video()
{
	var _width = $(document).width();
	var _height = $(document).height() - 60;
	var _left = 3903 + (942/2) - (_width/2);
	var _top = 3324 + (70/2) - (_height/2) + 45;
	$('#video').css({'width': _width+'px', 'height': _height+'px', 'left': _left+'px', 'top': _top+'px'});
}

/**
* Get the current coordinates of the first element in the set of matched
* elements, relative to the closest positioned ancestor element that
* matches the selector.
* @param {Object} selector
*/
jQuery.fn.positionAncestor = function(selector) {
	var left = 0;
	var top = 0;
	this.each(function(index, element) {
		// check if current element has an ancestor matching a selector
		// and that ancestor is positioned
		var $ancestor = $(this).closest(selector);
		if ($ancestor.length && $ancestor.css("position") !== "static") {
			var $child = $(this);
			var childMarginEdgeLeft = $child.offset().left - parseInt($child.css("marginLeft"), 10);
			var childMarginEdgeTop = $child.offset().top - parseInt($child.css("marginTop"), 10);

			var borderWidth = parseInt($ancestor.css("borderLeftWidth"), 10);
			if (isNaN(borderWidth)) borderWidth = 0;
			var ancestorPaddingEdgeLeft = $ancestor.offset().left + borderWidth;

			var borderWidth = parseInt($ancestor.css("borderTopWidth"), 10);
			if (isNaN(borderWidth)) borderWidth = 0;
			var ancestorPaddingEdgeTop = $ancestor.offset().top + borderWidth;
			left = childMarginEdgeLeft - ancestorPaddingEdgeLeft;
			top = childMarginEdgeTop - ancestorPaddingEdgeTop;

			// we have found the ancestor and computed the position
			// stop iterating
			return false;
		}
	});
	return {
		left:    left,
		top:    top
	}
};
