/**
 * The GPX object v1.1
 */

// init
var gpx = gpx || {};
gpx.swf = {};
gpx.fx = {};
gpx.fx.to = {};
gpx.fn = {}

// defaults
gpx.swf.defaults = {
	minVersion: '9.0.0',
	expressInstallSWF: '/&res/swf/expressInstall.swf'
};

// SWF
(function() {
	if(document.getElementById('tagcloudflash')) {
		var embedTagCloud = function() {
			this.id = 'tagcloudflash';
			this.swf = '/&res/swf/tagcloud.swf?'+(new Date()).getTime();
			this.width = '100%';
			this.height = '100%';
			this.flashvars = {};
			this.flashvars = {
				tcolorS1: '0x448800',
				tcolorS2: '0x225500',
				tcolorS4: '0x114400',
				tcolorS5: '0x002200',
				tcolorS6: '0x000000',
				tcolorSX: '0x000000',
				hoverC1: '0xFFFFFF',
				hoverC2: '0xFFFFFF',
				hoverC3: '0xFFFFFF',
				hoverC4: '0xFFFFFF',
				hoverC5: '0xFFFFFF',
				hoverC6: '0xFFFFFF',
				hcolorS6: '0xFFFFFF',
				hicolor: '0x555555',
				mode: 'both',
				radius: '180',
				trans: 'true',
				distr: 'true',
				tspeed: '60'
				//tagcloud: "<tags>"+$('#'+this.id+' > p.tags').html().replace(/\?/g, "%3F").replace(/&amp;/g, "%26")+"</tags>"
				//tagcloud: ("<tags>"+$('#'+this.id+' > p.tags').html()+"</tags>")
			};
			if(document.location.host.indexOf('preview') != -1) {
				this.flashvars.xmlpath = escape(document.location.href+'&debug=false&avpage-views=tagcloud');
			}
			else {
				this.flashvars.xmlpath = escape(document.location.href+'?avpage-views=tagcloud');
			}
			this.params = {
				wmode: 'transparent'
			};
			this.attributes = {
				id: 'tagcloudflash'
			};
			swfobject.embedSWF(this.swf, this.id, this.width, this.height, gpx.swf.defaults.minVersion, gpx.swf.defaults.expressInstallSWF, this.flashvars, this.params, this.attributes);
			return true;
		};
		embedTagCloud();
	}
})();

// Tweaks
(function() {
	var inputToggleDefaultValue = function() {
		$('input.toogleDefaultValue').each(function(i, el) {
			var defaultValue = this.value;
			$(this).bind('focus', function(e) {
				if(this.value == defaultValue) {
					this.value = '';
				}
			})
			.bind('blur', function(e) {
				if(this.value.length == 0) {
					this.value = defaultValue;
				}
			});
		});
	};
	inputToggleDefaultValue();
	
	if($('body').hasClass('article')) {
		var externalLinksBlank = function() {
			$('#observations a').each(function(i, el) {
				if(this.href.indexOf(document.location.hostname) == -1) {
					$(this).attr('target', '_blank');
				}
			});
			$('#box1079 a').each(function(i, el) {
				$(this).attr('target', '_blank');
			});
		};
		externalLinksBlank();
	}

	/*if($('body').hasClass('article') && $('body').hasClass('clients')) {
		var articleAjaxNav = function() {
			$('#related > ul.articles > li').each(function(i, el) {
				$('.info > h2 > a, .icon > a', el).bind('click', function(e) {
					$('#presentation > h1').text(' ');
					$('#presentation > p').text(' ');
					$('#article').empty().append(document.createTextNode('Loading...'));
					document.location.hash = 'articleTitle';
					$.ajax({
						async: true,
						url: this.href.indexOf('preview') != -1 ? this.href+'&debug=false' : this.href,
						success: function (d, s) {
							var page = $('<div/>').append(d.replace(/[\r\n]+/g, "").replace(/<script[^>]*>(.*?)<\/script>/g, ""));
							$('#presentation > h1').text($('#presentation > h1', page).text());
							$('#presentation > p').text($('#presentation > p', page).text());
							$('#article').html($('#article', page).html());
							gpx.fn.articleSlideShow();
						}
					});
					e.preventDefault();
				});
			});
		};
	};*/
})();

// FX
(function() {
	var mainMenuFX = function() {
		gpx.fx.id_sections = {
			slideDown: 200,
			slideUp: 400,
			trigger: 150
		};
		$('#sections > ul > li').mouseover(function (e) {
			if(this.id == 'tools') {
				return true;
			}
			gpx.fx.to.down = setTimeout(function() {
				$('#sections > ul > li > ul').each(function (i, el) {
					if($(this).is(':hidden')) {
						$(this).slideDown(gpx.fx.id_sections.slideDown);
					}
				});
			}, gpx.fx.id_sections.trigger);
			clearTimeout(gpx.fx.to.up);
		}).mouseout(function (e) {
			if(this.id == 'tools') {
				return true;
			}
			gpx.fx.to.up = setTimeout(function() {
				$('#sections > ul > li > ul').each(function (i, el) {
					if($(this).is(':visible')) {
						$(this).slideUp(gpx.fx.id_sections.slideUp);
					}
				});
			}, gpx.fx.id_sections.trigger);
			clearTimeout(gpx.fx.to.down);
		});
	};
	mainMenuFX();

	var test = gpx.fn.doSlideShowNav = function(targeElement, prevLabel, nextLabel) {
		var previous_label = ( $('body').hasClass('en') ? 'previous' : 'înapoi');
		var next_label = ( $('body').hasClass('en') ? 'next' : 'înainte');		
		this.prevLabel = prevLabel || previous_label;
		this.nextLabel = nextLabel || next_label;
		var t, div, a, txt;
		t = targeElement || '#slideshow';
		div = document.createElement('div');
		div.className = 'controller js-controller';
		a = document.createElement('a');
		txt = document.createTextNode(this.prevLabel);
		a.appendChild(txt);
		a.className = 'prev';
		a.href = '#';
		a.title = this.prevLabel;
		div.appendChild(a);
		a = document.createElement('a');
		txt = document.createTextNode(this.nextLabel);
		a.appendChild(txt);
		a.className = 'next';
		a.href = '#';
		a.title = this.nextLabel;
		div.appendChild(a);
		$(t).prepend(div);
		div = a = txt = null;
	}
	
	var articleSlideShow = gpx.fn.articleSlideShow = function() {
		if(document.getElementById('slideshow') && $('#slideshow > ul').children().size() > 1) {
			gpx.fx.SScontroller = {
				trigger: 150
			};
			gpx.fn.doSlideShowNav('#slideshow');
			$('.js-controller').hide();
			$('#slideshow').bind('mouseover', function(e) {
				gpx.fx.SScontroller.toIn = setTimeout(function() {
					$('.js-controller').fadeIn(300);
				}, gpx.fx.SScontroller.trigger);
				clearTimeout(gpx.fx.SScontroller.toOut);
			})
			.bind('mouseout', function(e) {
				gpx.fx.SScontroller.toOut = setTimeout(function() {
					$('.js-controller').fadeOut(300);
				}, gpx.fx.SScontroller.trigger);
				clearTimeout(gpx.fx.SScontroller.toIn);
			});
			$(function() {
				$('#slideshow > ul').cycle({
					fx: 'scrollHorz',
					timeout: 10000,
					next: '.js-controller > a.next',
					prev: '.js-controller > a.prev'
				});
			});
			return true;
		}
		else {
			return false;
		}
	};

	if(document.getElementById('person')) {
			$('#person').slideDown(800, function() {
				/**
				 * articleSlideshow must be called here.
				 */
				articleSlideShow();

				var t = 850;
				var m = $('#message');
				m.parent().fadeIn(t);
				m.children().eq(0).fadeIn(t, function() {
					if(m.children().size() > 1) {
						/*m.cycle({
							fx: 'fade',
							timeout: 10000,
							height: 'auto',
							pause: 1
						});*/
						gpx.fx.person = {};
						gpx.fx.person.toFade = setInterval(function() {
							$('#message > p:visible').fadeOut(200, function() {
								if($(this).next().size() > 0) {
									$(this).next().fadeIn(300);	
								}
								else {
									$(this).parent().children(':first').fadeIn(300);
								}
							});
						}, 10000);
					}
				});
				$('#photo').hide().css({visibility:'visible'}).fadeIn(t);
			});
		//});
	}

    $('#gallery a').lightBox();
})();