var VIDEO_ICON_ON = "/images/structure/bt_arrow_green_videos_on.gif";
var VIDEO_ICON_OFF = "/images/structure/bt_arrow_green_videos.gif";
var largePhotoOpen = false;


/**
* Sidebarnavigation
*/
var SidebarNav = {
    navElements : ["Video","Band","Lyrics","Comments","Photo"],
    openElement : "Video"
};

function toggleSidebarNav(id,objRef){
    if (id == SidebarNav.openElement){
        return;
    }
	resetHeadlines();
	objRef.className = "on";


    //getParentHeadline(id).className = "on";
    for (var i = 0; i < SidebarNav.navElements.length; i++){
       if (SidebarNav.navElements[i] == id){
           $(SidebarNav.navElements[i]).style.display = "block";
		   //$(SidebarNav.navElements[i]).style.display = "none";
		  // new Effect.BlindDown(id);
		  initializeScroller(id);
        } else {
            $(SidebarNav.navElements[i]).hide();



        }

    }

    SidebarNav.openElement = id;
    initializeScroller(id);
}


function resetHeadlines(){
var theHeadlines =  $('SidebarNavigation').getElementsByTagName('H2');
	for (var i = 0; i < theHeadlines.length; i++){
		theHeadlines[i].className = "off";
	}

}


/**
* Scrollbuttons
*
*/
function ScrollButton(inFrame,inContent) {
          this.frame = inFrame;
          this.content = inContent;
          var _self = this;
          this.isActive = false;
          this.scrolledDistance = 0;
          this.stepLength = 6;

   }
       ScrollButton.prototype.startScroll = function(inDirection){
           this.content.style.top = $(this.content).getStyle("top");
           this.distanceToScroll = this.content.scrollHeight - parseInt(this.frame.getStyle("height"));

           this.isActive = true;
            if (inDirection == "up"){
                this.scrollUp();
            } else {
                this.scrollDown();
            }

       }

       ScrollButton.prototype.scrollUp =  function() {
           var ref = this;
           if (!ref.isActive || ref.scrolledDistance >= ref.distanceToScroll){

            return;
           }
           this.scrolledDistance += ref.stepLength;

           setTimeout(function(){

               ref.content.style.top = parseInt(ref.content.style.top) - ref.stepLength + "px";

               ref.scrollUp();
           },5);
        }

        ScrollButton.prototype.scrollDown =  function() {
           var ref = this;
           if (!ref.isActive || ref.scrolledDistance <= 0) {
           	return;
           }
           this.scrolledDistance -= ref.stepLength;
           setTimeout(function(){

                ref.content.style.top = parseInt(ref.content.style.top) + ref.stepLength + "px";
                ref.scrollDown();
           },5);
        }

        ScrollButton.prototype.stopScroll = function(){
           this.isActive = false;
        }



		function initializeScroller(inElement){
			try {
			   if ($(inElement).getHeight() <= $(inElement + "Content").getHeight()){
					$(inElement + "Scroll").style.display = "block";
			   }
			} catch (Exception){
			}
		}


	function handleTellAFriend(){
		if ($('ContactUsForm').visible){
		new Effect.BlindUp('ContactUsForm',{duration: 0.5});
	}
	//shadow();
		if (!$('TellAFriendForm').visible()){
			new Effect.BlindDown('TellAFriendForm',{duration: 0.5});

		} else {
			new Effect.BlindUp('TellAFriendForm',{duration: 0.5});
		}
	}



/** Video functions */

function playVideo(inBand,inVideo,inTitle,inId){
	var theVideos = document.getElementsByClassName("video");
    for (var i = 0; i < theVideos.length; i++){
        theVideos[i].getElementsByTagName("IMG")[1].src = VIDEO_ICON_OFF;
        theVideos[i].getElementsByTagName("IMG")[2].style.display = "none";
    }
    $(inId).getElementsByTagName("IMG")[2].style.display = "block";
    $(inId).getElementsByTagName("IMG")[1].src = VIDEO_ICON_ON;

    setVideoTitle(inTitle);
	$('Player').src = "/flvplayer/flashvideoplayer.html?video="  + inBand + "/" + inVideo + ".flv";
	//console.log("/flvplayer/flashvideoplayer.html?video=/videos/"  + inBand + "/" + inVideo + ".flv");

}


function setVideoTitle(inVideo){
    $('Subtitle').innerHTML = "/&nbsp; " + inVideo;
 }

 function showArchive(){
 	new Effect.Appear('Archiv');
 	theHeight = $('Archiv').getDimensions()["height"];
 	$('Archiv').setStyle({height: theHeight + "px"});
 }




function removePreview(){
    if ($('MovieContainer').getElementsByTagName("IMG").length > 0)
        $('MovieContainer').removeChild($('MovieContainer').getElementsByTagName("IMG")[0]);

}

/**
* show Photos and hide others:
*/
function enlargePic(inPic){
    if (largePhotoOpen) {
        hidePic();
        return;
    }


    var theLargePic = "<div><a href=\"javascript:hidePic();\" onfocus=\"this.blur();\"><img src=\"/images/structure/icon_close.gif\" alt=\"\" style=\"margin:13px 0 0 302px;\" /></a>" +
    "<a href=\"javascript:void(null);\" onclick=\"hidePic();\" onfocus=\"this.blur();\"><img src=\"/images/content/bands/large/" + inPic + "\" alt=\"\" id=\"largePic\" /></a></div>";

    largePhotoOpen = true;
    $('SinglePhoto').innerHTML = theLargePic;
    new Effect.Appear("SinglePhoto");


}

function hidePic(){

    new Effect.Fade("SinglePhoto");
    largePhotoOpen = false;
}





/**
* Tell a friend and Validation
*/
function tellAFriendAction(){
      var sender_email = $F('sender_email');
      var sender_name = $F('sender_name');
      var receiver_email = $F('receiver_email');
      var receiver_name = $F('receiver_name');
      var current_url = $F('current_url');

      if (!_validateTellAFriendForm()) return;

      var theUrl = "sender_email="+ encodeURIComponent(sender_email) + "&sender_name="+ encodeURIComponent(sender_name) + "&receiver_name="+ encodeURIComponent(receiver_name) + "&receiver_email="+encodeURIComponent(receiver_email) + "&current_url="+encodeURIComponent(current_url);


	new Ajax.Updater("TellAFriendForm","/ajax/tell_a_friend_action.php?" + theUrl,{
        method:"get",
		onComplete: function(){
		  setTimeout(function(){
            new Effect.BlindUp('TellAFriendResponse');
            },
            3000);
	   }
    }
   )

}

/**
 *  überprüft das "Tell A Friend Formular und setzt Feldermeldungen
 * @private
 */
function _validateTellAFriendForm() {

    noErrors = true;
    try {
        if ($F("sender_name") == "") {
            Element.addClassName("sender_name", "error");
            noErrors = false;

        } else {
            removeError("sender_name");
        }
        if ($F("sender_email") == "") {
            Element.addClassName("sender_email", "error");


        } else if (!_isValidEmail($F("sender_email"))) {
            Element.addClassName("sender_email", "error");
            noErrors = false;
        } else {
            removeError("sender_email");
        }

        if ($F("receiver_email") == "E-Mail") {
            Element.addClassName("receiver_email", "error");
            noErrors = false;
        } else if (!_isValidEmail($F("receiver_email"))) {
            Element.addClassName("receiver_email", "error");
            noErrors = false;
        } else {
            removeError("receiver_email");
        }
        if ($F("receiver_name") == "") {
            Element.addClassName("receiver_name", "error");
            noErrors = false;

        } else {
            removeError("receiver_name");
        }
    } catch(e) {
        alert("Validation error: " + e);
    }


    return noErrors;
}





/**
* Comments an Validation
*/
function commentAction(){
      var user = $F('comment_user');
      var email = $F('comment_email');
      var comment = $F('comment');
      var band = $F('bandName');

      var theUrl = "user="+ encodeURIComponent(user) + "&email="+ encodeURIComponent(email) + "&comment="+ encodeURIComponent(comment) + "&band=" + encodeURIComponent(band);
      if (!validateCommentForm()) return;
      $('CommentsForm').reset();

    new Ajax.Updater("ActiveComments","/ajax/comment_action.php?" + theUrl,{
        method:"get",
        onComplete: function(){
          setTimeout(function(){
            $('CommentsForm').reset();
            toggleCommentsContent('Comments');
            },
            3000);
       },insertion: Insertion.Before
    }
   )

}

function validateCommentForm(){
    noErrors = true;
    try {
        if ($F("comment_user") == "") {
            Element.addClassName("comment_user", "error");
            noErrors = false;

        } else {
            removeError("comment_user");
        }
        if ($F("comment_email") == "") {
            Element.addClassName("comment_email", "error");


        } else if (!_isValidEmail($F("comment_email"))) {
            Element.addClassName("comment_email", "error");
            noErrors = false;
        } else {
            removeError("comment_email");
        }

        if ($F("comment") == "") {
            Element.addClassName("comment", "error");
            noErrors = false;
        } else {
            removeError("comment");
        }

    } catch(e) {
        alert("Validation error: " + e);
    }


    return noErrors;
}





function toggleCommentsContent(inItem){
   if (inItem == 'Comments'){
    $('CommentItems').show();
    $('CommentsFormContainer').hide();
   } else {
    $('CommentItems').hide();
    $('CommentsFormContainer').show();
   }
}


function _isValidEmail(sText) {
    var reEmail = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@([_a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})$/;
    return reEmail.test(sText);

}

/**
 * Entfernt CSS Fehlerklasse
 * @param String die ID
 * @private
 */
function removeError(inId, inOptionalClass) {
    try {
        Element.removeClassName(inId, "error");
    }
    catch (e) {
    }

}


function handleContactUs(){
	if ($('TellAFriendForm').visible){
		new Effect.BlindUp('TellAFriendForm',{duration: 0.5});
	}
	if (!$('ContactUsForm').visible()){
			new Effect.BlindDown('ContactUsForm',{duration: 0.5});

		} else {
			new Effect.BlindUp('ContactUsForm',{duration: 0.5});
		}
}

function _validateContactForm(){
    noErrors = true;
    try {
        if ($F("contact_name") == "") {
            Element.addClassName("contact_name", "error");
            noErrors = false;

        } else {
            removeError("contact_name");
        }
        if ($F("contact_email") == "") {
            Element.addClassName("contact_email", "error");


        } else if (!_isValidEmail($F("contact_email"))) {
            Element.addClassName("contact_email", "error");
            noErrors = false;
        } else {
            removeError("contact_email");
        }
		 if ($F("contact_message") == "") {
            Element.addClassName("contact_message", "error");


        }

    } catch(e) {
        alert("Validation error: " + e);
    }


    return noErrors;
}


function contactUsAction(){
      var contact_email = $F('contact_email');
      var contact_name = $F('contact_name');
      var contact_message = $F('contact_message');

      if (!_validateContactForm()) return;

      var theUrl = "contact_email="+ encodeURIComponent(contact_email) + "&contact_name="+ encodeURIComponent(contact_name) + "&contact_message="+ encodeURIComponent(contact_message);


	new Ajax.Updater("ContactUsForm","/ajax/contact_action.php?" + theUrl,{
        method:"get",
		onComplete: function(){
		  setTimeout(function(){
            new Effect.BlindUp('ContactUsResponse');
            },
            3000);
	   }
    }
   )

}

