/**
 * @author Fabian de Rijk
 * @company Total Active Media
 */

var style;
var styleNr;
var BGmaxed = false;
var ie6 = ( window.external && typeof window.XMLHttpRequest == "undefined" );

window.addEvent('domready', function(){
	getStyle();
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	
	if ((version >= 5.5) && (document.body.filters)) 
	{
	   for(var i=0; i<document.images.length; i++) {
	      var img = document.images[i];
	      var imgName = img.src.toUpperCase();
	      if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
	         var imgID = (img.id) ? "id='" + img.id + "' " : ""
	         var imgClass = (img.className) ? "class='" + img.className + "' " : ""
	         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
	         var imgStyle = "display:inline-block;" + img.style.cssText 
	         if (img.align == "left") imgStyle = "float:left;" + imgStyle
	         if (img.align == "right") imgStyle = "float:right;" + imgStyle
	         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
	         var strNewHTML = "<span " + imgID + imgClass + imgTitle
	         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
	         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
	         img.outerHTML = strNewHTML
	         i = i-1
	      }
	   }
	}
});

window.addEvent('resize', function() {
	var winSize = $(window).getSize();
	$('stickySurface').setStyle('height', winSize.size.y);
	$('stickySurface').setStyle('width', winSize.size.x);
});

function getPortfolio(id, page) {
	if(page > 0) {
		var plus = '&start='+page;
	} else {
		var plus = '';
	}
	var url = 'mods/portfolio/content.php?id='+id+plus;
	new Ajax(url, {
		method: 'get',
		update: $('replace_portfolio')
	}).request();
}

function showAllPortfolio(start) {
	var url = 'mods/portfolio/content.php?start='+start;
	new Ajax(url, {
		method: 'get',
		update: $('replace_portfolio')
	}).request();
}

function showAllPortfolioOud(type) {
	if(type == 'all') {
		var all = '?all=all';
	} else {
		var all = '';
	}
	var url = 'mods/portfolio/content.php'+all;
	new Ajax(url, {
		method: 'get',
		update: $('replace_portfolio')
	}).request();
}

function showAbout(type) {
	var url = 'mods/about/content.php?type='+type;
	new Ajax(url, {
		method: 'get',
		update: $('replace_about')
	}).request();
}

function getTopics(start, labelid) {
	if (labelid) {
		var url = 'mods/latest_topics/content.php?start='+start+'&label='+labelid;
	} else {
		var url = 'mods/latest_topics/content.php?start='+start;
	}
	new Ajax(url, {
		method: 'get',
		update: $('replace_LATESTTOPICS')
	}).request();
}

function getArticles(labelid) {
	var url = 'mods/latest_topics/content.php?label='+labelid;
	new Ajax(url, {
		method: 'get',
		update: $('replace_LATESTTOPICS')
	}).request();
}

function showArticle(id) {
	maxApplet('article');
	var url = 'mods/article/content.php?id='+id;
	new Ajax(url, {
		method: 'get',
		update: $('replace_article')
	}).request();
}

function getStyle() {
	var cookie = readCookie('style');
	if(cookie) {
		changeStyle(cookie)
	}
}

function showBG() {
	if(BGmaxed) {
		minApplet('BGSEL');
		BGmaxed = false;
	} else {
		maxApplet('BGSEL');
		BGmaxed = true;
	}
}

function changeStyle(style) {
	// set cookie
	var cookie = readCookie('style');
	if(cookie) {
		updateCookie('style', style);
	} else {
		setCookie('style', style);
	}
	
	switch(style) {
		case 'business':
			// create an array of windows to hide
			var hide = new Array();
			hide[0] = "BANNERS";
			hide[1] = "GBL";
			hide[2] = "LOA";
			hide[3] = "article";
			hide[4] = "DIM";
			hide[5] = "LINK";
			hide[6] = "DUMPLINKS";
			hide[7] = "ADDARTICLE";
			hide[8] = "ADDMOVIE";
			hide[9] = "ADDIMAGE";
			hide[10] = "FORPASS";
			hide[11] = "SHOWREELBANNER";
			hide[12] = "MOVIEOFTHEDAY";
			hide[13] = "archive";
			hide[14] = "BGSEL";
			hide[15] = "SHOWREEL";
			hide[16] = "TWITTER";
			hide[17] = "GMAPS";
			hide[18] = "slideShare";
			hide[19] = "FOURSQUARE";
			hide[20] = "ACTIVEMEDIAGAME";
			hide[21] = "SOCIALMEDIAPOSTER";
			
			// create an array of windows to show
			var show = new Array();
			show[0] = "portfolio";
			show[1] = "NAMEBAR";
			show[2] = "about";
			show[3] = "LATESTTOPICS";
			show[4] = "contact";
			
			// show windows and then hide windows
			show.each(function(x) {
				var maxEl = 'max_'+x;
				if ($(maxEl)) {
					$(maxEl).setStyle('display', 'none');
				}
				maxApplet(x);
			});
			
			var myChain = new Chain();
			
			myChain.chain(function() {
				// change css styles
				$('stickySurface').setStyle( 'background', '#FFFFFF url( bgpics/business.jpg ) no-repeat top left' );
				$('backgroundPullDown').setStyle('display', 'none');
				$('BGtext').setStyle('display', 'none');
				$('styleRight').addClass('businessR');
				$('styleLeft').addClass('businessL');
				$$('.Close').setStyle('display', 'none');
				$E('.Close', 'article').setStyle('display', 'block');
				
				// change topnav
				$('styleLeft').addClass('styleLeft');
				$('styleRight').removeClass('styleRight');
				$('styleRight').removeClass('businessR');
				
				
				$$('html').setStyle('overflow-y', 'scroll');
				$('stickySurface').setStyle('height', '800px');
				
				show.each(function(el) {
					if ($(el + 'DH')) {
						$(el+'DH').removeEvents();
						$(el).getChildren().forEach(function(chEl){
							$(chEl).removeClass('dragHandleDragger');
							$(chEl).getChildren().forEach(function(chchEl){
								$(chchEl).removeClass('dragHandleDragger');
								$(chchEl).getChildren().forEach(function(chchchEl){
									$(chchchEl).removeClass('dragHandleDragger');
								})
							})
						});
					}
				});
			})
			
			hide.each(function(x) {
				myChain.chain(function() {
					var maxEl = 'max_'+x;
					var maxArray = $$('div.maximize');
					maxArray.each(function(item)
					{
						if(item.id == maxEl)
						{
							$(maxEl).setStyle('display','none');
						}
					});
					fadeElt( x, 0, 0 );
					$(x).setStyle( 'opacity', '0' );
					updateCookie(x,'active=0|');
					$(x).setStyle( 'top', $(x).getStyle('top').toInt() - shiftUp + 'px' );
				})
			});
			
			myChain.chain(function() {
				// animate the bunch
				//aniBusiness(show);
				var windowWidth = $(window).getSize().size.x;
				var x;
				var y;
				var elWidth;
				
				elWidth = $('portfolio').getCoordinates().width;
				
				if(ie6 == true) {
					var portPos = 100;
				} else {
					var portPos = (windowWidth - elWidth) / 2;
				}
				
				var portfolioFX = new Fx.Styles($('portfolio'), { duration: 200, transition: Fx.Transitions.Quad.easeIn, onComplete: function() {
					var NAMEBARFX = new Fx.Styles($('NAMEBAR'), { duration: 200, transition: Fx.Transitions.Quad.easeIn, onComplete: function() {
						var aboutFX = new Fx.Styles($('about'), { duration: 200, transition: Fx.Transitions.Quad.easeIn, onComplete: function() {
							var ltFX = new Fx.Styles($('LATESTTOPICS'), { duration: 200, transition: Fx.Transitions.Quad.easeIn, onComplete: function() {
								var contactFX = new Fx.Styles($('contact'), { duration: 200, transition: Fx.Transitions.Quad.easeIn }).start({ 'left': $('LATESTTOPICS').getPosition().x + $('LATESTTOPICS').getCoordinates().width + 9, 'top': 305 });
							} }).start({ 'left': $('about').getPosition().x + $('about').getCoordinates().width + 9, 'top': 305 });
						} }).start({ 'left': $('portfolio').getPosition().x, 'top': 305 });
					} }).start({ 'left': $('portfolio').getPosition().x,	'top': 66});
				}}).start({ 'left': portPos, 'top': 100 });
			})
			
			var runChain = function() {
				myChain.callChain();
				if (myChain.chains.length == 0) { 
					runChain = $clear(timer);
				} 
			}
			
			if(cookie == 'business') {
				var timer = runChain.periodical(1);
			} else {
				var timer = runChain.periodical(100);
			}
			
			break;
		case 'chaos':
			if (cookie != 'chaos') {
				eraseCookies();
			}
			// change topnav
			$('styleLeft').removeClass('styleLeft');
			break;
	}
}

function aniBusiness(show) {
	var aniChain = new Chain();
	
	show.each(function(el) {
		aniChain.chain(function() {
			doAni(el);
		})
	})
	
	var runChain = function() {
		aniChain.callChain();
		if (aniChain.chains.length == 0) { 
			runChain = $clear(timer);
		} 
	}
	
	var timer = runChain.periodical(400);
}

function doAni(el) {
	var windowWidth = $(window).getSize().size.x;
	var x;
	var y;
	var elWidth;
	
	elWidth = $(el).getCoordinates().width;
	switch (el) {
		case 'portfolio':
			var myMove = new Fx.Styles($(el), {
				duration: 200,
				transition: Fx.Transitions.Quad.easeIn
			});
			myMove.start({
				'left': (windowWidth - elWidth) / 2,
				'top': 100
			});
			break;
		case 'about':
			var myMove = new Fx.Styles($(el), {
				duration: 200,
				transition: Fx.Transitions.Quad.easeIn
			});
			myMove.start({
				'left': $('portfolio').getPosition().x,
				'top': 305
			});
			break;
		case 'LATESTTOPICS':
			var myMove = new Fx.Styles($(el), {
				duration: 200,
				transition: Fx.Transitions.Quad.easeIn
			});
			myMove.start({
				'left': $('about').getPosition().x + $('about').getCoordinates().width + 9,
				'top': 305
			});
			break;
		case 'contact':
			var myMove = new Fx.Styles($(el), {
				duration: 200,
				transition: Fx.Transitions.Quad.easeIn
			});
			myMove.start({
				'left': $('LATESTTOPICS').getPosition().x + $('LATESTTOPICS').getCoordinates().width + 9,
				'top': 305
			});
			break;
		case 'NAMEBAR':
			var myMove = new Fx.Styles($(el), {
				duration: 200,
				transition: Fx.Transitions.Quad.easeIn
			});
			myMove.start({
				'left': $('portfolio').getPosition().x,
				'top': 66
			});
			break;
	}
}

function getPortfolioImage(id, image, start, ext, mediaUrl) {
	if(start > 0) {
		var plus = '&start='+start;
	} else {
		var plus = '';
	}
	var url = 'mods/portfolio/content.php?id='+id+'&image='+image+plus;
	new Ajax(url, {
		method: 'get',
		update: $('replace_portfolio'),
		onComplete: function() {
			if( ext == 'flv' ) {
				var so = new SWFObject( "./flash/FLVPlayerPortfolio.swf?color=#fe4d6a&mediaUrl="+mediaUrl, "FLVPlayer", "908", "338", "9", "#eeeeee" );
				so.addParam( "wmode", "transparent" );
				//so.addVariable( "color", "#fe4d6a" );
				//so.addVariable( "mediaUrl", mediaUrl );
				so.write( "flvContentPortfolio" );
			}
		}
	}).request();
}

function getPortfolioFlash(id, image, all, mediaUrl, e) {
	console.log( 'test' )
	if (e) {
		new Event(e).stop();
	}
	if(all == 'true') {
		var plus = '&all=all';
	} else {
		var plus = '';
	}
	var url = 'mods/portfolio/content.php?id='+id+'&image='+image+plus;
	new Ajax(url, {
		method: 'get',
		update : $('replace_portfolio'),
		onComplete: function(){		   
		   	var so = new SWFObject( "FLVPlayer.swf", "FLVPlayer", "908", "338", "9", "#eeeeee" );
			so.addParam( "wmode", "transparent" );
			so.addVariable( "color", "#fe4d6a" );
			so.addVariable( "mediaUrl", mediaUrl );
			so.write( "flvContentPortfolio" );
		}
	}).request();
}
