/*
 (var)		ie6
 (var)		log - Safe console.log.
 (proxy)	_l() -> log
 (class)	TabSet -> Renamed from Tabs to avoid name conflict in Backoffice
 (class) 	GalleryNavigator
 (function)	showVideo
*/

var ie6 = (Browser.Engine.name=="trident" && Browser.Engine.version=="4");
var log = function(msg) { try{ console.log(msg)} catch(e) {} };
var _l = log;

var TabSet = new Class({
	initialize: function(group){
		this.tabs = $("tabs").getElements("ul."+group+" li");
		this.panels = $("tabs-content").getElements("div."+group+" div.tab-content");
		this.panels.removeClass("active");
		if ($defined(this.panels[0])) this.panels[0].addClass("active");
		if ($defined(this.tabs[0])) this.tabs[0].addClass("active");
		this.addEvents();
	},
	addEvents: function(){
		this.tabs.addEvent('click', this.setActive.bind(this));
	},
	setActive: function(event){
		var target = (event.target.tagName == "A")? event.target:event.target.getElement("a");
		var tabName = target.getAttribute("rel");
		this.panels.removeClass("active");
		this.tabs.removeClass("active");
		$$($(tabName), target.getParent("li")).addClass("active");
	}
});

var GalleryNavigator = new Class({
	initialize: function(element) {
		this.element = $(element);
		if (!this.element) return;
		this.groups = this.element.getElements(".group");
		this.window = this.element.getElement(".window");
		this.slider = this.element.getElement(".slider");
		this.show = 0;
		this.groupWidth = undefined;
		this.animating = false;
		
		this.hideExtra();
		this.setEvents();
		this.updateButtons();
	},
	hideExtra: function(element) {
		this.groups.setStyle("float","left");
		this.groupWidth = this.groups[0].getStyle("width").toInt();
		this.window.setStyles()
	},
	setEvents: function() {
		this.element.getElement("a.back").addEvent("click", this.paginateBack.bind(this));
		this.element.getElement("a.fw").addEvent("click", this.paginateFw.bind(this));
		if ($("gallery_active")) {
			this.slider.getElements("img").addEvent("click", function(e){
				var src = e.target.src;
				if (src.match(/\/thumbs/))
					$("gallery_active").src = src.replace(/\/thumbs/, "");
				else
					$("gallery_active").src = src.replace(/\/thumb/, "/medium");
			});
		}
	},
	paginateBack: function() {
		if (this.show <= 0)
			return;
		this.paginate(1);
	},
	paginateFw: function() {
		if (this.show >= this.groups.length-1)
			return;
		this.paginate(-1);
	},
	paginate: function(direction) {
		if (this.animating)
			return;
		this.show = this.show + direction*-1;
		this.animating = true;
		var margin, fx;
		margin = this.slider.getStyle("margin-left").toInt();
		fx = new Fx.Tween(this.slider);
		fx.addEvent("complete", function(){
			this.animating=false;
			this.updateButtons();
		}.bind(this));
		fx.start("margin-left", margin, margin + direction * this.groupWidth);
	},
	updateButtons: function(){
		var fw = (this.show >= this.groups.length-1)? 0.4:1;
		var bw = (this.show <= 0)? 0.4:1;
		this.element.getElement("a.fw").set("opacity", fw);
		this.element.getElement("a.back").set("opacity", bw);
	}
});

function showVideo(videofile, element, flashvars, params) {
	_l("showVideo", videofile);
	//if (!$(element)) element = "mediaspace";
	//element = $(element);
	//flashvars = flashvars || {
	//	file: videofile, stretching: 'fill', smoothing: false,
	//	lightcolor: "0D70B3", backcolor: "000000", frontcolor: "505050",
	//	controlbar: "none", volume: 0 };
	//params = params || {allowfullscreen: 'true', wmode: 'opaque'};
	//swfobject.embedSWF('/mediaplayer/player.swf', "mediaspace", '272', '176', '9.0.0', '/js/swfobject/expressInstall.swf', flashvars, params);	
}
