function CheckDelete(url, question) {
    Check = confirm(question);
    if (Check == true)
        window.location.href = url;
}

var error_msg = 'Bitte ausfüllen';
var reqClass = 'formcheck';
var errorcol = '#db0000';
var readycol = '#222222';

function FormCheck(form_submit) {

    var is_error=false;
    $$('#'+form_submit.id + ' .'+reqClass).each(function(item) {
        v = $F(item);
        if ( v === '' || v === null || v.length == 0 || v === error_msg || /^\s+$/.test(v) || (item.hasClassName('validate-digits') && /[^\d]/.test(v)) ) {

            
            is_error=true;
            $(item).setStyle({
                color:errorcol
            });
            $(item).value = error_msg;

            new Effect.Pulsate($(item).id);
        } else {
            $(item).setStyle({
                color:readycol
            });
        }

    });

    if (form_submit && !is_error) {
        $(form_submit.id).submit();
    } else {
        return !is_error;
    }
}

function FileFormCheck(form_submit, mediatype) {


    var is_error=false;
    $$('#'+form_submit.id + ' .'+reqClass).each(function(item) {
        v = $F(item);
        if ( v === '' || v === null || v.length == 0 || v === error_msg || /^\s+$/.test(v) || (item.hasClassName('validate-digits') && /[^\d]/.test(v)) ) {


            is_error=true;
            $(item).setStyle({
                color:errorcol
            });
            $(item).value = error_msg;

            new Effect.Pulsate($(item).id);
        } else {
            $(item).setStyle({
                color:readycol
            });
        }
        

        if ( item.type == 'file' ) {
            
            switch (mediatype) {
                case 'Foto':
                    regex = /[jpg|jpeg|JPG|JPEG|gif|GIF|png|PNG]$/;
                    break;
                case 'Video':
                    regex = /[mp4|MP4|avi|AVI|mov|MOV|wmv|WMV]$/;
                    break;
                case 'Audio':
                    regex = /[mp3|MP3]$/;
                    break;
            }
            
            if ( regex.test(item.value) == 0 ) {
                is_error=true;
                alert("Die Datei muss eine "+ mediatype +"-Datei sein");
            }
        }

    });

    if (form_submit && !is_error) {
        ShowUploadLayer();
        $(form_submit.id).submit();
    } else {
        return !is_error;
    }
}

function TippsFormCheck(form_submit, adress) {

    var is_error=false;
    $$('#'+form_submit.id + ' .'+reqClass).each(function(item) {
        v = $F(item);
        if (
            v === '' || v === null || v.length == 0 || v === error_msg || /^\s+$/.test(v)
            || (item.hasClassName('validate-digits') && /[^\d]/.test(v))
            ) {
            is_error=true;
            $(item).setStyle({
                color:errorcol
            });
            $(item).value = error_msg;

            new Effect.Pulsate($(item).id);
        } else {
            $(item).setStyle({
                color:readycol
            });
        }
    });

    if ( !is_error ) {
        var geocoder = new GClientGeocoder();
        geocoder.getLatLng( adress ,function(point) {

            if (point) {
                $('point').value = point.toUrlValue();
                //alert(point);
                $(form_submit.id).submit();
            } else {
                alert('Die Adresse konnte nicht gefunden werden!');
            }
        });
    }
}

function CleanField(item) {
    if(item.value === error_msg) {
        $(item).setStyle({
            color:readycol
        });
        $(item).value = '';
    }

    if(item.type == 'checkbox' && item.checked) {
        $(item).value = true;
    }
}

var W3CDOM = (document.createElement && document.getElementsByTagName);

function initFileUploads() {
    if (!W3CDOM) return;
    var fakeFileUpload = document.createElement('div');
    fakeFileUpload.className = 'fakefile';
    var input = document.createElement('input');
    input.className = 'input_text';
    input.id = 'rfakefile';
    input.readonly = 'readonly';
    fakeFileUpload.appendChild(input);
    var link = document.createElement('a');
    link.href = 'javascript:';
    link.className = 'rfakefile_link';
    link.innerHTML = 'Durchsuchen';
    fakeFileUpload.appendChild(link);
    var x = document.getElementsByTagName('input');
    for (var i=0;i<x.length;i++) {
        if (x[i].type != 'file') continue;
        if (x[i].parentNode.className != 'fileinputs') continue;
        //x[i].className = 'file hidden formcheck';
        var clone = fakeFileUpload.cloneNode(true);
        x[i].parentNode.appendChild(clone);
        x[i].relatedElement = clone.getElementsByTagName('input')[0];
        x[i].onchange = function () {
            this.relatedElement.value = this.value;
        }
        x[i].onmouseout = function () {
            this.relatedElement.value = this.value;
        }
    }
}

function check_upload(value, type){
    
    switch (type) {
        case 'Bild':
            regex = /[jpg|jpeg|JPG|JPEG|gif|GIF|png|PNG]$/;
            break;
        case 'Video':
            regex = /[mp4|MP4]$/;
            break;
        case 'Audio':
            regex = /[mp3|MP3]$/;
            break;
    }
    
    return regex.test(value);
}

function createAudioPlayer (filename, container, width, height, skin) {

    var flashvars = {
        file:'/content/ausflugstipps/Audio/'+filename ,
        skin:skin
    }

    var params = {
        wmode: 'transparent',
        allowfullscreen: 'false',
        usefullscreen: 'false'
    }

    var attributes = {};
    attributes.name = container;

    swfobject.embedSWF('/player/player.swf', container, width, height, '9', false, flashvars, params, attributes);
}

function createYoutubePlayer (youtubeID, container, width, height, skin) {

    
    var flashvars = {
        file: 'http://www.youtube.com/watch?v=' + youtubeID ,
        image: 'http://i.ytimg.com/vi/'+ youtubeID +'/1.jpg',
        skin: skin
    }

    var params = {
        wmode: 'transparent',
        allowfullscreen: 'true'
    }

    var attributes = {};
    attributes.name = container;
    
    swfobject.embedSWF('/player/player.swf', container, width, height, '9', false, flashvars, params, attributes);
}

function createVideoPlayer (file, image, container, width, height, skin) {

    var flashvars = {
        file: file ,
        image: image,
        skin: skin
    }

    var params = {
        wmode: 'transparent',
        allowfullscreen: 'true'
    }
    var attributes = {};
    attributes.name = container;

    swfobject.embedSWF('/player/player.swf', container, width, height, '9', false, flashvars, params, attributes);
}

var width = 0;

function setRating(stars) {
    width = stars * 20;
    $('rating_stars').setStyle( 'width:' + width + 'px' );
    $('stars').value = stars;
}

function setBack() {
    $('rating_stars').setStyle( 'width:' + width + 'px' );
}

function bookmarksite(title,url){
    if (window.sidebar) { // firefox
        window.sidebar.addPanel(title, url, "");
    }
    else if(window.opera && window.print) { // opera
        var elem = document.createElement('a');
        elem.setAttribute('href',url);
        elem.setAttribute('title',title);
        elem.setAttribute('rel','sidebar');
        elem.click();
    }
    else if(document.all)// ie
    {
        window.external.AddFavorite(url, title);
    }
}

var player;

function playerReady(obj) {
    //alert('the videoplayer '+obj['id']+' has been instantiated. Sound: ' + sound);
    player = $(obj['id']);

    if ( sound == 'on' ) {
        player.sendEvent("MUTE","false");
    }

    if ( sound == 'off' ) {
        player.sendEvent("MUTE","true");
    }

    player.addControllerListener("MUTE","muteTracker");
}

function muteTracker(obj) {
    //alert('the new mute state is: '+obj.state);
    if ( obj.state ) {
        $('sound_button').className = 'sound_button_off';
        sound = 'off';
        new Ajax.Request('/index/togglesound/sound/off');
    } else {
        $('sound_button').className = 'sound_button_on';
        sound = 'on';
        new Ajax.Request('/index/togglesound/sound/on');
    }
}

function toggleSound() {
    if ( sound == 'on' ) {
       
        if(player) {
            player.sendEvent("MUTE","true");
        }

        if ( $("myDynamicContent") ) {
            $("myDynamicContent").SoundAnAus(0);
        }

        $('sound_button').className = 'sound_button_off';
        sound = 'off';
        new Ajax.Request('/index/togglesound/sound/off');
    } else {
        
        if(player) {
            player.sendEvent("MUTE","false");
        }

        if ( $("myDynamicContent") ) {
            $("myDynamicContent").SoundAnAus(1);
        }

        $('sound_button').className = 'sound_button_on';
        sound = 'on';
        new Ajax.Request('/index/togglesound/sound/on');
    }
    return false
}



function PopupCenter(pageURL, title,w,h) {
    var left = (screen.width/2)-(w/2);
    var top = (screen.height/2)-(h/2);
    var targetWin = window.open (pageURL, "PopUp", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=0, resizable=no, copyhistory=no, width="+w+", height="+h+", top="+top+", left="+left+", screenX="+left);
}

function setKeywords(keywords)
{
    var meta;
    if (document.getElementsByTagName) {
        meta = document.getElementsByTagName('meta')[2];
        if (meta.name == 'keywords') {
            meta.content = keywords;
        }
    }
}

function setDescription(description)
{
    var meta;
    if (document.getElementsByTagName) {
        meta = document.getElementsByTagName('meta')[1];
        if (meta.name == 'description') {
            meta.content = description;
        }
    }
}

function updateFBSVZLinks(URL, title) {
    var fburl = 'http://www.facebook.com/sharer.php?u=' + URLEncode(URL) + '&t=' + title;
    $('fb_link').href = fburl;

    var vzurl = 'http://www.studivz.net/Suggest/Selection/?u=' + URLEncode(URL) + '&desc=' + title + '&prov=www.leibniz.de';
    $('vz_link').href = vzurl;
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

var position = 'left';

function moveSubnavi(direction, speed) {

    if ( speed == 'fast') {
        var dur = 0.01;
    } else {
        var dur = 1;
    }

    if (direction == 'right' && position == 'left') {
        new Effect.Move('sub_nav_container', {duration: dur, y:0, x:-950});
        position = 'right';
        $('subnav_left').className = '';
        $('subnav_right').className = 'active';
    }

    if (direction == 'left' && position == 'right') {
        new Effect.Move('sub_nav_container', {duration: dur, y:0, x:+950});
        position = 'left';
        $('subnav_left').className = 'active';
        $('subnav_right').className = '';
    }
}

function ShowLoader() {
        var layer_width = 500;

	//an den anfang der Seite springen
	self.scrollTo(0,0);

	hgesamt = document.getElementById('cms_page').offsetHeight;

	var arrayPageSize = document.viewport.getDimensions();
  	$('my_overlay').setStyle({ width: arrayPageSize.width + 'px', height: hgesamt + 'px' });

        new Effect.Appear($('my_overlay'), { duration: 0.2, from: 0.0, to: 0.6 });

	$('ytloader').setStyle({
  	left: parseInt((document.viewport.getWidth()-layer_width)/2)+'px',
  	top: parseInt(300)+'px'
	});
	new Effect.Appear($('ytloader'), { duration: 0.2 });
}
