function chatAnimation(){
    $('#chatButton .chat_act').animate({
        opacity: 0.25
    }, 1000, function(){
        $('#chatButton a').attr('style', '');
        chatAnimation();
    })
}


function last_check(msg, url){
    var answer = confirm(msg);
    if (answer) 
        document.location = url;
}

function addsmiley(name, value){
    var oldval = $('#' + name).val();
    $('#' + name).val(oldval + value);
}

var pWaChat = null;
var sWaChatSettings = 'width=325,height=614,scrollbars=no,location=no,resizable=no,toolbar=no';

function inviteChat(user){
    $.ajax({
        async: true,
        data: 'action=sysMessages&type=chf&topic=privater+Chat&invite=' + user,
        url: '/chatbackend.php',
        type: 'GET',
        success: function(data){
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            alert('Käse');
        }
    });
}

function openChat(){

    var count = GetCookie('wiealtdepopup');
    
    var status = 0;
    
    if (count) {
        if (count == "0") 
            status = 1;
        if (count == "1") 
            status = 2;
    }
    
    if (status == 2) {
        //alert("popup could be there");
    }
    
    if (status == 0 || status == 1) {
        //alert("popup not present");
        pWaChat = window.open('http://www.wiealt.de/chatnew', 'wiealtde_chat_main', sWaChatSettings);
        if (pWaChat != 'undefined' && pWaChat != null) {
            pWaChat.focus();
        }
    }
}

//*******************************************************
//Cookies
//*****************************************************

var expDays = 1; // number of days the cookie should last
var exp = new Date();
exp.setTime(exp.getTime() + (expDays * 24 * 60 * 60 * 1000));

function GetCookie(name){
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) 
            return getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) 
            break;
    }
    return null;
}

function getCookieVal(offset){
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) 
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

function checkBrowserName(name){  
   var agent = navigator.userAgent.toLowerCase();  
   if (agent.indexOf(name.toLowerCase())>-1) {  
     return true;  
   }  
   return false;  
 }  

$(document).ready(function(){
    $('.teaser_reiter a[href="#"]').css('cursor', 'default');
    $('.draggable_teaser').css('cursor', 'pointer');
    $('.teaser_icons_close').css('cursor', 'pointer');
    chatAnimation();
    $('#chatButton .chat_act').click(function(){
        $('#chatButton .chat_act').attr('style', '');
        $('#chatButton a').removeClass('chat_act');
    });
    
    $('#header_menue a, #header_submenue a.subtext').hover(function(){
        var img, src, src2, pos, imgname, imgname2;
        img = $(this).children('img');
        src = img.attr('src');
        if(src !== undefined){
            pos = src.indexOf('/neu_b');
            imgname = src.substr(pos + 1);
            imgname2 = imgname.replace('.gif','_hov.gif');
            src2 = src.replace(imgname,imgname2);
            img.attr('src',src2);
        }
    },
    function(){
        var img, src, src2, pos, imgname, imgname2;
        img = $(this).children('img');
        src = img.attr('src');
        if(src !== undefined){
            pos = src.indexOf('/neu_b');
            imgname = src.substr(pos + 1);
            imgname2 = imgname.replace('_hov.gif','.gif');
            src2 = src.replace(imgname,imgname2);
            img.attr('src',src2);
        }
    });
    
    $('a.teaser_icons_richtungskreuz').mousedown(function(){
        $(this).parent().parent().removeClass('box_header').addClass('box_header2');
    });
    $('a.teaser_icons_richtungskreuz').mouseup(function(){
        $(this).parent().parent().removeClass('box_header2').addClass('box_header');
    });
    
    $('.teaser').each(function(i){
        if(i == 1 && $(this).attr('id') == 'middle_lastNews'){
        	box = $(this);
        	bheight = box.outerHeight();
        	$('#drag_bg').css('top',bheight + 20 + 'px');
        	if(checkBrowserName('MSIE')){  
        		$('#drag_bg').css('left','-4px');
        	}
        }
    });
    
    $('.checkpics').each(function(i){
        var prev,img,pos;
        
        prev = $(this).prev('.checkpics-div');
        
        img = prev.children().children();
        pos = img.offset();
        $('body').append('<div id="checkpics_' + i + '" style="display:none">' + $(this).html());
        $('#checkpics_' + i).addClass('checkpics-vis').css({
        	position   : 'absolute',
        	top        : pos.top + 'px',
        	left       : (pos.left + 2) + 'px',
        	width      : '14px',
        	height     : '40px',
        	'z-index'  : 2000,
        	display    : 'block'
        });
    });
    
    $('.dev').click(function(){
        if($(this).attr('id') == 'dev_close'){
        	$('#dev_open').show();
        }
        else{
        	$('#dev_open').hide();
        }
        $('#dev-toolbar').slideToggle();
    });
});

