var BrowserOK=false;
var pics;
var a;
var clin;
var allecartoons_loaded=false;
var suche_loaded=false;
var ecardError = false;

browserOK=true;
pics = new Array();

var objCount=0;

        function preload(name, first, second)
{
if(browserOK)
        {
        pics[objCount] = new Array(3);
        pics[objCount][0] = new Image();
        pics[objCount][0].src = first;
        pics[objCount][1]=new Image();
        pics[objCount][1].src = second;
        pics[objCount][2]=name;
        objCount ++;
        }
}

function on(name){
        if (browserOK){
        for (i=0; i < objCount; i++) {
         if (document.images[pics[i][2]] !=null)
          if (name !=pics[i][2]) {
           if (pics[i][0].complete)
            document.images[pics[i][2]].src = pics[i][0].src;
} else {
if (pics[i][1].complete)
    document.images[pics[i][2]].src=pics[i][1].src;
  }
 }
 }
}

function off(){
 if (browserOK) {
  for (i=0; i < objCount; i++){
        if (document.images[pics[i][2]] !=null)
         if (pics[i][0].complete)
          document.images[pics[i][2]].src = pics[i][0].src;
}
}
}

preload("s1", "http://static.nichtlustig.de/sys_img/spacer.gif", "http://static.nichtlustig.de/sys_img/"+language+"/chara/lemming.gif");
preload("s2", "http://static.nichtlustig.de/sys_img/spacer.gif", "http://static.nichtlustig.de/sys_img/"+language+"/chara/yeti.gif");
preload("s3", "http://static.nichtlustig.de/sys_img/spacer.gif", "http://static.nichtlustig.de/sys_img/"+language+"/chara/dino.gif");
preload("s4", "http://static.nichtlustig.de/sys_img/spacer.gif", "http://static.nichtlustig.de/sys_img/"+language+"/chara/riebmann.gif");
preload("s5", "http://static.nichtlustig.de/sys_img/spacer.gif", "http://static.nichtlustig.de/sys_img/"+language+"/chara/ausserirdische.gif");
preload("s6", "http://static.nichtlustig.de/sys_img/spacer.gif", "http://static.nichtlustig.de/sys_img/"+language+"/chara/arzt.gif");
preload("s7", "http://static.nichtlustig.de/sys_img/spacer.gif", "http://static.nichtlustig.de/sys_img/"+language+"/chara/ente.gif");
preload("s8", "http://static.nichtlustig.de/sys_img/spacer.gif", "http://static.nichtlustig.de/sys_img/"+language+"/chara/geburtstag.gif");
preload("s9", "http://static.nichtlustig.de/sys_img/spacer.gif", "http://static.nichtlustig.de/sys_img/"+language+"/chara/tod.gif");
preload("s10", "http://static.nichtlustig.de/sys_img/spacer.gif", "http://static.nichtlustig.de/sys_img/"+language+"/chara/pudel.gif");
preload("s11", "http://static.nichtlustig.de/sys_img/spacer.gif", "http://static.nichtlustig.de/sys_img/"+language+"/chara/nilpferd.gif");
preload("s12", "http://static.nichtlustig.de/sys_img/spacer.gif", "http://static.nichtlustig.de/sys_img/"+language+"/chara/professoren.gif");
preload("s13", "http://static.nichtlustig.de/sys_img/spacer.gif", "http://static.nichtlustig.de/sys_img/"+language+"/chara/weihnachten.gif");
preload("s14", "http://static.nichtlustig.de/sys_img/toon/suche_input_lupe.gif", "http://static.nichtlustig.de/sys_img/toon/suche_input_lupe2.gif");



function preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}


	bounceb = new Array();
  var first=1;
  
  function boing(elm) {
	
	telm = "sub_" + elm;
	document.images[telm].src  = "http://static.nichtlustig.de/sys_img/" + language + "/toon/" + elm + ".png";
	}
	
		function boingout(elm) {
  
    bounceb[elm]="";
    document.images[telm].src  = "http://static.nichtlustig.de/sys_img/spacer.gif";
  
  }

  function icon(elm) {
	document.getElementById(elm).style.display='inline';
	}

  function icoff(elm) {
	document.getElementById(elm).style.display='none';
	}

function ajax_allecartoons(page,datum) {
	if( !allecartoons_loaded ) {
	 	new Ajax.Updater('alleCartoonsinner', '../sys_inc/allecartoons.php', {parameters:'page='+page+'&datum='+datum,evalScripts:true,asynchronous:true});
	 	allecartoons_loaded = true;
	}
}

function ajax_sucheLayer(cartoonDate) {
	if( !suche_loaded ) {
	 	new Ajax.Updater('sucheinner', '../ajax/CartoonSearch.php', {parameters:'cartoonDate='+cartoonDate,evalScripts:true,asynchronous:true});
	 	suche_loaded = true;
	}
}

function sucheLayer(suche) {
	if( suche.length == 0 ) {
	 	new Ajax.Updater('sucheinner', '../ajax/CartoonSearch.php', {parameters:'neueSuche=1',evalScripts:true,asynchronous:true});
	} else {
	 	new Ajax.Updater('sucheinner', '../ajax/CartoonSearch.php', {parameters:'such='+suche,evalScripts:true,asynchronous:true});
	}
}

function sucheSlider(page, pages, numresults) {
    
                var oldvSuche=page;
                
                toonSucheList = new Array();
                
                for( var i=0 ; i<=pages ; i++ ) {
                    if( i == page ) {
                        toonSucheList.push(new Array("toonSucheList"+i,"1","1"));
                    } else {
                        toonSucheList.push(new Array("toonSucheList"+i,"0","0"));
                    }
                }
                
                var tmpSuche = document.getElementById('carousel-list-suche').innerHTML;
                document.getElementById('carousel-list-suche').innerHTML = "";
                for( var i=0 ; i<page ; i++ ) {
                    document.getElementById('carousel-list-suche').innerHTML += '<li id="'+toonSucheList[i][0]+'"><table><tr><td><img src="http://static.nichtlustig.de/sys_img/toon/thumb-loader.gif" width="65" height="65" border="0"></td><td><img src="http://static.nichtlustig.de/sys_img/toon/thumb-loader.gif" width="65" height="65" border="0"></td><td><img src="http://static.nichtlustig.de/sys_img/toon/thumb-loader.gif" width="65" height="65" border="0"></td><td><img src="http://static.nichtlustig.de/sys_img/toon/thumb-loader.gif" width="65" height="65" border="0"></td><td><img src="http://static.nichtlustig.de/sys_img/toon/thumb-loader.gif" width="65" height="65" border="0"></td><td><img src="http://static.nichtlustig.de/sys_img/toon/thumb-loader.gif" border="0"></td><td><img src="http://static.nichtlustig.de/sys_img/toon/thumb-loader.gif" width="65" height="65" border="0"></td></tr></table></li>';
                }
                document.getElementById('carousel-list-suche').innerHTML += tmpSuche;
                new Effect.MoveBy('carousel-clip-region-suche', 0, -566*page, {duration:0.0});
        
            if( pages == 0 ) {
                pagesSlider=0;
            } else {
                pagesSlider = pages-1;
            }
        
            sliderSuche = new Control.Slider('handleSuche','sliderSuche',{
              sliderValue:page, range:$R(0,pagesSlider),
              onSlide:function(v){
                v = Math.round(v);
                if( v<0 ) {
                    v=0;
                }
                var ende=(v*7)+7; 
                if( ende > numresults ) {
                    ende = numresults;
                }
                if( v == 0 && ende == 0 ) {
                    document.getElementById('suchepos').value = 0;
                } else {
                    document.getElementById('suchepos').value = ((v*7)+1)+' - '+ende;
                }
              },
              onChange:function(v,slider){
                        slider.setDisabled();
                v = Math.round(v);
                var ende=(v*7)+7; 
                if( ende > numresults ) {
                    ende = numresults;
                }
                if( v < 0 ) {
                    v=0;
                }
                document.getElementById('suchepos').value = ((v*7)+1)+' - '+ende;
                if( oldvSuche < v ) {
                    // elemente dranbappen, wenn sie noch nich geladen sind
                    for( var i=oldvSuche+1 ; i<=v ; i++ ) {
                        if( toonSucheList[i][1] == "0" ) {
                            document.getElementById('carousel-list-suche').innerHTML += '<li id="'+toonSucheList[i][0]+'"><table><tr><td><img src="http://static.nichtlustig.de/sys_img/toon/thumb-loader.gif" width="65" height="65" border="0"></td><td><img src="http://static.nichtlustig.de/sys_img/toon/thumb-loader.gif" width="65" height="65" border="0"></td><td><img src="http://static.nichtlustig.de/sys_img/toon/thumb-loader.gif" width="65" height="65" border="0"></td><td><img src="http://static.nichtlustig.de/sys_img/toon/thumb-loader.gif" width="65" height="65" border="0"></td><td><img src="http://static.nichtlustig.de/sys_img/toon/thumb-loader.gif" width="65" height="65" border="0"></td><td><img src="http://static.nichtlustig.de/sys_img/toon/thumb-loader.gif" border="0"></td><td><img src="/sys_img/toon/thumb-loader.gif" width="65" height="65" border="0"></td></tr></table></li>';
                            toonSucheList[i][1] = "1";
                        }
                    }
                }
                        
                        if( toonSucheList[v][2] == "0" ) {
                            new Ajax.Updater('toonSucheList'+v, '../ajax/CartoonSearch.php', { method:'post',parameters:'start='+(7*v),evalScripts:true,asynchronous:true});            
                            toonSucheList[v][2] = "1";                  
                        }
                        new Effect.MoveBy('carousel-clip-region-suche', 0, 566*(oldvSuche-v),{ afterFinish: function(){slider.setEnabled();} });
                        oldvSuche = v;
              }
            });

    
}


function sendEcard(toon) {

	if( ecardError ) {
		document.getElementById('ecardRecImg').src = 'http://static.nichtlustig.de/sys_img/'+language+'/toon/ecard_empf.gif';
		document.getElementById('ecardSenderImg').src = 'http://static.nichtlustig.de/sys_img/'+language+'/toon/ecard_abs.gif';
		ecardError = false;
	}

	if( document.getElementById('ecardRec').value.length == 0 ) {
		document.getElementById('ecardRecImg').src = 'http://static.nichtlustig.de/sys_img/'+language+'/toon/ecard_empf_vergessn.gif';
		ecardError=true;
	}
	if( document.getElementById('ecardSender').value.length == 0 ) {
		document.getElementById('ecardSenderImg').src = 'http://static.nichtlustig.de/sys_img/'+language+'/toon/ecard_abs_vergessn.gif';
		ecardError=true;
	}
    
	if( !ecardError ) {


        new Effect.Opacity('ecardform', { from: 1.0, to: 0.2, duration: 0.1, queue: 'end' });

        var params = $H({ 
            NLaction: 'sendEcard',
            toon:     toon, 
            sender:   document.getElementById('ecardSender').value,
            rec:      document.getElementById('ecardRec').value,
            msg:      document.getElementById('ecardMsg').value
        });

        new Ajax.Request('../../ajax/ecard.php', {
            parameters: params.toQueryString(),
            evalScripts: true,
            asynchronous: true,
            onSuccess: function(response) {

                var json_data = response.responseText;
                eval("var status = (" + json_data + ")");
                
                document.getElementById('ecardSender').value = "";
                document.getElementById('ecardRec').value = "";
                document.getElementById('ecardMsg').value = "";
                document.getElementById('ecardmidlayer').style.display = "block";
                document.getElementById('ecardmidlayer2').style.display = "block";
                setTimeout("document.getElementById('ecardmidlayer').style.display = 'none';",6000);
                setTimeout("document.getElementById('ecardmidlayer2').style.display = 'none';",6000);

                new Effect.Opacity('ecardform', { from: 0.2, to: 1.0, duration: 0.1, queue: 'end' });
                
            }
        });

	}
}

function pb2off() {     
  document.getElementById('pb').style.visibility = 'hidden';  
}

function pfeilauf(name) {
	document.getElementById(name).src = "../sys_img/toon/"+name+"_mouseover.gif";
}
  
function pfeilzu(name) {
	document.getElementById(name).src = "../sys_img/toon/"+name+"_normal.gif";
}

// HERE STARTS THE ADDED PART!!!
var pos=0;
function scrollVertical(value, element) {
	element.scrollTop = Math.round(pos+value);
	if( pos+value < 0 ) {
		pos = 0;
	} else if( pos > element.scrollHeight - element.offsetHeight ) {
		pos = element.scrollHeight-element.offsetHeight;
	} else {
		pos = pos+value;
	}
}

var PBshown = false;
function showPB(cartoon,datum,bgcolor,step,zoom) {
	if (step == 3 && PBshown ) {
        new Ajax.Updater('pb', '../ajax/postcard.php', {parameters:
            'updatePost=1'+
			'&step='+step+
			'&datum='+datum+
			'&cartoon='+cartoon+
			'&bgcolor='+bgcolor+
			'&name='+htmlspecial(document.getElementById('name').value)+
			'&adr='+htmlspecial(document.getElementById('adr').value)+
			'&adr2='+htmlspecial(document.getElementById('adr2').value)+
			'&plz='+htmlspecial(document.getElementById('plz').value)+
			'&ort='+htmlspecial(document.getElementById('ort').value)+
			'&country_id='+document.getElementById('country').selectedIndex+
			'&entry='+htmlspecial(document.getElementById('pb2_text').value)
		,evalScripts:false,asynchronous:true});
	} else {
		new Ajax.Updater('pb', '../ajax/postcard.php', {parameters:
			'step='+step+
			'&datum='+datum+
			'&cartoon='+cartoon+
			'&bgcolor='+bgcolor
			,evalScripts:true,asynchronous:true});
	}
	if (step == 1 && zoom != 0) {
		slideLayer('pb');
	}
	PBshown = true;
	
}

function htmlspecial(str) {
	str = str.replace(/\?/g,"||fragezeichen||");
	str = str.replace(/\&/g,"||und||");
	str = str.replace(/#/g,"||raute||");
    str = str.replace(/„/g, "||bdquo||");
    str = str.replace(/“/g, "||ldquo||");
    str = str.replace(/”/g, "||ldquo||");
	return(str);
}

function switchCountry(value) {
	document.getElementById('country').value = value;
	document.getElementById('countryDropdown').style.display='none';
}

function hidePB() {
	PBshown = false;
	slideLayer('pb');
}

function clearPBData() {
    var params = $H({
        step: 'clear'
    });
    var ajax = new Ajax.Request("../ajax/postcard.php", { 
        method:'post', 
        parameters: params.toQueryString()
    });      
}

function Zaehlen () {
	var laenge = textfeld.value.length;
	var zeilen = textfeld.value.split('\n');
	for( var i=0 ; i<zeilen.length-1 ; i++ ) {
		laenge += 32-((zeilen[i].length%32)+1);
	}
	
    var uebrig = maxZeichen - laenge;

    if( uebrig < 0 ) {
		var inhalt = textfeld.value;
        var neuerInhalt = inhalt.substr(0, inhalt.length-1);
        textfeld.value = neuerInhalt;
        uebrig = 0;
    }
    
	document.getElementById('uebrig').innerHTML = uebrig;
}


function fbs_click(location) {
    u=location.href;
    t=document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(location)+'&t='+encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436');
    return false;
}

function isdefined(variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

layer = new Array();
layerPos = new Array();
var layerOn = '';

function slideLayer(id) {

    var height = 0;

    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        height = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        height = document.documentElement.clientHeight;
    } else if( document.body && document.body.clientHeight ) {
        //IE 4 compatible
        height = document.body.clientHeight;
    }


    var y = 0;

    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        y = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        y = document.body.scrollTop;
    } else if( document.documentElement && document.documentElement.scrollTop ) {
        //IE6 standards compliant mode
        y = document.documentElement.scrollTop;
    }

    var layerHeight = $(id).style.height.substr(0,3);
    var pos = 1000;
    var scroll = y+pos+((height-layerHeight)/2);

    if( layer[id] != 'an' ) {
        if( layerOn != '' ) {
            new Effect.MoveY(layerOn, { y: -layerPos[layerOn], mode: 'relative' , duration: 0.5 });
            layer[layerOn] = 'aus';
            layerOn = '';
        }
        layerPos[id] = scroll;
        new Effect.MoveY(id, { y: scroll, mode: 'relative' , duration: 0.5 });
        layer[id] = 'an';
        layerOn = id;
    } else {
        new Effect.MoveY(id, { y: -layerPos[id], mode: 'relative' , duration: 0.5 });
        layer[id] = 'aus';
        layerOn = '';
    }

}


function countClickAndView(type, cartoon, sn) {

    if( type == 'view' ) {

        var incoming = '';
        if( window.location.hash != '' ) {
            incoming = window.location.hash.substr(1);
        } else if( window.location.href.search(/\?/) != -1 ) {
            incoming = window.location.href.substr(window.location.href.search(/\?/)+1);
        }
        
        if( incoming != '' ) {
            var params = $H({
                NLaction: 'countViewCartoon',
                cartoon: cartoon,
                countIncoming: incoming
            });
        } else {
            var params = $H({
                NLaction: 'countViewCartoon',
                cartoon: cartoon
            });
        }

        var ajax = new Ajax.Request("../ajax/countViews.php", { 
            method:'post', 
            parameters: params.toQueryString()
        });      
        
    } else if ( type == 'sn' ) {
        
        var params = $H({
            NLaction: 'countClickSN',
            cartoon: cartoon,
            sn: sn
        });

        var ajax = new Ajax.Request("../ajax/countClick.php", { 
            method:'post', 
            parameters: params.toQueryString() 
        });      

    } else if ( type == 'ecard' ) {

        var params = $H({
            NLaction: 'countEcardSend',
            cartoon: cartoon
        });

        var ajax = new Ajax.Request("../ajax/countClick.php", { 
            method:'post', 
            parameters: params.toQueryString() 
        });      

    } else if ( type == 'cartoon' ) {

        var params = $H({
            NLaction: 'countClickCartoon',
            cartoon: cartoon
        });

        var ajax = new Ajax.Request("../ajax/countClick.php", { 
            method:'post', 
            parameters: params.toQueryString() 
        });      

    }
    
}

