// Bookmark
var url='http://www.d0g.de'
var title='www.d0g.de - cause we LIKE it doggy style'
function bookmark()
{
    window.external.AddFavorite(url,title);
}
function sendmailto(mail1,mail2)
{
    document.location.replace('mailto:'+mail1+'@'+mail2);
}

var vote;
var plug;
Event.observe(window, 'load', function() {
	vote = new Vote() ;
	plug = new Plug() ;
	var isnew = window.location.search;
	if(isnew != ''){
	    if(isnew.slice(1,4) == 'new'){
	        alert('Klicke jetzt auf "Gefällt mir".');
	    }
	}
} );


var Vote = Class.create({
	initialize: function() {
        this.requestURL = '/ajax/vote.php';
        this.requestMethod = 'POST';
        return this;
	},

	_do: function(id,v) {
        $('vote_0').innerHTML = '<img src="/images/vote_00.gif" class="voting" />';
        $('vote_1').innerHTML = '<img src="/images/vote_10.gif" class="voting" />';

		new Ajax.Request(this.requestURL, {
			method: this.requestMethod,
			parameters: 
			    { 
			        id : id, 
			        todo: v 
                },
		    onSuccess: function(response,json) {
				var data = response.responseText.evalJSON(true);
                if(data){
                    // Text Update
                    $('bewertung').innerHTML = data['rating'];
                    $('votes').innerHTML = data['votes'];
                    // Stars Update
                    for (var i = 1; i <= 5; i++){
                        if(i <= Math.round(data['rating'])){
                            $('star_'+i).innerHTML = '<img src="/images/star_full.gif" class="votingstar" />';
                        }else{
                            $('star_'+i).innerHTML = '<img src="/images/star_empty.gif" class="votingstar" />';
                        }
                    }
                }
			}.bind(this)
	    }); 
	}
}) ;

var Plug = Class.create({
	initialize: function() {
        this.requestURL = '/ajax/add.php';
        this.requestMethod = 'POST';
        return this;
	},

	_add: function() {
	    if($('spruch').value == ''){
	        alert('Ohne Spruch geht nichts!');
	        return;
	    }
	    $('submit').value = 'Bitte warten ...';

		new Ajax.Request(this.requestURL, {
			method: this.requestMethod,
			parameters: { spruch: $('spruch').value },
			onSuccess: function(response,json) {
				var data = response.responseText.evalJSON(true);
                if(!data){
                    location.replace('http://'+window.location.host);
                }else{
                    location.replace('http://'+window.location.host+'/'+data+'.html?new');
                }
			}.bind(this)
	    });
	}
}) ;
