(function($) {
	var imgList = [];
	$.extend({
		preload: function(imgArr, option) {
			var setting = $.extend({
				init: function(loaded, total) {},
				loaded: function(img, loaded, total) {},
				loaded_all: function(loaded, total) {}
			}, option);
			var total = imgArr.length;
			var loaded = 0;
			
			setting.init(0, total);
			for(var i in imgArr) {
				imgList.push($("<img />")
					.attr("src", imgArr[i])
					.load(function() {
						loaded++;
						setting.loaded(this, loaded, total);
						if(loaded == total) {
							setting.loaded_all(loaded, total);
						}
					})
				);
			}
			
		}
	});
})(jQuery);

SITE = {
};

// video ration is 0.5625
SITE.CONSTS = {
	pathImages: 'images/page_bg/',
	speedFadeIn: 500,
	speedFadeOut: 250,
	speedGroup: 750,
	videoHeight: 283.5,
	videoWidth:504
};

SITE.VARS = {
	backgroundShowing: 0,
	contentChanging: 0,	
	contentShowing: '',
	contentToShow: '',
	heightBackground:960,
	heightInitial:0,
	heightNew:0,
	loading: 1,
	timeoutResizing:false,
	widthBackground:1280,
	widthInitial:0,
	widthNew:0
};

$(function() {
$.preload([
	SITE.CONSTS.pathImages + 'home.jpg',
	SITE.CONSTS.pathImages + 'digital.jpg',
	SITE.CONSTS.pathImages + 'motion.jpg',
	SITE.CONSTS.pathImages + 'systems.jpg',
	SITE.CONSTS.pathImages + 'about.jpg',
	SITE.CONSTS.pathImages + 'contact.jpg'
    ],
	{
		init: function(loaded, total) {
			$('#loading-img').css({display:'inline'});
		},
		loaded: function(img, loaded, total) {
		},
		loaded_all: function(loaded, total) {
			$('#loading-img').css({display:'none'});
			$('#content-wrapper').css({visibility:'visible'});
			SITE.VARS.widthInitial = $(window).width();
			SITE.VARS.heightInitial = $(window).height();
			LIBRARY.addTargetAttribute('targetBlank', '_blank', textTitleBlank);
			//LIBRARY.prepopulateFieldsText('fieldPrepopulate_');
			//LIBRARY.bindFieldsRequired('fieldPrepopulate_', 'fieldRequired', '#f5762b', '#666666');
			SITE.VARS.contentToShow = pageStart;
			//alert(SITE.VARS.contentToShow)
			SITE.CONTENT.changeBackground();
			SITE.CONTENT.contentToggle();
			SITE.CONTENT.contentControlsBind();
			if ($(".popupTrigger").size() > 0) {
				$(".popupTrigger").colorbox({width:"660px", inline:true, href:"#popupInside"});
			}
			SITE.VARS.loading = 0;
		}
	});
});

$(document).ready(function() {
});

$(window).load(function() {
});

$(window).resize(function() {
	if (SITE.VARS.loading == 0) {
		SITE.VARS.widthNew = $(window).width();
		SITE.VARS.heightNew = $(window).height();
		if(SITE.VARS.timeoutResizing != false) {
			clearTimeout(SITE.VARS.timeoutResizing);
		}
		if ((SITE.VARS.widthNew != SITE.VARS.widthInitial) || (SITE.VARS.heightNew != SITE.VARS.heightInitial)) {
			SITE.VARS.timeoutResizing = setTimeout('SITE.CONTENT.footerResize()', 200);
		}
	}
});

// *******************************************************************************************

SITE.GENERIC = {
	pageTrack: function(thePage) {
		_gaq.push(function(){
			var tracker = _gat._getTracker(gaAccount);
			tracker._trackPageview(thePage);
		});
	}
};

// *******************************************************************************************

SITE.CONTENT = {
	footerResize: function() {
		SITE.VARS.widthInitial = SITE.VARS.widthNew;
		SITE.VARS.heightInitial = SITE.VARS.heightNew;
		SITE.CONTENT.contentShow();
	},

	footerPosition: function() {
		var browserDimensions = LIBRARY.getDimensionsBrowser();
		$('.pageMain').css({
			height: 'auto'
		});
		var heightPageInside = LIBRARY.getTotalHeight($('.pageInside'));
		var heightBrowser = browserDimensions.height - 20;
		if (heightBrowser > heightPageInside) {
			$('.pageMain').css({
				height: heightBrowser - LIBRARY.getTotalHeight($('.pageFooter')) + 'px'
			});
		}
		$('.pageFooter').css({
			visibility: 'visible'
		});
	},

	contentToggle: function() {
		SITE.VARS.contentChanging = 1;		
		if (SITE.VARS.contentShowing != '') {
			SITE.CONTENT.changeBackground();
 			$('.pageInside').fadeOut(SITE.CONSTS.speedFadeOut, function() {
				$('.pageInside').removeClass('pageInside_' + SITE.VARS.contentShowing);				
				$('.contentBox').css({'display':'none'});
				$('.contentGroup').css({
					opacity:0,
					visibility:'hidden'
				});
				SITE.CONTENT.contentShow();
			});
		} else if (SITE.VARS.contentToShow != '') {
			SITE.CONTENT.menuActivate();
			SITE.CONTENT.contentShow();
		}
	},

	contentShow: function() {
		$('.pageInside').addClass('pageInside_' + SITE.VARS.contentToShow);
		$('#content_' + SITE.VARS.contentToShow).css({'display':'inline'});
		$('.pageInside').fadeIn(SITE.CONSTS.speedFadeIn, function() {
			var countGroup = 0;
			$('#content_' + SITE.VARS.contentToShow + ' .contentGroup').each(function() {
				var eachElement = $(this);
				eachElement.css({
					opacity:0,					
					visibility:'visible'
				});
				setTimeout(function(){SITE.CONTENT.groupShow(eachElement)},(countGroup * SITE.CONSTS.speedGroup));
				countGroup++;
			})

			if (SITE.VARS.contentToShow == 'motion') {
				if (SITE.VARS.contentShowing != 'motion') {
					SITE.CONTENT.videoShow();
				}
			} else if (SITE.VARS.contentShowing == 'motion') {
				SITE.CONTENT.videoHide();
			}
			SITE.VARS.contentShowing = SITE.VARS.contentToShow;
			SITE.VARS.contentChanging = 0;
			SITE.CONTENT.footerPosition();
		});
	},

	groupShow: function(theGroup) {
		theGroup.animate({
			opacity:1
			}, SITE.CONSTS.speedFadeIn);
	},

	contentControlsBind: function() {
		$('.contentControls a, a.contentControls').unbind('click');
		$('.contentControls a, a.contentControls').bind('click', function() {
			var theElement = $(this);
			if (SITE.VARS.contentChanging == 0) {
				SITE.VARS.contentToShow = theElement.attr('href').substr(parseInt(theElement.attr('href').indexOf('_')) + 1);
				SITE.GENERIC.pageTrack('/' + pageLanguage + '/' + SITE.VARS.contentToShow);
				SITE.CONTENT.menuActivate();
				SITE.CONTENT.contentToggle();
			}
			return false;
		});
	},

	menuActivate: function() {
		$('.contentControls li').removeClass('active');
		$('.contentControls li a').each(function() {
			var eachElement = $(this);
			if (eachElement.attr('href') == '#content_' + SITE.VARS.contentToShow) {
				eachElement.parent().addClass('active');
			}
		});
	},

	videoShow: function() {
		var htmlIframe = '<iframe src="' + videoSrc + '" width="' + SITE.CONSTS.videoWidth + '" height="' + SITE.CONSTS.videoHeight + '" frameborder="0"></iframe>'
		$('#content_motion .contentRight .contentGroup').html(htmlIframe);
	},

	videoHide: function() {
		$('#content_motion .contentRight iframe').remove();
	},

	changeBackground: function() {
		$.fn.supersized.options = {
			startwidth: SITE.CONSTS.widthBackground,
			startheight: SITE.CONSTS.heightBackground,
			vertical_center: 1,
			slides : [
				{ image: SITE.CONSTS.pathImages + SITE.VARS.contentToShow + '.jpg' }
			]
		};

		if (SITE.VARS.backgroundShowing == 1) {
			$('#supersized').fadeOut(SITE.CONSTS.speedFadeOut, function() {
				$('#supersized').html('');
				$('#supersized').supersized();
				$('#supersized').fadeIn(SITE.CONSTS.speedFadeIn);
				$('#supersized').resizenow();
			});
		} else {
			$('#supersized').supersized();
			if ($('#supersized').css('display').toLowerCase() == 'none') {
				$('#supersized').css({
					'display':'block'
				});
			}
			SITE.VARS.backgroundShowing = 1;
		}
	}
};
// *******************************************************************************************

SITE.FORMS = {

};

// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************

