function kursBlendIn(kurs){
    $('kurs_'+kurs+'_short').slideUp();
    $('kurs_'+kurs+'_full').slideDown();
}

function kursBlendOut(kurs){
    $('kurs_'+kurs+'_short').slideDown();
    $('kurs_'+kurs+'_full').slideUp();
}

function trainerBlendIn(trainer){
    $('trainer_'+trainer+'_short').slideUp();
    $('trainer_'+trainer+'_full').slideDown();
}

function trainerBlendOut(trainer){
    $('trainer_'+trainer+'_short').slideDown();
    $('trainer_'+trainer+'_full').slideUp();
}

var map = null;
var overlays = [];

// Creates a marker at the given point with the given number label
function createMarker(point, number) {
  var marker = new GMarker(point);
  //GEvent.addListener(marker, "click", function() {
  //  marker.openInfoWindowHtml(number);
  //});
  return marker;
}

function load(latitude,longitude,location,zoom) {
  if (GBrowserIsCompatible()) {
    if(!zoom){
        zoom = 13;
    }

    var map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(latitude, longitude), zoom);

    var point = new GLatLng(latitude,longitude);
    map.addOverlay(createMarker(point,location));
  }
}


/***
 *  @purpose    Login processor
 ***/
function login(fromsignup){
    (new AjaxRequest).send({
        url     : "/ajax/index.php",
        data    : {
            action: 'login',
            mail: $('email').getValue(),
            pass: $('pw').getValue()
        },

        onSuccess  : function(data, info)
        {
            if(!data.ok){
                $('errorMessage').blindDown();
                setTimeout(function(){ $('errorMessage').blindUp(); }, 3000);
                Effect.Shake('button');
                $('email').activate();
                $('email').addClassName('inputError');
                $('pass').addClassName('inputError');
            }else{
                if(fromsignup){
                    window.location.href = 'signup.html?part=confirm';
                }else{
                    window.location.reload();
                }
            }
        }
      });

    return;
}


/***
 *  @purpose    Repass
 ***/
function repass(){
    if($('email').getValue()){
        (new AjaxRequest).send({
            url     : "/ajax/index.php",
            data    : {
                action: 'repass',
                mail: $('email').getValue()
            },

            onSuccess  : function(data, info)
            {
                $('confirmMessage').blindDown();
                setTimeout(function(){ $('confirmMessage').blindUp(); }, 5000);
            }
          });
    }

    return;
}


/***
 *  Change password processor
 ***/
function changePassword(){
    if($('newpass').getValue()){
        if($('newpass').getValue() == $('newpass2').getValue()){
            (new AjaxRequest).send({
                url     : "/ajax/index.php",
                data    : {
                    action: 'changePassword',
                    pass: $('newpass').getValue()
                },

                onSuccess  : function(data, info)
                {
                    $('confirmMessage').slideDown();
                    $('changePasswordForm').slideUp();
                }
              });
        }else{
            $('errorMessage').blindDown();
            setTimeout(function(){ $('errorMessage').blindUp(); }, 3000);
            Effect.Shake('button');
        }
    }

    return;
}


/***
 *  Load cities for a given country
 ***/
function loadCities(obj, fid){
    if(obj.value){
        $('scity').appear();
        (new AjaxRequest).send({
            url     : "/ajax/index.php",
            data    : {
                action: 'loadCities',
                cat_id: obj.value,
                filter: fid
            },

            onSuccess  : function(data, info)
            {                
                selectClear($('city'));        
                selectAdd($('city'),'Alle','');   
                if(data.subcats){
                    for(var i=0; i<data.subcats.length; i++){
                        selectAdd($('city'),data.subcats[i].name,data.subcats[i].value);   
                    }
                }
            }
          });
        
    }else{
        $('scity').hide();
    }
}


/*
    --- COMMENTS ---

 */
function sendComment(site){
    var error = false;

    if(!$('senderComment').value){
        $('senderComment').addClassName('error');
        error = true;
    }

    if(!$('senderMail').value){
        $('senderMail').addClassName('error');
        error = true;
    }

    if(!$('senderName').value){
        $('senderName').addClassName('error');
        $('senderName').focus();
        error = true;
    }


    if(!error){
        $('sendButton').disabled = true;
        $('sendButton').value    = 'Please Wait...';

        (new AjaxRequest).send({
            url     : "/ajax/index.php",
            data    : {
                action: 'postComment',
                site_id: site,
                name: encodeURIComponent($('senderName').value),
                mail: $('senderMail').value,
                comment: encodeURIComponent($('senderComment').value)
            },

            onSuccess  : function(data, info)
            {
                $('sendButton').disabled = false;
                $('senderComment').value = '';
                window.location.hash = 'comment'+data.comment_id;
                window.location.reload();
            }
          });
    }
}

function checkComment(){
    if($('senderComment').value){
        $('senderComment').removeClassName('error');
    }

    if($('senderMail').value){
        $('senderMail').removeClassName('error');
    }

    if($('senderName').value){
        $('senderName').removeClassName('error');
    }
}

function preDropComment(cid){
    $('preDelete'+cid).hide();
    $('delete'+cid).show();
}

function dropComment(cid,sid){
    (new AjaxRequest).send({
        url     : "/ajax/index.php",
        data    : {
            action: 'dropComment',
            comment_id: cid,
            site_id: sid
        },

        onSuccess  : function(data, info)
        {
            $('comment_'+cid).hide();

            if(!data.comments == '0'){
                $('comments').hide();
            }
        }
      });
}

function voteup(sid){
    $('ilike').slideUp();
    $('youand').appear();


    (new AjaxRequest).send({
        url     : "/ajax/index.php",
        data    : {
            action: 'vote',
            vote: 'up',
            site_id: sid
        },

        onSuccess  : function(data, info)
        {

        }
      });
}

function votedown(sid){
    $('cast_vote').hide();
    $('cast_vote_down').show();

    (new AjaxRequest).send({
        url     : "/ajax/index.php",
        data    : {
            action: 'vote',
            vote: 'down',
            site_id: sid
        },

        onSuccess  : function(data, info)
        {

        }
      });
}


function saveBLP(sid){
    (new AjaxRequest).send({
        url     : "/ajax/index.php",
        data    : {
            action: 'saveblp',
            from: $('published_from').getValue(),
            until: $('published_until').getValue(),
            partner: $('blp').checked,
            site_id: sid
        },

        onSuccess  : function(data, info)
        {
            $('confirmMessage').blindDown();
            setTimeout(function(){ $('confirmMessage').blindUp(); }, 5000);
        }
      });

    return;
}


function dropSite(id){
    if(confirm('Wenn Sie ok klicken, wird Ihr Eintrag unwiderruflich gelöscht. Sind Sie sicher?')){
        (new AjaxRequest).send({
            url     : "/ajax/index.php",
            data    : {
                action: 'dropSite',
                sid: id
            },
    
            onSuccess  : function(data, info)
            {
                $('site_'+id).blindUp(); 
            }
          });
    }
}


function addToCart(aid){
    (new AjaxRequest).send({
        url     : "/ajax/index.php",
        data    : {
            action: 'addToCart',
            quantity: $('quant').value,
            article_id: aid
        },

        onSuccess  : function(data, info)
        {
            $('addtocart').blindUp(); 
            
            //if(!$('totarts').innerHTML){
            $('scinfo').blindUp();
            
            if($('scitems').style.display == 'none'){
                $('scitems').blindDown();
            }
            
            $('totarts').innerHTML = data.totalArts;
        }
      });
}


function removeFromCart(aid){
    (new AjaxRequest).send({
        url     : "/ajax/index.php",
        data    : {
            action: 'removeFromCart',
            article_id: aid
        },

        onSuccess  : function(data, info)
        {
            window.location.reload();
        }
      });
}


function createShopOrder(){
    var inputs  = $$('.inputMandatory');
    var clean   = true;

    for(var i = 0; i < inputs.length; i++){
        if((inputs[i].type == 'checkbox' && !inputs[i].checked) || (!inputs[i].value && inputs[i].offsetHeight)){ //check only visible objects
            inputs[i].addClassName('inputError');
            clean = false;
        }else{
            inputs[i].removeClassName('inputError');
        }
    }
    
    if(clean){
        $('orderform').submit();
    }else{
        $('errorMessage').blindDown();
        setTimeout(function(){ $('errorMessage').blindUp(); }, 3000);
            
        Effect.Shake('button');
        return false;
    }
}
