$(document).ready(function(){
	var PAGE_STATUS = {NEW: 0, RENDERED: 1};
	var uploading_file = false;
	var upload_inputs_loaded = false;
	
	var Page = Class.extend({
		init: function(){
			this.set_page_title();
			this.set_background_image();
			this.status = PAGE_STATUS.NEW;
		},
			
		set_background_image: function(){
			$('body').css('backgroundImage','url(/media/img/bg/' + this.background_image +'.jpg)'); 
		},
	
		set_page_title: function(){
			//document.title = this.page_title + ' | ' + document.title.split('|')[1];
			document.title = 'Conteúdo Musical | ' + this.page_title;
		},
		
		render: function(){
			$(this.main_container).show();
			this.highlight_menu_item(true);
			this.status = PAGE_STATUS.RENDERED;
		},
					
		highlight_menu_item: function(b){
			if(this.menu_selector){
				if(b)
					$('.' + this.menu_selector).addClass(this.menu_selector +'_selected');
				else
					$('.' + this.menu_selector).removeClass(this.menu_selector +'_selected');
			}
		},
		
		close: function(){
			this.highlight_menu_item(false);
			$(this.main_container).hide();
		},
		
		abort: function(){
			// something to abort the ajax call before closing
			this.close();
		}
	});
	
	/* HOME */
	var Home = Page.extend({
		init: function(){
			this.main_container = '#home';
			this.page_title = 'Página Inicial';
			this.background_image = '1';
			this.hash = 'home/';
			this._super();
		},
	
		render: function(){
			$.getJSON('/data/home/?k=' + Math.random(), function(data){
				
				// blog entries
				if(data.posts)
					$.each(data.posts, function(i, item){
						element = $("#blog_item_model").clone();
						element.attr('id', 'post_'+ item.id);
						$("h1", element).html(item.title);
						$("p", element).html(item.text);
						
						$(".twitter", element).attr("href", "http://twitter.com/home?status=Leia%20%23ConteudoMusical%20"+ Url.encode("http://www.conteudomusical.com.br/#blog/0/"+item.id+"/"));
						$(".google", element).attr("href", "http://www.google.com/bookmarks/mark?op=edit&bkmk="+ Url.encode("http://www.conteudomusical.com.br/#blog/0/"+item.id+"/") + "&title="+  Url.encode(item.title));
						$(".facebook", element).attr("href", "http://www.facebook.com/sharer.php?u=" + Url.encode("http://www.conteudomusical.com.br/#blog/0/"+item.id+"/") + "&t="+Url.encode(item.title));						
						$(".facebook", element).attr("rel", "http://www.conteudomusical.com.br/media/screenshot.jpg");						
						$(".technorati", element).attr("href", "http://www.technorati.com/faves?add="+ Url.encode("http://www.conteudomusical.com.br/#blog/0/"+item.id+"/") + "&title="+  Url.encode(item.title));
						$(".digg", element).attr("href", "http://digg.com/submit?phase=2&url="+ Url.encode("http://www.conteudomusical.com.br/#blog/0/"+item.id+"/") + "&title="+  Url.encode(item.title));
						$(".delicious", element).attr("href", "http://del.icio.us/post?url="+ Url.encode("http://www.conteudomusical.com.br/#blog/0/"+item.id+"/") + "&title="+  Url.encode(item.title));
						
						$(".author", element).html('# '+ item.author +' '+ item.date_added);
						$(".comments span", element).html(item.comment_c);
						if(item.image_url){
							image = new Image();
							image.src = item.image_url;
							image.alt = item.title;
							$("p", element).prepend(image);
						}
						$("#home .left").append(element);
						element.removeClass("hidden");
						sep = $(document.createElement('div')).addClass('sep');
						sep.insertAfter(element);
					});
			    $('a[href^="http://www.youtube.com"]').flash(
			        { height: 370, width: 450 },
			        { version: 8 },
			        function(htmlOptions) {
			            $this = $(this);
			            htmlOptions.src = $this.attr('href');
			            $this.before($.fn.flash.transform(htmlOptions));						
			        }
			    );
				
				// cases
				if(data.cases)
					$.each(data.cases, function(i, item){
						element = $("#case_item_model").clone();
						element.attr('id', 'case_'+ item.id);
						$("h2", element).html(item.title);
						if(item.image_url){
							$("img", element).attr('src', item.image_url);
							$("img", element).attr('alt', item.title);
						}else{
							// show default image
						}
						$("#box_cases").append(element);
						element.removeClass("hidden");
						
					});
				
				// new bands
				if(data.new_bands)
					$.each(data.new_bands, function(i, item){
						element = $("#new_bands_item_model").clone();
						element.attr("id", "new_band_"+ item.id);
						$("h2", element).html(item.title);
						if(item.image_url){
							$("img", element).attr("src", item.image_url);
							$("img", element).attr("alt", item.title);
						}else{
							// show default image
						}
						$("#box_new_bands").append(element);
						element.removeClass("hidden");
					});
				
				// my secret playlist
				if(data.playlist)
					$("#secret_pl .artist_name").html(data.playlist.guest);
					if(data.playlist.image_url){
						$("#secret_pl img").attr("src", data.playlist.image_url);
					}
				
					$.each(data.playlist.songs, function(i, item){
						element = $("<li rel='"+ i +"'><span>"+ item.artist +"</span> - "+ item.name +"</li>");
						$("#secret_pl .songs").append(element);
					});
					$("#secret_pl .songs").attr('rel', data.playlist.id);


			});		
			this._super();
		},
		close: function(){
			$(this.main_container + " .left *").remove();
			$("#box_cases div").remove();
			$("#box_new_bands div").remove();
			$("#secret_pl .songs li").remove();
			this._super();
		}
	});
	
	
	/* ABOUT */
	var About = Page.extend({
		init: function(){
			this.main_container = '#about';
			this.page_title = 'Sobre';
			this.background_image = '4';
			this.hash = 'about/';
			this.menu_selector = "about";
			this._super();
		}
	});
	
	/* BLOG */
	var Blog = Page.extend({
		init: function(current_page, year, month){
			if(current_page===undefined)
				this.current_page = 1;
			else
				this.current_page = current_page;
			
			if(year===undefined)
				this.year = null;
			else
				this.year = year;
			
			if(month===undefined)
				this.month = null;
			else
				this.month = month;
		
			this.main_container = '#blog';
			this.page_title = 'Blog';
			this.background_image = '2';
			this.menu_selector = "blog";
			this.hash = 'blog/';
			
			this._super();
		},
	
		render: function(){
			if(this.month && this.year)
				archives_url = "&month="+ this.month +"&year="+ this.year;
			else
				archives_url = "";
				
			$.getJSON('/blog/json/?current_page='+ this.current_page +''+ archives_url +'&k=' + Math.random(), function(data){
				// blog entries
				if(data.posts)
					$.each(data.posts, function(i, item){
						element = $("#blog_item_model").clone();
						element.attr('id', 'post_'+ item.id);
						$("h1", element).html(item.title);
						$("p", element).html(item.text);
						
						
						$(".twitter", element).attr("href", "http://twitter.com/home?status=Leia%20%23ConteudoMusical%20"+ Url.encode("http://www.conteudomusical.com.br/#blog/0/"+item.id+"/"));
						$(".google", element).attr("href", "http://www.google.com/bookmarks/mark?op=edit&bkmk="+ Url.encode("http://www.conteudomusical.com.br/#blog/0/"+item.id+"/") + "&title="+  Url.encode(item.title));
						$(".facebook", element).attr("href", "http://www.facebook.com/sharer.php?u=" + Url.encode("http://www.conteudomusical.com.br/#blog/0/"+item.id+"/") + "&t="+Url.encode(item.title));						
						$(".technorati", element).attr("href", "http://www.technorati.com/faves?add="+ Url.encode("http://www.conteudomusical.com.br/#blog/0/"+item.id+"/") + "&title="+  Url.encode(item.title));
						$(".digg", element).attr("href", "http://digg.com/submit?phase=2&url="+ Url.encode("http://www.conteudomusical.com.br/#blog/0/"+item.id+"/") + "&title="+  Url.encode(item.title));
						$(".delicious", element).attr("href", "http://del.icio.us/post?url="+ Url.encode("http://www.conteudomusical.com.br/#blog/0/"+item.id+"/") + "&title="+  Url.encode(item.title));
						
						
						$(".author", element).html('# '+ item.author +' '+ item.date_added);
						$(".comments span", element).html(item.comment_c);
						if(item.image_url){
							image = new Image();
							image.src = item.image_url;
							image.alt = item.title;
							$("p", element).prepend(image);
						}
						element.insertBefore("#blog #paginator");
						element.removeClass("hidden");
						sep = $(document.createElement('div')).addClass('sep');
						sep.insertAfter(element);
					});
				
				if(data.has_prev_page){
					$("#paginator .prev").removeClass("hidden");
					$("#paginator .prev").attr("href", "#blog/"+ parseInt(data.current_page-1) +"/");
				}else
					$("#paginator .prev").addClass("hidden");
				
				if(data.has_next_page){
					$("#paginator .next").removeClass("hidden");
					$("#paginator .next").attr("href", "#blog/"+ parseInt(data.current_page+1) +"/");
				}else
					$("#paginator .next").addClass("hidden");
				
				
				$.each(data.archive, function(i, item){
					$('<a class="reset" href="#blog/1/archives/' + item.month_n +'/">'+ item.month_name +'</a>').insertAfter("#blog .right .archive");						
				});
				$.each(data.links, function(i, item){
					$('<a target="_blank" class="reset" href="'+ item.url +'">'+ item.title +'</a>').insertAfter("#blog .right .links");						
				});
				
			    $('a[href^="http://www.youtube.com"]').flash(
			        { height: 370, width: 450 },
			        { version: 8 },
			        function(htmlOptions) {
			            $this = $(this);
			            htmlOptions.src = $this.attr('href');
			            $this.before($.fn.flash.transform(htmlOptions));						
			        }
			    );
				
				$("#blog .right").height($("#blog .left").height());
			});		
			this._super();
		},
		close: function(){
			$("#blog .left .blog_item").remove();
			$("#blog .left .sep").remove();
			$("#blog .right .reset").remove();
			this._super();
		}
	});
	
	/* CONTACT */
	var Contact = Page.extend({
		init: function(case_id){
			this.main_container = "#contact";
			this.page_title = "Contato";
			this.background_image = "4";
			this.menu_selector = "contact";
			this.hash = 'contact/';
			this._super();
		},
		check_form: function(){
			var err = '';
			
			if($('#id_name').val().length<1)
				err += '\n- Nome';
			if($('#id_email').val().length<1)
				err += '\n- Email';
			if($('#id_message').val().length<1)
				err += '\n- Mensagem';

			if(err.length>0){
				alert('Por favor verifique os seguintes campos:' + err);
				return false;
			}
			
			return true;
		},
		
		send_form: function(){
			if(!this.check_form())
				return;
	
			$("#contact_form").ajaxSubmit({success: function(){
				alert("Sua mensagem foi enviada, em breve retornaremos");
				$("#id_name, #id_email, #id_message").val("");
			}});
		}
	});
	
	/* NEW BAND ADDED */
	var NewBandAdded = Page.extend({
		init: function(case_id){
			this.main_container = "#new_band_added";
			this.page_title = "Banda cadastrada";
			this.background_image = "4";
			this.menu_selector = "artists";
			this.hash = 'add-band-ret/';
			this._super();
		}
	});
	
	/* ADD NEW BAND */
	var AddNewBand = Page.extend({
		init: function(case_id){
			this.main_container = "#new_band_add";
			this.page_title = "Cadastre sua banda";
			this.background_image = "4";
			this.menu_selector = "artists";
			this.hash = 'add-band/';
			$("#form_add_band .member").tooltip({ 
			    bodyHandler: function() { 
			        return 'Nome do integrante'; 
			    },
				track:true,
				fade:100,
				delay:0,
			    showURL: false 
			});	
			$("#form_add_band .instr").tooltip({ 
				    bodyHandler: function() { 
				        return 'Instrumento'; 
				    },
					track:true,
					fade:100,
					delay:0,
				    showURL: false 
				});
			$("#image_upload_container").tooltip({ 
			    bodyHandler: function() { 
			        return 'Clique no botão "Selecionar Foto" e escolha a imagem <br/>desejada no seu computador. Aguarde o upload e ela aparecerá<br/> abaixo. Você poderá enviar até cinco imagens com até 1mb cada uma.<br/> Se precisar deletar alguma foto, clique no x.'; 
			    },
				track:true,
				fade:100,
				delay:0,
			    showURL: false 
			});	
			$("#song_upload_container").tooltip({ 
			    bodyHandler: function() { 
			        return 'Clique no botão "Selecionar Música" e escolha o arquivo <br/> mp3 desejado no seu computador. Aguarde o upload e ela<br/> aparecerá abaixo. A seguir nomeie sua música como quer que ela apareça<br/> no site. Para deletar alguma música clique no botão Remover'; 
			    },
				track:true,
				fade:100,
				delay:0,
			    showURL: false 
			});

			if(!upload_inputs_loaded){
				// initialize upload inputs
				$('#image_upload').fileUpload({
					'uploader': '/media/swf/uploader.swf',
					'script': '/artists/upload/image/',
					'cancelImg' : 'cancel.png',
					'auto': true,
					'sizeLimit': 1048576,
					'folder' : '/uploads',
					'fileDataName': 'nb_file',
					'fileExt': '*.jpg;*.jpeg',
					'buttonText': 'SELECIONAR FOTO',
					'fileDesc': 'Somente imagens (*.jpg e *.jpeg)',
					'width': '135px',
	                'onComplete': function(e, q, f, r, d) {
							var data = JSON.parse(r);
							if(data.error){
								alert(data.error_dsc);
							}else{
								current_item = ($("#uploaded_images").find("div").length+1);
								$("#uploaded_images").append($('<div><img src="'+ data.thumb_url +'" alt="'+ data.filename +'" />X</div>'));
								
								if(current_item==5){
									$("#image_upload_box").fadeOut();
								}
							}
						}
					});
				$('#song_upload').fileUpload({
					'uploader': '/media/swf/song_uploader.swf',
					'script': '/artists/upload/song/',
					'cancelImg' : 'cancel.png',
					'auto': true,
					'folder' : '/uploads',
					'fileDataName': 'sn_file',
					'fileExt': '*.mp3',
					'sizeLimit': 6291456,
					'fileDesc': 'Somente audio (*.mp3)',
					'width': '135px',
	                'onComplete': function(e, q, f, r, d) {
							uploading_file = false;
							var data = JSON.parse(r);
							if(data.error){
								alert(data.error_dsc);
							}else{
								current_item = ($("#uploaded_songs").find(".item").length+1);
								item = $('<div rel="'+ data.filename +'" class="item"></div>');
								p1 = $('<label><b>Arquivo '+ current_item +'</b> - <i class="clickable remove_song">Remover</i></label><h2>'+ data.original_filename +'</h2>');
								p2 = $('<label>Nome da música</label><input type="text" id="item_'+ current_item +'"  />');
								item.append(p1); item.append(p2);
								$("#uploaded_songs").append(item);
							
								if(current_item==2){
									$("#song_upload_box").fadeOut();
								}
							
							}
						}
					});
				upload_inputs_loaded = true;
			}
			this._super();
		},
		
		check_form: function(){
			if(uploading_file){
				alert('Por favor aguarde o upload dos arquivos');
				return false;
			}
			var err = '';

			if($('#id_band_name').val().length<1)
				err += '\n- Nome da banda';
			if($('#id_member1').val().length<1)
				err += '\n- Integrante (1)';
			if($('#id_release').val().length<1)
				err += '\n- Release';
			
			for(i=1;i<=$("#uploaded_songs").find(".item").length;i++)
				if($("#uploaded_songs .item").find("#item_"+ i).val().length==0)
					err += '\n- Nome da música enviada (arquivo '+ i + ')';
			
			if($("#uploaded_images div").length==0){
				err += '\n- Envie ao menos uma foto';
			}

			if($("#uploaded_songs .item").length==0){
				err += '\n- Envie ao menos uma música';
			}

			
			if(err.length>0){
				alert('Por favor verifique os seguintes campos:' + err);
				return false;
			}
			
			return true;
			
		},
		
		save_form: function(){
			if(!this.check_form())
				return;
			
			image_c = $('<input type="hidden" value="'+$("#uploaded_images img").length+'" name="image_c" />');
			$("#form_add_band").append(image_c);
			$("#uploaded_images img").each(function(i){
				element = $('<input type="hidden" value="'+$(this).attr('alt')+'" name="image_'+ i +'" />');
				$("#form_add_band").append(element);
			});
			
			song_c = $('<input type="hidden" value="'+$("#uploaded_songs div").length+'" name="song_c" />');
			$("#form_add_band").append(song_c);
			$("#uploaded_songs div").each(function(i){
				element1 = $('<input type="hidden" value="'+$(this).attr('rel')+'" name="song_file_'+ i +'" />');
				element2 = $('<input type="hidden" value="'+$(this).find("input").val()+'" name="song_name_'+ i +'" />');
				$("#form_add_band").append(element1);
				$("#form_add_band").append(element2);
			});
				
			$("#new_band_add form").ajaxSubmit({success: function(){
				nav.set_hash('add-band-ret/');
			}});
		},
		
		close: function(){
			/*$("#new_band_add .input_reset").val('');
			$("#uploaded_songs *").remove();
			$("#uploaded_images *").remove();
			$("#new_band_add .fileUploadQueue").remove();
			$("#new_band_add embed").remove();*/
			
			this._super();
		}
	});
	
	/* NEW BANDS */
	var NewBands = Page.extend({
		init: function(startswith, current_page){
			this.main_container = "#new_bands";
			this.page_title = "Bandas novas";
			this.background_image = "3";
			this.menu_selector = "artists";
			this.hash = 'new-bands/';
			this.image_array = new Array();
			if(startswith===undefined || startswith=='ALL' || startswith=='') this.startswith = '!';
			else this.startswith = startswith;
			if(current_page===undefined) this.current_page = 1;
			else this.current_page = current_page;
			
			this._super();
		},
		
		render: function(){
			url = 'startswith='+ this.startswith;
			var that = this;
			$.getJSON("/artists/new_bands/?current_page=" + this.current_page + "&k=" + Math.random() + "&" + url, function(data){
				if(data.bands){
					var that2 = that;
					$.each(data.bands, function(i, item){
						
						element = $("#new_band_item_model").clone();
						element.attr("id", "new_band_"+ item.id);
						$("h1", element).html(item.name);
						$("p", element).html(item.release);
						$("img", element).attr("alt", item.title);
						
						$("img", element).attr("id", "index_"+i);
						
						
						
						if(item.image_url){
							$("img", element).attr("src", item.image_url);
							item.images[item.images.length] = {'image_url': item.image_url};
						}else{
							// show default image
						}
						
						that2.image_array[i] = item.images;
						
						$(".members li", element).remove();
						$.each(item.members, function(i, m){
							if(m.instrument==null)
								$(".members", element).append($("<li>"+ m.name +"</li>"));
							else
								$(".members", element).append($("<li>"+ m.name +"<br/><i>"+ m.instrument +"</i></li>"));
						});	
						
						$(".links ul li", element).remove();
						$.each(item.links, function(i, l){
							$(".links ul", element).append($('<li><a target="_blank" href="'+ l.url +'">'+ l.type +'</a></li>'));
						});
						
						
						if(item.songs.length>0){
							$(".listen ul li", element).remove();
							$.each(item.songs, function(i, s){
								$(".listen ul", element).append($('<li rel="'+ i +'">'+ s.name +'</li>'));
							});
							$(".listen ul", element).attr("rel", item.id);
						}else{
							$(".listen", element).remove();
						}
						
						element.insertBefore($("#new_bands .paging"));
						element.removeClass("hidden");
						element.addClass("reset");
					});
					
				if(data.has_prev_page){
					$("#new_bands .paging .prev").removeClass("hidden");
					$("#new_bands .paging .prev").attr("href", "#new-bands/" + that.startswith + "/"+ parseInt(data.current_page-1) +"/");
				}else
					$("#new_bands .paging .prev").addClass("hidden");
				
				if(data.has_next_page){
					$("#new_bands .paging .next").removeClass("hidden");
					$("#new_bands .paging .next").attr("href", "#new-bands/" + that.startswith + "/"+ parseInt(data.current_page+1) +"/");
				}else
					$("#new_bands .paging .next").addClass("hidden");
				}
				
			});
			this._super();
		},
		
		close: function(){
			$("#new_bands .reset").remove();
			$("#new_bands .left ul li").removeClass("slct");
			$("#new_bands .paging .next, #new_bands .paging .prev").addClass("hidden");

			this._super();
		}
		
	});
	
	/* EVENTS */
	var Agenda = Page.extend({
		init: function(case_id){
			this.main_container = "#agenda";
			this.page_title = "Agenda";
			this.background_image = "1";
			this.menu_selector = "agenda";
			this.hash = 'agenda/';
			this._super();
		},
	
		render: function(){
			$.getJSON("/events/?k="+ Math.random(), function(data){
				if(data.events)
					$.each(data.events, function(i, item){
						element = $("#item_agenda_model").clone();
						element.attr("id", "agenda_"+ item.id);
						$(".left h1", element).html(item.title);
						$(".left h2", element).html(item.date);
						$("p", element).html(item.description);
						$(".right h1", element).html(item.venue);
						$(".right h2", element).html(item.venue_address);
						if(item.image_url){
							$("img", element).attr("src", item.image_url);
							$("img", element).attr("alt", item.title);
							$("img", element).removeClass("hidden");
						}else{
							$("img", element).addClass("hidden");
						}
						$("#agenda").append(element);
						element.removeClass("hidden");
						element.addClass("item_agenda");
					});
			});
			
			this._super();
		},
				
		close: function(){
			$("#agenda .item_agenda").remove();	
			this._super();
		}
	});
	
	
	/* CASES */
	var Cases = Page.extend({
		init: function(case_id){
			if(case_id===undefined)
				this.case_id = 0; // get the last case
			else
				this.case_id = case_id;
		
			this.main_container = "#cases";
			this.page_title = "Nossos Cases";
			this.background_image = "2";
			this.menu_selector = "cases";
			this.image_array = null;
			this.current_image = 0;
			this.hash = 'cases/'+ this.case_id +'/';
			this._super();
		},
	
		render: function(){
			var clos = this;
			$.getJSON("/cases/"+ this.case_id +"/?k="+ Math.random(), function(data){
				// all cases
				if(data.cases)
					$.each(data.cases, function(i, item){
						element = $("#case_list_item_model").clone();
						element.attr("id", "case_"+ item.id);
						$("h2", element).html(item.title);
						if(item.image_url){
							$("img", element).attr("src", item.image_url);
							$("img", element).attr("alt", item.title);
						}else{
							// show default image
						}
						$("#case_list").append(element);
						element.removeClass("hidden");
					});
				
				// "case" is a js reserved word and fails on IE, so "_case"
				$("#case_detail h2").html(data._case.title);
				
				if(data._case.image_url.length>0){
					$("#case_detail .main_image").attr("src", data._case.image_url);
					$("#case_detail .main_image").attr("alt", data._case.title);
					$("#case_detail .caption span").html(data._case.image_caption);
					data._case.images[data._case.images.length] = {'image_url': data._case.image_url, 'caption': data._case.image_caption};
				}
				
				$("#case_detail p").html(data._case.text);
				$("#case_detail").removeClass("hidden");
				
				if(data._case.youtube_video_url.length>0){
					if(data._case.video_title==null)
						data._case.video_title = 'veja o clipe';
					video_label = $('<h4 rel="'+ data._case.youtube_video_url +'" class="video_label video_related">' + data._case.video_title + '</h4>');
					$("#case_detail").append(video_label);
				}

				if(data._case.images.length>0){
					clos.image_array = data._case.images;
					$("#case_detail .next_image").removeClass("hidden");
				}else
					$("#case_detail .next_image").addClass("hidden");

				// set caption bar alpha
				$("#case_detail .caption").fadeTo(0, 0.7);
				$("#case_detail .caption span").fadeIn()
			});
			
			this._super();
		},
		
		load_next_image: function(){
			if(this.image_array!=null){
				$("#case_detail .main_image").attr("src", "");
				$("#case_detail .main_image").attr("alt", "");
				this.current_image = (this.current_image % this.image_array.length)+1;
				$("#case_detail .main_image").attr("src", this.image_array[this.current_image-1].image_url);
				$("#case_detail .main_image").attr("alt", this.image_array[this.current_image-1].caption);
				$("#case_detail .caption").html($('<span></span>').append(this.image_array[this.current_image-1].caption));
			}
		},
		
		
		
		close: function(){
			$("#case_list div").remove();	
			$("#case_detail .video_related").remove();
			$("#case_detail").addClass("hidden");
			$('.video_player').remove();
			
			
			this._super();
		}
	});
	
	
	/* ARTISTS */
	var Artists = Page.extend({
		init: function(artist_id){
			if(artist_id===undefined)
				this.artist_id = 0; // get the last case
			else
				this.artist_id = artist_id;
		
			this.main_container = "#artists";
			this.page_title = "Nossos Artistas";
			this.background_image = "3";
			this.menu_selector = "artists";
			this.hash = 'artists/'+ this.artist_id +'/'
			this.image_array = new Array();
			this.current_image = 0;
			this._super();
		},
		
		render: function(){
			var clos = this;
			$.getJSON("/artists/"+ this.artist_id +"/?k="+ Math.random(), function(data){
				// all artists
				if(data.artists)
					$.each(data.artists, function(i, item){
						element = $("#artist_list_item_model").clone();
						element.attr("id", "artist_"+ item.id);
						$("h2", element).html(item.name);
						$("#artist_list").append(element);
						element.removeClass("hidden");
					});
				
				$("#artist_detail h2").html(data.artist.name);
				
				if(data.artist.image_url){
					$("#artist_detail .main_image").attr("src", data.artist.image_url);
					$("#artist_detail .main_image").attr("alt", data.artist.name);
					data.artist.images[data.artist.images.length] = {'image_url': data.artist.image_url};
				}
				

				$("#artist_detail p").html(data.artist.text);
				
				
				if(data.artist.links.length>0){
					$.each(data.artist.links, function(i, item){
						element = $('<a target="_blank" href="'+item.url+'" rel="external">'+ item.type +'</a>');
						$("#artist_detail .links").append(element);
					});
					$("#artist_detail .links").removeClass("hidden");
				}
				
				if(data.artist.images.length>0){
					clos.image_array = data.artist.images;
					$("#artist_detail .next_image").removeClass("hidden");
				}else
					$("#artist_detail .next_image").addClass("hidden");
				
				$("#artist_detail").removeClass("hidden");
				
			});
			
			this._super();
		},
		
		load_next_image: function(){
			if(this.image_array!=null){
				$("#artist_detail .main_image").attr("src", "");
				$("#artist_detail .main_image").attr("alt", "");
				this.current_image = (this.current_image % this.image_array.length)+1;
				$("#artist_detail .main_image").attr("src", this.image_array[this.current_image-1].image_url);
				$("#artist_detail .main_image").attr("alt", this.image_array[this.current_image-1].caption);
			}
		},
		
		close: function(){
			$("#artist_list div").remove();
			$("#artist_detail .links a").remove();
			$("#artist_detail h2").html("");
			$("#artist_detail .main_image").attr("src", "");
			$("#artist_detail .main_image").attr("alt", "");
			$("#artist_detail p").html("");
			$("#artist_detail").addClass("hidden");
			
			this._super();
		}
	});
	
	
	/* NAMIDIA LINKS */
	var NaMidiaLink = Page.extend({
		init: function(current_page){
			if(current_page===undefined)
				this.current_page = 1;
			else
				this.current_page = current_page;
					
			this.main_container = '#namidia';
			this.page_title = 'Na Mídia';
			this.background_image = '3';
			this.menu_selector = "contact";
			this.hash = 'namidia/';
			
			this._super();
		},
	
		render: function(){
			$.getJSON('/namidia/?current_page='+ this.current_page +'&k=' + Math.random(), function(data){
				// links
				if(data.links)
					$.each(data.links, function(i, item){
						element = $('<li class="left"><a href="'+ item.link_to +'" target="_blank"><img src="'+ item.image_url +'" /></a><p><a href="'+ item.link_to +'" target="_blank">'+ item.title +'</a></p></li>');
						$("#namidia_list").append(element);					    
					});
				

				if(data.has_prev_page){
					$(".paging .prev").removeClass("hidden");
					$(".paging .prev").attr("href", "#namidia/"+ parseInt(data.current_page-1) +"/");
				}else
					$(".paging .prev").addClass("hidden");
				
				if(data.has_next_page){
					$(".paging .next").removeClass("hidden");
					$(".paging .next").attr("href", "#namidia/"+ parseInt(data.current_page+1) +"/");
				}else
					$(".paging .next").addClass("hidden");

			});		
			this._super();
		},
		close: function(){
			$("#namidia_list li").remove();
			this._super();
		}
	});



	var WindowNav = Class.extend({	
		init: function(){
			this.current_page = null;
		},
		
		open: function(class_page){
			this.show_loading_bar(true);
			if(this.current_page){
				if(this.status!=PAGE_STATUS.RENDERED)
					this.current_page.abort();
				this.current_page.close();
			}			
			this.current_page = class_page;
			this.current_page.render();
			this.show_loading_bar(false);
			//document.location.hash = this.current_page.hash;
		},
		
		set_hash: function(hash){
			document.location.hash = hash;
		},
		
		show_loading_bar: function(b){
			if(b) $('#loading_bar').show();
			else $('#loading_bar').hide();
		}
	});
		
	
	
	/* HANDLERS */
	$(".home_page").click(function(){
		nav.set_hash('home/');
	});
	
	$("#menu .cases").click(function(){
		nav.set_hash('cases/0/');
	});
	
	$("#menu .artists").click(function(){
		nav.set_hash('artists/0/');
	});
	
	$("#menu .contact").click(function(){
		nav.set_hash('contact/');
	});
	
	$("#menu .agenda").click(function(){
		nav.set_hash('agenda/');
	});
	
	$("#menu .blog").click(function(){
		nav.set_hash('blog/');
	});

	
	$("#menu .about").click(function(){
		nav.set_hash('about/');
	});
	
	$(".blog_item .comments").live("click", function(){
		window.open('/blog/comment/'+$(this).parent().attr('id').split('_')[1]+'/','comments','height=450,width=280');
	});
	
	
	$("#new_bands_link, #box_new_bands, #menu .new_bands").click(function(){
		nav.set_hash('new-bands/');
	});
	
	$("#new_bands .left  ul li").click(function(){
		$(this).addClass("slct");
		if($(this).html()!='#') var l = $(this).html();
		else var l = 'n_s';
			
		nav.set_hash('new-bands/'+ l +'/');
	});
	
	$("#case_list div, #box_cases div").live("click", function(){
		nav.set_hash('cases/'+$(this).attr('id').split('_')[1]+'/');
	});
	
	$("#artist_list div").live("click", function(){
		nav.set_hash('artists/'+$(this).attr('id').split('_')[1]+'/');
	});
	
	$("#new_band_add .submit").click(function(){
		nav.current_page.save_form();
	});
	
	$("li.add_member").click(function(){
		i = parseInt($("#member_c").val()) + 1;
		$("#member_c").val(i);
		$('<li><label>&nbsp;</label> <input class="input_reset member" name="member_'+ i +'" type="text" id="id_member'+ i +'" /><input class="input_reset instr" name="instr_'+ i +'" type="text" id="id_instr'+ i +'" /></li>').insertBefore($(".add_member"));
	});
	
	$(".next_image").live("click", function(){
		nav.current_page.load_next_image();
	});
	
		
	$(".new_bands_next_image").live("click", function(){	
		image = $(this).parent().find('.band_image');
		band_index = image.attr('id').split('_')[1];
		current_image = parseInt(image.attr('rel'));
		if(current_image>=nav.current_page.image_array[band_index].length)
			current_image = 0;
		image.attr('src', nav.current_page.image_array[band_index][current_image].image_url);
		image.attr('rel', current_image+1);
		image.fadeIn();
	});
	
	
	$("#footer .rss").mouseover(function(){
		$("#footer_hover_message").html("assine nosso feed!");
	});
	
	$("#footer .twitter").mouseover(function(){
		$("#footer_hover_message").html("siga-nos no twitter");
	});
	
	$("#footer .facebook").mouseover(function(){
		$("#footer_hover_message").html("torne-se fã no facebook");
	});
	
	$("#footer a").mouseout(function(){
		$("#footer_hover_message").html("");
	});
	
	$(".your_band").click(function(){
		nav.set_hash('add-band/');
	});
	
	$(".remove_song").live("click", function(){
		$(this).parent().parent().fadeOut("slow", function(){
			$(this).remove();	
			$("#song_upload_box").fadeIn();
		});
	});
	
	$("#uploaded_images div").live("click", function(){
		$(this).fadeOut("slow", function(){
			$(this).remove();	
			$("#image_upload_box").fadeIn();
		});
	});
	
	$("#new_band_add .reset_onclick").click(function(){
		$(this).removeClass("reset_onclick");
		$(this).val("");
	});
	
	$("#secret_pl .songs li").live("click", function(){
		o.setSecretPlaylist($(this).parent().attr('rel'), $(this).attr('rel'));
	});
	
	$("#new_bands .new_band_item .listen ul li").live("click", function(){
		o.setNewBandPlayList($(this).parent().attr('rel'), $(this).attr('rel'));
	});
	
	$(".video_label").live("click", function(){
		$('.video_player').remove();
		
		youtube_id = $(this).attr("rel").replace(/^[^v]+v.(.{11}).*/,"$1"); 
		embed_code = "<div class=\"video_player\"><object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http://www.youtube.com/v/"+ youtube_id +"&hl=pt-br&fs=1&\"></param><param name=\"allowFullScreen\" value=\"true\"> 		</param><param name=\"allowscriptaccess\" value=\"always\"></param> 		<embed src=\"http://www.youtube.com/v/"+ youtube_id +"&hl=pt-br&fs=1&\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"425\" height=\"344\"></embed></object></div>";
		$(embed_code).insertAfter($(this));
		
	});

	$("#contact_form .submit").click(function(){
		nav.current_page.send_form();
	});
	
	
	/* NAVIGATION */
	var nav = new WindowNav();
	var current_anchor = null;
	
	function check_anchor(){
		if(current_anchor!=document.location.hash){
			current_anchor = document.location.hash;
			
			if(!current_anchor){
				nav.open(new Home());
			}else{
				hash = document.location.hash.split('#')[1].split('/');		

				if(hash[0]=='artists')
					nav.open(new Artists(hash[1]));
				else if(hash[0]=='add-band')
					nav.open(new AddNewBand());
				else if(hash[0]=='blog'){
					if(hash.length==3)
						nav.open(new Blog(hash[1]));
					else if(hash.length==5){
						if(hash[2]=='archives')
							nav.open(new Blog(hash[1], hash[3].split('-')[1], hash[3].split('-')[0]));
						else
							nav.open(new Blog());
					}else
						nav.open(new Blog());
				}else if(hash[0]=='about')
					nav.open(new About());
				else if(hash[0]=='add-band-ret')
					nav.open(new NewBandAdded());
				else if(hash[0]=='new-bands')
					if(hash.length==4)
						nav.open(new NewBands(hash[1], hash[2]));
					else
						nav.open(new NewBands(hash[1]));
					
				else if(hash[0]=='cases')
					nav.open(new Cases(hash[1]));
				else if(hash[0]=='agenda')
					nav.open(new Agenda());
    			else if(hash[0]=='contact')
    				nav.open(new Contact());
				else if(hash[0]=='namidia')
				if(hash.length==3)
				    nav.open(new NaMidiaLink(hash[1]));
				else
				    nav.open(new NaMidiaLink());
				
				else
					nav.open(new Home());
			}
		}
	}
		

	// id_release
	$('#id_release').each(function(){
	      var length = $(this).val().length;
	      $(this).parent().find('.counter').html( 450 - length + ' caracteres restantes');
	      $(this).keyup(function(){
		      var new_length = $(this).val().length;
		      if(new_length>450){
		    	  $(this).val($(this).val().substring(0, 449));

		      }
		      $(this).parent().find('.counter').html( 450 - new_length + ' caracteres restantes');
	      });
	});
	
	// artists submenu
	var artists_submenu_on = false;
	$('#menu .artists').mouseover(function(){
		if(artists_submenu_on){
			return;
		}
		var pos = $(this).offset();  
		var height = $(this).height();
		var width = $(this).width();
				
		$("#artists_submenu").css( { "left": (pos.left) + "px", "top":pos.top+ height + 10 + "px", "width": width + "px"} );
		$("#artists_submenu").show();
		artists_submenu_on = true;
		
		
	});
	
	$('#menu .artists').mouseleave(function(){
		$("#artists_submenu").hide();
		artists_submenu_on = false;
	})
	

	$(document).everyTime(300, function() {
		check_anchor();
	});
});