var musictotalFrontEndClass = {	
	init:function(){
		this.initArtistDetailTab();
		this.initEmailLinks();
		this.initDialog();
		this.initRating();
		this.initInlineLabel();
		this.initLightbox();
	},
	initArtistDetailTab:function(){		
		jQuery('.wrapper-artist-tab').hide();
		
		// show the corresponding tab on load
		if (window.location.hash.length>0)
		{						
			jQuery('.wrapper-artist-'+window.location.hash.substring(1)).show();
			jQuery('.nav-artist-detail > li > a').each(function(){
				if (jQuery(this).attr('href')==window.location.hash)
				{
					jQuery(this).parent('li').addClass('active');
				}
			});
		}
		else
		{
			jQuery('.wrapper-artist-home').show();
		}
		jQuery('.nav-artist-detail > li > a').click(function(event){		
			
			jQuery('.wrapper-artist-tab').hide();
			jQuery('.wrapper-artist-'+jQuery(this).attr('href').substring(1)).show();
			jQuery('.nav-artist-detail > li').removeClass('active');
			jQuery(this).parent('li').addClass('active');
		});				
	},
	initEmailLinks:function(){
		jQuery('.js-create-mail-link').each(function(){
			var arrMail=jQuery(this).html().split('[at]');
			jQuery(this).replaceWith('<a href="mailto:'+arrMail[0]+'@'+arrMail[1]+'">'+arrMail[0]+'@'+arrMail[1]+'</a>');
		});
	},
	initDialog:function(){
		jQuery('.js-show-dialog-rating').click({frontedClass:this},function(evt){
			if (jQuery('#dialog-rating').hasClass('js-dialog-rating-none'))
			{
				evt.data.frontedClass.showDialogRatingNone();
			}
			if (jQuery('#dialog-rating').hasClass('js-dialog-rating-artist'))
			{
				evt.data.frontedClass.showDialogRatingArtist();
			}
			if (jQuery('#dialog-rating').hasClass('js-dialog-rating-organiser'))
			{
				evt.data.frontedClass.showDialogRatingOrganiser();
			}			
		});
	},
	showDialogRatingNone:function(){
		jQuery( "#dialog-rating" ).dialog({
			resizable: false,
			minHeight:150,
			width:400,
			draggable:false,
			modal: true,
			buttons: {
				"Jetzt registrieren": function() {
					location.href="index.php?article_id=36";
				},
				"Einloggen": function() {
					location.href="index.php?article_id=99&return_to_artist="+musictotalFrontEndClass.getURLParameter('userid');
					jQuery( this ).dialog( "close" );
				},
				"Abbrechen": function() {
					jQuery( this ).dialog( "close" );
				}
			}
		});
	},
	showDialogRatingArtist:function(){
		jQuery( "#dialog-rating" ).dialog({
			resizable: false,
			minHeight:150,
			width:400,
			draggable:false,
			modal: true,
			buttons: {				
				"Abbrechen": function() {
					jQuery( this ).dialog( "close" );
				}
			}
		});
	},
	showDialogRatingOrganiser:function(){
		jQuery( "#dialog-rating" ).dialog({
			resizable: false,
			minHeight:150,
			width:590,
			draggable:false,
			modal: true,
			buttons: {
				"Bewertung abgeben": function() {
					musictotalFrontEndClass.submitVote();
				},				
				"Abbrechen": function() {
					jQuery( this ).dialog( "close" );
				}
			}
		});
	},
	initInlineLabel:function(){
		jQuery('.js-inline-label').inline_label();		
	},
	initRating:function(){
		jQuery(".starify").stars({cancelShow: false});
		jQuery("#ratings").submit(function(event){
			event.preventDefault();
			
		});
	},
	submitVote:function()
	{
		var d=new Date();
		jQuery('.ui-button:first').addClass('button-snake');
		
		jQuery.post("index.php?article_id=98&rand="+d.getTime(),jQuery('#ratings').serialize(),function(data) {			
			var result=jQuery.parseJSON(data);			
			if (result.status=='success')
			{
				location.reload();
			}
			else
			{				
				jQuery('.rating-error').hide();
				jQuery.each(result.arrError,function(intIndex,strValue){
					jQuery('#'+strValue+'_error').show();
				});				
				jQuery('.ui-button:first').removeClass('button-snake');
			}
						
		 }
		);
	},
	initLightbox:function(){
		jQuery('.js-open-lightbox').fancybox();
	},
	getURLParameter:function(name) {
	    return unescape(
	        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
	    );
	}
};

jQuery(document).ready(function() {
	musictotalFrontEndClass.init();
});

