var fi = {};

var Work = Class.create({
	
	initialize: function() {
		
		this.sortToggleBtn		= $$('#work_navigation_sort a span')[0];
		this.sortToggleList		= $('work_navigation_sort');
		this.sortList			= $('sortOptions');
		this.sortListVisible	= false;
		this.sortButtons		= $$('ul#sortOptions a');
		this.expandedBtn		= $('work_view_expanded');
		this.listedBtn			= $('work_view_listed');
		this.expandedDiv		= $('work_expanded_view');
		this.listedDiv			= $('work_list_view');
		this.sortType			= 0;
		this.viewType			= 0;
		this.sectionCounter		= 0;
		this.timerRunning		= false;
		
		this.addListeners();
		
		/*if (window.videoExists == 1) {
			this.createMainVideo();
		}*/
		
	}, 
	
	toggleSortOptions: function(){
		
		var mover;
		this.sortListVisible = !this.sortListVisible;
		
		if (this.sortListVisible === true) {
			this.sortToggleList.addClassName('selected');
			this.sortList.style.left = '-60px';
			mover = new Effect.Move(this.sortList, {
				x: 0 + window.sortOffset,
				duration: 0.2,
				transition: Effect.Transitions.easeOutQuart,
				limit: 1,
				mode: 'absolute'
			});
		}
		else {
			this.sortToggleList.removeClassName('selected');
			mover = new Effect.Move(this.sortList, {
				x: -60,
				duration: 0.2,
				transition: Effect.Transitions.easeInQuart,
				limit: 1,
				mode: 'absolute', 
				afterFinish: function(){
					this.sortList.style.left = '-10000px';
				}.bind(this)
			});
		}
		
	},
	
	sortItems: function(btnIndex){
		var ajaxReq;
		var selected = this.sortButtons[btnIndex];
		
		this.sortButtons.each(function(item){
			item.removeClassName('selected');
		}.bind(this));
		selected.addClassName('selected');
		
		switch (btnIndex) {
			case 0: 
				ajaxReq = new Ajax.Request('/work_view_update.php', {method: 'post', postBody: 'sortMode=release_date'});
				this.massHideContent();
				this.loadData('date', window.category);
				break;
			case 1: 
				ajaxReq = new Ajax.Request('/work_view_update.php', {method: 'post', postBody: 'sortMode=cat_name'});
				this.massHideContent();
				this.loadData('category', window.category);
				break;
			case 2: 
				ajaxReq = new Ajax.Request('/work_view_update.php', {method: 'post', postBody: 'sortMode=client_name'});
				this.massHideContent();
				this.loadData('client', window.category);
				break;
		}
		
	},
	
	loadData: function(sortType, catType){
		
		var sender = new Ajax.Request('/work_sort.php', {
			method: 'get',
			parameters: {method: sortType, categories_id: catType}, 
			onSuccess: function(transport){
				this.refreshData(transport.responseText.evalJSON());
			}.bind(this)
		});
		
	},
	
	refreshData: function(inputJSON){
		
		var exp_title			= $$('div.expanded_details h2 a');
		var exp_icon			= $$('div.expanded_details img.icon');
		var exp_category		= $$('div.expanded_details a.workCategory');
		var exp_date			= $$('div.expanded_details span.releaseDate');
		var exp_description		= $$('div.expanded_details h2+p');
		var exp_link			= $$('div.expanded_details a.l4');
		var exp_ep				= $$('img.smallEP');
		var exp_ep_link			= $$('a.smallEPLink')
		var exp_icon_link		= $$('a.iconLinkExp');
		
		var list_title			= $$('div.listed_details h2 a');
		var list_icon			= $$('div.listed_details img.icon');
		var list_category		= $$('div.listed_details a.workCategory');
		var list_date			= $$('div.listed_details span.releaseDate');
		var list_description	= $$('div.listed_details h2+p');
		var list_link			= $$('div.listed_details a.l4');
		var list_icon_link		= $$('a.iconLinkList');
		
		var i = 0;
		var size = exp_title.length;
		var curr_category;
		for (i = 0; i < size; i++) {
			
			exp_icon[i].writeAttribute('src', inputJSON.media_path + 'works-' + inputJSON.works[i].works_id + '/' + inputJSON.works[i].works_img_work_logo);
			list_icon[i].writeAttribute('src', inputJSON.media_path + 'works-' + inputJSON.works[i].works_id + '/' + inputJSON.works[i].works_img_work_logo_centered);
			exp_ep[i].writeAttribute('src', inputJSON.media_path + 'works-' + inputJSON.works[i].works_id + '/' + inputJSON.works[i].works_img_ep_small);
			
			
			
			curr_category = inputJSON.works[i].categories_txt_name;
			
			exp_ep_link[i].writeAttribute('href', inputJSON.works[i].works_url);
			exp_icon_link[i].writeAttribute('href', inputJSON.works[i].works_url);
			list_icon_link[i].writeAttribute('href', inputJSON.works[i].works_url);
			exp_title[i].writeAttribute('href', inputJSON.works[i].works_url);
			list_title[i].writeAttribute('href', inputJSON.works[i].works_url);
			exp_link[i].writeAttribute('href', inputJSON.works[i].works_url);
			list_link[i].writeAttribute('href', inputJSON.works[i].works_url);
			
			exp_title[i].update(inputJSON.works[i].works_txt_title);
			list_title[i].update(inputJSON.works[i].works_txt_title);
			
			exp_date[i].update(inputJSON.works[i].formatted_date);
			list_date[i].update(inputJSON.works[i].formatted_date);
			
			if (exp_category.length > 0) {
				exp_category[i].update(curr_category);
				list_category[i].update(curr_category);
				
				if (curr_category == 'Application') {
					exp_category[i].writeAttribute('href', '/applications');
					list_category[i].writeAttribute('href', '/applications');
				}
				else if (curr_category == 'Microsite') {
						exp_category[i].writeAttribute('href', '/microsites');
						list_category[i].writeAttribute('href', '/microsites');
				}
				else if (curr_category == 'Portal') {
					exp_category[i].writeAttribute('href', '/portals');
					list_category[i].writeAttribute('href', '/portals');
				}
				else {
					exp_category[i].writeAttribute('href', '/work');
					list_category[i].writeAttribute('href', '/work');
				}
				
			}
			
			exp_description[i].update(inputJSON.works[i].works_txt_short_desc);
			list_description[i].update(inputJSON.works[i].works_txt_short_desc);
			
		}
		
		if (this.timerRunning === true) {
			this.massHideContent();
			this.sectionCounter = 0;
			this.interval.stop();
		}
		
		this.interval = new PeriodicalExecuter(this.stepShowContent.bind(this), 0.05);
		this.timerRunning = true;
	},
	
	massHideContent: function() {
		
		$$('div.spanbox').each(function(item){
			item.style.visibility = 'hidden';
		});
		
		$$('div#work_list_view div.spanbox').each(function(item){
			item.style.visibility = 'hidden';
		});
		
	}, 
	
	stepShowContent: function() {
		
		$$('div.spanbox')[this.sectionCounter].style.visibility = 'visible';
		
		this.sectionCounter++;
		
		if (this.sectionCounter >= $$('div.spanbox').length) {
			this.interval.stop();
			this.sectionCounter = 0;
			this.timerRunning = false;
		}
		
	},
	
	createMainVideo: function(){
		
		var so = new SWFObject('swf/videoPlayer.swf', 'videoMain', '672', '275', '0', '#000000');
		var fv = $H(window.projectFlashVars);
		
		fv.each(function(item){
			so.addVariable(item.key, item.value);
		});
		
		so.write($('feature'));
		
	},
	
	createVideos: function(flashVarsData){
		
		var players = $$('div.smallVideo');
		
		players.each(function(item, index){
			
			var so = new SWFObject('swf/videoPlayerSmall.swf', 'videoSmall' + index, '428', '219', '0', '#000000');
			var fv = $H(flashVarsData[index].flashVars);
			
			fv.each(function(item){
				so.addVariable(item.key, item.value);
			}.bind(this));
		
			so.write(item);
			
		}.bind(this));
		
	},
	
	stopSmallVideos: function(){
		
		var players = $$('div.smallVideo');
		var currentPlayer;
		
		players.each(function(item, index){
			
			currentPlayer = $('videoSmall' + index);
			
			if (currentPlayer.getPlaying() === true){
				currentPlayer.stopVideo();
			}
			
		});
		
	},
	
	showExpanded: function(){
		
		this.expandedBtn.addClassName('selected');
		this.expandedDiv.removeClassName('hide');
		this.listedBtn.removeClassName('selected');
		this.listedDiv.addClassName('hide');
		
		var req = new Ajax.Request('/work_view_update.php', {method: 'post', postBody: 'viewMode=expanded'});
		
	},
	
	showListed: function(){
		
		this.expandedBtn.removeClassName('selected');
		this.expandedDiv.addClassName('hide');
		this.listedBtn.addClassName('selected');
		this.listedDiv.removeClassName('hide');
		
		var req = new Ajax.Request('/work_view_update.php', {method: 'post', postBody: 'viewMode=listed'});
		
	},
	
	addListeners: function(){
		
		Event.observe(this.sortToggleBtn, 'click', this.toggleSortOptions.bindAsEventListener(this));
		Event.observe(this.expandedBtn, 'click', this.showExpanded.bindAsEventListener(this));
		Event.observe(this.listedBtn, 'click', this.showListed.bindAsEventListener(this));
		
		this.sortButtons.each(function(item, index){
			Event.observe(item, 'click', function(){
				this.sortItems(index);
			}.bindAsEventListener(this));
		}.bind(this));
		
	}
	
});

function initpage(){
	var numOfTsEntries  = 6;
	fi.base				= new Base(numOfTsEntries);
	fi.work				= new Work();
	fi.formValidator	= new FormValidator();
}

document.observe("dom:loaded",initpage);
