var App = {
  "#edit_user_sex::change":function(element,event) {
    var maleRow = $("genre_his");
    var femaleRow = $("genre_hers");
    var hardcoreRow = $("genre_hardcore");
    var showMale = element.value.toLowerCase() == "m";
    if (maleRow && femaleRow && hardcoreRow) {
      maleRow.setStyle("display",showMale ? "" : "none");
      femaleRow.setStyle("display",showMale ? "none" : "");
      hardcoreRow.setStyle("display",showMale ? "" : "none");
    } else {
      maleRow.setStyle("display",showMale ? "none" : "");
      femaleRow.setStyle("display",showMale ? "" : "none");
      hardcoreRow.setStyle("display",showMale ? "none" : "");
    }
  },
  ".hide":function(element) {
    element.setStyle("display","none");
  },
  ".show":function(element) {
    element.setStyle("display","");
  },
  "a.expander::click":function(element,event) {
    event.stop();
    var image = element.getElement("img");
    new Expander(image,{src:element.href});
  },
  "a.new":function(element) {
    element.target = "_new" + element;
  },
  "a.opener::click":function(element,event) {
    event.stop();
    window.opener.location = element.href;
    window.close();
  },
  "a.pop::click":function(element,event) {
    event.stop();
    new Window(element.href);
  },
	"a.tab::click":function(element,event) {
		event.stop();
		element.getParent().getElements("a.tab").forEach(function(tab){
			if (element == tab) {
				tab.addClass("active");
				$(tab.getAttribute("rel")).setStyle("display","");
			} else {
				tab.removeClass("active");
				$(tab.getAttribute("rel")).setStyle("display","none");
			}
		});
	},
  "a.toggle::click":function(element,event) {
    event.stop();
    var toggle = $(element.getAttribute("rel"));
    if (toggle) toggle.setStyle("display",(toggle.getStyle("display") == "none" ? "" : "none"));
  }
}

var FlashTag = new Class({
	options:{allowFullScreen:"true",bgcolor:"",height:0,id:"flash",name:"flash",quality:"high",src:"",style:"",variables:{},width:0,wmode:"window"},
	initialize:function(container,options) {
		this.setOptions(options);
		this.container = $(container);
	},
	embed:function() {
		this.container.setHTML(this.getHTML());
		if (window.ie) this.fixIE.delay(100,this);
	},
	fixIE:function() {
		this.container.outerHTML = this.container.outerHTML.replace(/<param name="FlashVars" value="">/ig,'<param name="FlashVars" value="'+this.getVariables()+'"');
	},
	getHTML:function() {
		if (window.ie) {
			var html = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" height=\""+this.options.height+"\" id=\""+this.options.id+"\" style=\""+this.options.style+"\" width=\""+this.options.width+"\">";
			var options = this.getIEOptions();
			for (key in options)
				html += "<param name=\""+key+"\" value=\""+options[key]+"\" />";
			html += "</object>";
		} else {
			var tmp = new Element("div");
			this.getObject().injectInside(tmp);
			var html = tmp.innerHTML;
		}
		return html;
	},
	getIEOptions:function() {
		return {allowFullScreen:this.options.allowFullScreen,movie:this.options.src,bgcolor:this.options.bgcolor,quality:this.options.quality,flashvars:this.getVariables(),wmode:this.options.wmode};
	},
	getObject:function() {
		if (window.ie) {
			return new Element("div").setHTML(this.getHTML()).getFirst();
		} else {
			return new Element("embed",{bgcolor:this.options.bgcolor,flashVars:this.getVariables(),height:this.options.height,id:this.options.id,name:this.options.name,quality:this.options.quality,src:this.options.src,style:this.options.style,"type":"application/x-shockwave-flash",width:this.options.width,wmode:this.options.wmode});
		}
	},
	getVariables:function() {
		var variables = [];
		for (key in this.options.variables) {
			if (key != "") variables.push(key + "=" + this.options.variables[key]);
		}
		return variables.join("&");
	}
});
FlashTag.implement(new Options);

var Video = {
	Local:function(video,image,width,height,id) {
		var flash = new FlashTag($("video-" + id),{allowFullScreen:true,height:height,id:"video" + id,name:"video" + id,src:"/images/video-player.swf",variables:{file:video,image:image},width:width,wmode:"transparent"});
		flash.embed.delay(100,flash);
	}
}


var Window = new Class({
  initialize:function(url,options) {
    this.url = url + (url.contains("?") ? "&" : "?") + "external=yes";
    window.open(this.url,"_new",'height=500,width=650,left=100,resizable=yes,scrollbars=yes');
    return false;
    this.setOptions(options);
    this.cover = $("inline-cover") || new Element("div",{id:"inline-cover"}).setStyle("display","none").injectInside(document.body).addEvent("click",this.hide.bind(this)).setStyle("opacity",0.8);
    this.cover.addClass("loading").setStyle("display","");
    this.iframe = $("inline-window") || new Element("iframe",{id:"inline-window",frameBorder:0,scrolling:"auto"}).setStyle("border","none").setStyle("display","none").injectInside(document.body);
    this.iframe.addEvent("load",this.show.bind(this));
    this.iframe.src = this.url;
  },
  hide:function() {
    this.cover.setStyle("display","none");
    this.iframe.setStyle("display","none");
    if (this.iframe.parentNode)
      this.iframe.remove();
  },
  show:function() {
    this.cover.removeClass("loading");
    this.iframe.setStyle("display","");
  }
});
Window.implement(new Options);

/*
Inserts GA using DOM insertion of <script> tag and "script onload" method to
initialize the pageTracker object. Prevents GA insertion from blocking I/O!

As suggested in Steve Souder's talk. See:
http://google-code-updates.blogspot.com/2009/03/steve-souders-lifes-too-short-write.html
*/

function gaSSDSLoad (acct) {
  var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."),
      pageTracker,
      s;
  s = document.createElement('script');
  s.src = gaJsHost + 'google-analytics.com/ga.js';
  s.type = 'text/javascript';
  s.onloadDone = false;
  function init () {
    pageTracker = _gat._getTracker(acct);
    pageTracker._trackPageview();
    if (window.ongaSSDSLoad) {
      window.ongaSSDSLoad(pageTracker);
    }
  }
  s.onload = function () {
    s.onloadDone = true;
    init();
  };
  s.onreadystatechange = function() {
    if (('loaded' === s.readyState || 'complete' === s.readyState) && !s.onloadDone) {
      s.onloadDone = true;
      init();
    }
  };
  document.getElementsByTagName('head')[0].appendChild(s);
}
