function getSWF(movieName)
{
    if (navigator.appName.indexOf("Microsoft") != -1)
    {
        return window[movieName];
    }
    else
    {
        return document[movieName];
    }
}

function playClip(clip,thumb,duration){
	
	getSWF('flashVideo').playVideo(clip,thumb,"00:00");
}
	
function switchVideo(videoId, poster){
	playClip(videoId+".mp4", poster, null);
}

var Gallery = Class.extend({
		init: function(section){
			this.section = section;
			this.index = 0;
			this.next = $("#nextPic", this.section);
			this.prev = $("#prevPic", this.section);
			this.next.click(jQuery.proxy( this, "nextImage" ));
			this.prev.click(jQuery.proxy( this, "prevImage" ));
		},
		nextImage: function(){
			this.index++;
			var $a = $(".selector ul a", this.section);
			//trace("nextImage, " + $(this.section).attr("id") +", index: " + this.index + ", html:" + $($a.get(this.index)).length);
			$($a.get(this.index)).click();
		},
		
		prevImage: function(){
			this.index--;
			var $a = $(".selector ul a", this.section);
			//trace("prevImage, " + $(this.section).attr("id") +", index: " + this.index + ", html:" + $($a.get(this.index)).length);
			$($a.get(this.index)).click();
			
		},
		
		setIndex: function(value){
			this.index = value;
		}
});

var ImageSwitcher = Class.extend({
		init: function(aElm, section, index, length, gallery){
			this.aElm = aElm;
			this.index = index;
			this.length = length;
			this.section = section;
			this.gallery = gallery;
			this.imgMain = $(".imgMain", this.section);
			this.loader = $(".imgLoader", this.section);
			this.next = $("#nextPic", this.section);
			this.prev = $("#prevPic", this.section);
			aElm.click(jQuery.proxy( this, "switchImage" ));
		},
		
		switchImage:function(){
			//trace("switchImage");
			this.aElm.addClass("activo");
			
			this.gallery.index = this.index;
			
			if(this.index == 0) this.prev.css("display", "none");
			else this.prev.css("display", "block");
			
			if(this.index == this.length-1) this.next.css("display", "none");
			else this.next.css("display", "block");
			
			this.prev.css("visibility", "hidden");
			this.next.css("visibility", "hidden");
			
			//trace("len" + this.imgMain.length + ", id:"+ $(this.section).attr("id"));
			this.imgMain.animate({ 
       			opacity: 0
			}, 500, jQuery.proxy( this, "switchImageCallback" ));
			return false;
		},
		
		switchImageCallback:function(){
			this.loader.show();
			this.imgMain.attr("src", this.aElm.attr("href"));
		}

});

var ProjectSwitcher = Class.extend({
		init: function(section, a, gallery){
			this.section = section;
			this.gallery = gallery;
			this.a = a;
			this.imgMain = $(".imgMain", this.section);
			this.loader = $(".imgLoader", this.section);
			this.title = $(".descripcion h3", this.section);
			this.desc = $(".descripcion p", this.section);
			this.prev = $("#prevPic", this.section);
			this.fbSharer = $(".fbSharer", this.section);
			this.twSharer = $(".twSharer", this.section);
			
			this.sectionParam = parseUri(this.a.attr("href")).queryKey.section;
			this.projectParam = parseUri(this.a.attr("href")).queryKey.project;
			
			this.projectData = getSiteProject(siteXml, this.sectionParam.toUpperCase(), this.projectParam);
			//this.test = test;
			this.a.click(jQuery.proxy(this, "switchProject"));	
			////trace("new psw, id:"+ $(this.section).attr("id"));
		},
		
		setSharers:function(){

			this.fbSharer.attr("href", "http://www.facebook.com/sharer.php?t=" + encodeURIComponent(this.projectData.title) + "&u="+encodeURIComponent("http://minnim.tv/?project=" + this.sectionParam + "_" + this.projectParam ));
			this.twSharer.attr("href", "http://twitter.com/share?url=http://minnim.tv/?project=" + this.sectionParam + "_" + this.projectParam);
		},
		
		switchProject:function(){
			//trace("change project - " + $(this.section).attr("id") + " - " + this.project);
			this.a.addClass("activo");
			
			var project = getSiteProject(siteXml, this.sectionParam.toUpperCase(), this.projectParam)
			var gallery = project.gallery;
			var video = project.video;
			var url = project.url;
			//
			var galleryHTML = getImgListHTML(gallery);
			//alert(galleryHTML);
			$(".selector ul", this.section).html(galleryHTML);
			var $a = $(".selector ul a", this.section);
			var $img = this.imgMain;
			var $loader = this.loader;
			var $projectA = $(".projectButton", this.section);
			
			//trace("change project url:" + $(this.section).attr("id") +", url:"+url); 
			$projectA.attr("href", project.url);
			
			this.title.html(project.title);
			this.desc.html(project.desc);
			
			$a.click(function(){
				$a.removeClass("activo");
			});
			
			this.prev.css("display","none");
			
			if(video[0]!= undefined){
				//alert(gallery[0] + " - " + video[0]);
				switchVideo(video[0], gallery[0]);
				$($a.get(0)).addClass("activo");
			}else{
				//trace("change project renew gallery- " + $(this.section).attr("id"));
				var proxySection = this.section;
				this.gallery.index = 0;
				var gallery = this.gallery;
				$a.each(function(index){
					var is = new ImageSwitcher($(this), proxySection, index, $a.length, gallery);

				});	
				$($a.get(0)).click();
				
			}
			
			this.setSharers();
			return false;
		}
});
