/*
* Portfolio
* @Author: Alexander Gavazov
* @Site: www.studio.bg
*/


var Portfolio = function(list, text, details, videoLabel, photoLabel) {
	this.list = list;
	this.text = text;
	this.details = details;

	this.listIsFaded = false;

	this.oldObject = null;
	this.oldText = null;
	this.ajaxId = 0;

	this.currentPhoto = 0;

	this.videoLabel = videoLabel || 'Видео';
	this.photoLabel = photoLabel || 'Снимки';

	this.setBehaviour();
}

Portfolio.prototype.setBehaviour = function() {
	// List
	var zIndex = 9999;
	this.list.select('li').each(function(node) {
		var tip = node.select('.tip')[0];
		tip.hide();

		node.observe('mouseover', tip.show.bind(tip));
		node.observe('mouseout', tip.hide.bind(tip));

		node.observe('click', Event.stop);
		node.observe('click', this.showProject.bind(this, node));

		node.style.zIndex = zIndex;
		zIndex--;
	}.bind(this));


	// Hide images
	this.list.select('img').each(function(node) {
		node.setOpacity(0);
	}.bind(this));


	// Text
	this.text.select('.p_text').each(function(node) {
		node.setOpacity(0);
		node.hide();
	}.bind(this));
	$('pf_intro').show();
	$('pf_intro').setOpacity(1);
}

Portfolio.prototype.showProject = function(node) {
	$('pf_intro').hide();

	node.addClassName('active');
	var id = node.select('a')[0].href.replace(/.*#/gi, '');
	ajaxId = id.replace(/.*_/gi, '');
	var text = $(id);

	if (ajaxId == this.ajaxId) {
		return;
	}

	if (this.details) {
		this.details.update(null);
	}

	this.ajaxId = ajaxId;

	if (!this.listIsFaded) {
		this.fadeOutList();
		this.listIsFaded = true;
	}

	if (this.oldObject) {
		node.removeClassName('active');
		this.fade(this.oldObject.select('img')[0], 0);

		this.oldText.setOpacity(0);
		this.oldText.hide();
		setTimeout(this.oldText.setOpacity.bind(this.oldText, 0), 500);
	}

	this.fade(node.select('img')[0], 1);
	text.show();
	this.fade(text, 1);

	this.fixScroll();

	this.getData();

	this.oldObject = node;
	this.oldText = text;
}

Portfolio.prototype.fadeOutList = function() {
	this.list.select('img').each(function(node) {
		if (node.parentNode.parentNode.className != 'active') {
			this.fade(node, 0);
		}
	}.bind(this));
}

Portfolio.prototype.fade = function(node, fadeTo) {
	if (node.Effect) {
		node.Effect.cancel();
	}

	node.Effect = new Effect.Opacity(node, {to: fadeTo, duration: .5});
}

Portfolio.prototype.getData = function() {
	var id = this.ajaxId;
	var url = location.href + '/get_json/' + id;

	new Ajax.Request(url, {
	  method: 'post',
	  onSuccess: this.getResponse.bind(this, id)
	});
}

Portfolio.prototype.getResponse = function(id, transport) {
	if (id != this.ajaxId) {
		return;
	}

	var data = transport.headerJSON;

	if (data.photos[0]) {
		this.createGallery(data.photos, true);
	}

	if (data.video[0]) {
		var playNow = data.photos[0] ? false : true;
		this.createVideo(data.video, playNow);
	}

	if (data.video[0] && data.photos[0]) {
		this.createSwitchLinks();
		this.switchTo('photo');
	}
}


Portfolio.prototype.fixScroll = function() {
	setScroll();
	$('scrollbar').style.left = '495px';
	$('scrollbar').style.top = '185px';
	$('scrollbar').style.height = '445px';
	$('scroll_conatiner').style.top = '0';
}

////

Portfolio.prototype.createGallery = function(photos, show) {
	this.pContainer = new Element('div', {id: 'photos'});

	var phoWrapper = new Element('div').addClassName('pho_wrapper');
	this.pContainer.insert(phoWrapper);

	this.details.insert(this.pContainer);

	this.photoGallery = [];

	for (var i = 0; i < photos.length; i++) {
		this.photoGallery[i] = new Element('img', {src: photos[i]});
		this.photoGallery[i].setOpacity(0);
		this.photoGallery[i].hide();

		phoWrapper.insert(this.photoGallery[i]);
	}

	// Create controlls
	this.currentPhoto = 0;

	var next = new Element('div').addClassName('next_control');
	var prev = new Element('div').addClassName('prev_control');
	var count = new Element('div').addClassName('count_control');
	this.pContainer.insert(next);
	this.pContainer.insert(prev);
	this.pContainer.insert(count);

	this.photoCount = count;
	next.observe('click', this.showPhoto.bind(this, 'next'));
	prev.observe('click', this.showPhoto.bind(this, 'prev'));

	if (this.photoGallery.length == 1) {
		next.hide();
		prev.hide();
		count.hide();
	}

	if (!show) {
		this.hidePhotoContainer();
	} else {
		this.showPhotoContainer();
	}
}

Portfolio.prototype.showPhoto = function(action) {
	var pId = 'xxx';

	if (action == 'next') {
		if (this.currentPhoto + 1 >= this.photoGallery.length) {
			pId = 0;
		} else {
			pId = this.currentPhoto + 1;
		}
	} else if (action == 'prev') {
		if (this.currentPhoto <= 0) {
			pId = this.photoGallery.length - 1;
		} else {
			pId = this.currentPhoto - 1;
		}
	}

	this.photoCount.update((pId + 1)  + ' / ' + (this.photoGallery.length));

	// Old
	if (this.photoGallery[this.currentPhoto]) {
		var oldPhoto = this.photoGallery[this.currentPhoto];
		oldPhoto.setOpacity(0);
		oldPhoto.hide();
	}

	// New
	if (this.photoGallery[pId]) {
		this.photoGallery[pId].show();
		this.fade(this.photoGallery[pId], 1);
	}

	this.currentPhoto = pId;
}

Portfolio.prototype.hidePhotoContainer = function() {
	this.pContainer.hide();
}

Portfolio.prototype.showPhotoContainer = function() {
	this.pContainer.show();
	this.currentPhoto = -1;
	this.showPhoto('next');
}

Portfolio.prototype.createSwitchLinks = function() {
	this.photosLink = new Element('li').update(this.photoLabel);
	this.videosLink = new Element('li').update(this.videoLabel);
	var ul = new Element('ul').addClassName('switch_links');

	ul.insert(this.photosLink);
	ul.insert(this.videosLink);

	this.photosLink.observe('click', this.switchTo.bind(this, 'photo'));
	this.videosLink.observe('click', this.switchTo.bind(this, 'video'));

	this.details.insert(ul);
}

Portfolio.prototype.switchTo = function(action) {
	if (action == 'photo') {
		this.pContainer.show();
		this.videoContaner.hide();

		this.photosLink.addClassName('active');
		this.videosLink.removeClassName('active');

		this.videoPlayer.update(null);
	} else if (action == 'video') {
		this.pContainer.hide();
		this.videoContaner.show();

		this.photosLink.removeClassName('active');
		this.videosLink.addClassName('active');

		this.createVideoSource();
	}
}

Portfolio.prototype.createVideo = function(video, playNow) {
	this.videos = video;
	this.videoContaner = new Element('div').addClassName('videos');
	this.videoPlayer = new Element('div');
	this.details.insert(this.videoContaner);
	this.details.insert(this.videoPlayer);

	// Create controlls
	this.currentVideo = 0;

	if (this.videos.length > 1) {
		var next = new Element('div').addClassName('next_control');
		var prev = new Element('div').addClassName('prev_control');
		var count = new Element('div').addClassName('count_control');
		this.videoContaner.insert(next);
		this.videoContaner.insert(prev);
		this.videoContaner.insert(count);

		this.videoCount = count;
		this.videoCount.update(1  + ' / ' + this.videos.length);

		next.observe('click', this.showVideo.bind(this, 'next'));
		prev.observe('click', this.showVideo.bind(this, 'prev'));
	}

	if (playNow) {
		this.createVideoSource();
	}
}

Portfolio.prototype.showVideo = function(action) {
	var vId = 'xxx';

	if (action == 'next') {
		if (this.currentVideo + 1 >= this.videos.length) {
			vId = 0;
		} else {
			vId = this.currentVideo + 1;
		}
	} else if (action == 'prev') {
		if (this.currentVideo <= 0) {
			vId = this.videos.length - 1;
		} else {
			vId = this.currentVideo - 1;
		}
	}

	this.videoCount.update((vId + 1)  + ' / ' + (this.videos.length));

	this.currentVideo = vId;

	this.createVideoSource(vId);
}

Portfolio.prototype.createVideoSource = function() {
	//this.videoPlayer.update(null);
	this.videoPlayer.update('Flash player 9 needed to view content. <a href="http://www.adobe.com/products/flashplayer/" target="_blank">Download here</a>');
	//this.videoPlayer.insert(this.videos[this.currentVideo]);

	var so = new SWFObject(imgPath + "player.swf","ply","360","260","9","#FFFFFF");
	so.addParam('allowfullscreen','true');
	so.addParam("flashvars","autostart=true&displayclick=none&skin=" + imgPath + "player_thin.swf&file=" + this.videos[this.currentVideo]);
	so.write(this.videoPlayer);
}