var cookieDomain = 'www.iskenderpasa.com';

function addLoadEvent(func) { // Source: http://simon.incutio.com/archive/2004/05/26/addLoadEvent
    var oldonload = window.onload;
    if (typeof window.onload != 'function') window.onload = func;
    else {
        window.onload = function() {
            if (oldonload) oldonload();
            func();
        }
    }
}

addLoadEvent(OzellikleriAc);
//addLoadEvent(etiketlerimiOlustur);
addLoadEvent(hizlierisimOlustur);

function runLoadEvent() {
    window.onload();
    window.onload = function() { };
}

function CookieAyarla(ad, deger, gecerli, yol, alan, guvenli) {
    document.cookie = ad + "=" + escape(deger) +
        ((gecerli) ? "; expires=" + gecerli.toGMTString() : "") +
        ((yol) ? "; path=" + yol : "") +
        ((alan) ? "; domain=" + alan : "") +
        ((guvenli) ? "; secure" : "");
}

function CookieAl(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
    if (CookieAl(name)) document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function OzellikleriAc() {
    var x;
    x = document.getElementById('hizlierisim');
    if (x) {
        if (!document.getElementById('etiketlerim')) {
            var wDiv = document.createElement('DIV');
            wDiv.className = 'section';
            wDiv.innerHTML = '<h3>Etiketlerim</h3><div id="etiketlerim"></div>';
            x.parentNode.insertBefore(wDiv, x.nextSibling);
        }
    }
}

var hizliErisimCookie = 'EtiketlerimListe';

function etiketlerimeEkle(title) {
    lbGizleYardimci();
    if (etiketVarMi(title)) return false;
    var exp = new Date().getTime();
    exp += 1000 * 60 * 60 * 24 * 31 * 3; // 3 ay sureyle gecerli
    exp = new Date(exp);
    var cookieData = CookieAl(hizliErisimCookie);
    CookieAyarla(hizliErisimCookie, (cookieData == null || cookieData.trim() == '' ? '' : cookieData + ';') + title, exp, '/', cookieDomain);
    if (!etiketVarMi(title)) {
        var cookieData = CookieAl(hizliErisimCookie);
        if (cookieData == null || cookieData == '') alert('Üzgünüz, etiket eklenemedi.\n\nLütfen tarayıcınızda çerezlere izin verildiğinden emin olun.');
        else alert('Üzgünüz, etiket eklenemedi.\n\nEtiketlerim listenizde çok fazla etiket bulunuyor olabilir.');
    }
    else etiketlerimiOlustur(title);
    return false;
}

function etiketSil(title) {
    lbGizleYardimci();
    var cookieData = CookieAl(hizliErisimCookie);
    if (cookieData == null) {
        return false;
    }
    var removed = false;
    var etiketListe = cookieData.split(';');
    for (var i = 0; i < etiketListe.length; i++) {
        if (title == etiketListe[i]) {
            etiketListe.splice(i, 1); // arrayden bu itemi kaldir
            removed = true;
        }
    }
    cookieData = etiketListe.join(';');
    if (cookieData == '') cookieData = ' ';
    var exp = new Date().getTime();
    exp += 1000 * 60 * 60 * 24 * 31 * 3; // 3 ay sureyle gecerli
    exp = new Date(exp);
    CookieAyarla(hizliErisimCookie, cookieData, exp, '/', cookieDomain);
    if (!removed) alert('Üzgünüz, etiket silinemedi.');
    else etiketlerimiOlustur();
    return false;
}

function etiketVarMi(title) {
    var cookieData = CookieAl(hizliErisimCookie);
    if (cookieData == null || cookieData == '') return false;
    var etiketListe = cookieData.split(';');
    for (var i = 0; i < etiketListe.length; i++) {
        if (title == etiketListe[i]) return true;
    }
    return false;
}

function etiketlerimiOlustur(newTitle) {
    var w;
    var varsayilanEtiketler = 'iskender paşa';
    if (w = document.getElementById('etiketlerim')) {
        w.innerHTML = '';
        var cookieData = CookieAl(hizliErisimCookie);
        if (cookieData == null) {
            cookieData = varsayilanEtiketler;
            var exp = new Date().getTime();
            exp += 1000 * 60 * 60 * 24 * 31 * 3; // 3 ay sureyle gecerli
            exp = new Date(exp);
            CookieAyarla(hizliErisimCookie, cookieData, exp, '/', cookieDomain);
        }
        w.parentNode.style.display = 'block'; // divi goster
        var html = '';
        if (cookieData.trim() != '') {
            var etiketListe = cookieData.trim().split(';');
            for (var i = 0; i < etiketListe.length; i++) {
                var highlight = false;
                var fadefrom = new Array(255, 255, 51); // rgb rengi
                if (newTitle) if (newTitle == etiketListe[i]) {
                    highlight = true;
                    for (var j = 1; j <= 10; j++) {
                        setTimeout('if (wn = document.getElementById("myt' + i + '")) wn.style.backgroundColor="rgb(' + Math.round(fadefrom[0] + (j / 10) * (255 - fadefrom[0])) + ',' + Math.round(fadefrom[1] + (j / 10) * (255 - fadefrom[1])) + ',' + Math.round(fadefrom[2] + (j / 10) * (255 - fadefrom[2])) + ')";', ((j / 10) * 1000) + 500);
                    }
                }
                html += '<li><a href="etiketler.aspx?tag=' + etiketListe[i] + '" id="myt' + i + '"' + (highlight ? ' style="background-color:rgb(' + fadefrom[0] + ',' + fadefrom[1] + ',' + fadefrom[2] + ');"' : '') + '>' + etiketListe[i].replace(/-/g, '-<wbr></wbr>') + '</a></li>';
            }
            if (html != '') html = '<ul><li>Etiketlerim</li><li><ul>' + html + '</ul></li></ul>';
        }
        if (html == '' || cookieData == varsayilanEtiketler) html += '<p>Takip etmek istediğiniz etiketleri buraya ekleyebilirsiniz.</p>';
        //html += '<p id="tl"><a href="etiketler.aspx" class="more">Etiketleri Listele</a> | <a href="etiketbulutu.aspx" class="more">Etiket Görseli</a></p>';
        w.innerHTML = html;
        etiketleriAyarla();
    }
    linkleriIsle();
}

function etiketleriAyarla() {
    if (document.URL.match(/\/tag\/[^\.]+\//)) { 
        tagname = document.URL;
        tagname = tagname.replace(/^.*\/tag\//, '');
        tagname = tagname.replace(/\/.*$/, '');
        var wla;
        var tl;
        if (wla = document.getElementById('etiketlerimeEkle')) {
            if (etiketVarMi(tagname)) {
                wla.onclick = function() { return etiketSil(tagname) };
                wla.innerHTML = 'Etiketlerimden Çıkar';
            }
            else {
                wla.onclick = function() { return etiketlerimeEkle(tagname) };
                wla.innerHTML = 'EtiketlerimeEkle';
            }
        }
        if (tl = document.getElementById('tl')) {
            var tp;
            if (tp = document.getElementById('tp')) tp.innerHTML = '';
            else {
                tp = document.createElement('P');
                tl.parentNode.insertBefore(tp, tl);
            }
            if (etiketVarMi(tagname)) tp.innerHTML = '<a href="javascript:void(0);" onclick="etiketSil(tagname);">Sil: \'' + tagname + '\'</a>';
            else tp.innerHTML = '<a href="javascript:void(0);" onclick="etiketlerimeEkle(tagname);">Ekle: \'' + tagname + '\'</a>';
        }
    }
}

var hizlierisimCookie = 'HizliErisimListe';

function hizlierisimeEkle(url, title) {
    lbGizleYardimci();
    url = url.replace(/^[a-z]+:\/\/[^\/]+/, ''); // sadece relative pathi al
    title = title.replace(/(<[^>]+>)/ig, '').trim(); // 

    user = document.getElementById('txtUser');
    if (user == null || user.innerHTML == null || user.innerHTML == '' || user.innerHTML == '0') {
        alert('Bu özelliği kullanabilmek için üye girişi yapmanız gerekiyor!');
        return false;
    }
    
    if (hizlierisimdeVarmi(url)) return false;
    var exp = new Date().getTime();
    exp += 1000 * 60 * 60 * 24 * 31 * 3; // 3 ay sureyle gecerli
    exp = new Date(exp);
    var cookieData = CookieAl(hizlierisimCookie);
    CookieAyarla(hizlierisimCookie, (cookieData == null || cookieData.trim() == '' ? '' : cookieData + ';') + url + '~' + title, exp, '/', cookieDomain);
    if (!hizlierisimdeVarmi(url)) {
        var cookieData = CookieAl(hizlierisimCookie);
        if (cookieData == null || cookieData == '') alert('Üzgünüz, sayfa hızlı erişim listenize eklenemedi.\n\nTarayıcınızın çerezleri kabul ettiğinden emin olun.');
        else alert('Üzgünüz, sayfa hızlı erişim listenize eklenemedi.\n\nListeniz dolu olabilir.');
    }
    else hizlierisimOlustur(url);
    
    return false;
}

function hizlierisimdenCikar(url) {
    lbGizleYardimci();
    var cookieData = CookieAl(hizlierisimCookie);
    if (cookieData == null) {
        return false;
    }
    var removed = false;
    var hizlierisimdekiler = cookieData.split(';');
    for (var i = 0; i < hizlierisimdekiler.length; i++) {
        if (url.indexOf(hizlierisimdekiler[i].split('~')[0]) != -1) {
            hizlierisimdekiler.splice(i, 1); // arrayden cikar
            removed = true;
        }
    }
    cookieData = hizlierisimdekiler.join(';');
    if (cookieData == '') cookieData = ' ';
    var exp = new Date().getTime();
    exp += 1000 * 60 * 60 * 24 * 31 * 3; // 3 ay sureyle gecerli
    exp = new Date(exp);
    CookieAyarla(hizlierisimCookie, cookieData, exp, '/', cookieDomain);
    if (!removed) alert('Üzgünüz, listeden silinemedi.');
    else hizlierisimOlustur();
    
    return false;
}

function hizlierisimdeVarmi(url) {
    var cookieData = CookieAl(hizlierisimCookie);
    if (cookieData == null || cookieData == '') return false;
    var hizlierisimdekiler = cookieData.split(';');
    for (var i = 0; i < hizlierisimdekiler.length; i++) {
        if (url.indexOf(hizlierisimdekiler[i].split('~')[0]) != -1) return true;
    }
    return false;
}

function hizlierisimOlustur(newURL) {
    var c;
    if (c = document.getElementById('hizlierisim')) {
        c.innerHTML = '';
        var cookieData = CookieAl(hizlierisimCookie);
        if (cookieData == null || (typeof cookieData == 'string' && cookieData.trim() == '')) c.innerHTML = '<ul><li>Hızlı Erişim Listesi</li><li>Sayfaları daha sonra ulaşmak için hızlı erişim listenize ekleyebilirsiniz.</li></ul>';
        else {
            var hizlierisimdekiler = cookieData.split(';');
            var html = '';
            for (var i = 0; i < hizlierisimdekiler.length; i++) {
                var x = hizlierisimdekiler[i].split('~');
                var highlight = false;
                var fadefrom = new Array(255, 255, 51); // rgb colour
                if (newURL) if (newURL.indexOf(x[0]) != -1) {
                    highlight = true;
                    for (var j = 1; j <= 10; j++) {
                        setTimeout('if (cn = document.getElementById("myc' + i + '")) cn.style.backgroundColor="rgb(' + Math.round(fadefrom[0] + (j / 10) * (255 - fadefrom[0])) + ',' + Math.round(fadefrom[1] + (j / 10) * (255 - fadefrom[1])) + ',' + Math.round(fadefrom[2] + (j / 10) * (255 - fadefrom[2])) + ')";', ((j / 10) * 1000) + 500);
                    }
                }
                html += '<li><a class="linkekle" href="' + x[0] + '" id="myc' + i + '"' + (highlight ? ' style="background-color:rgb(' + fadefrom[0] + ',' + fadefrom[1] + ',' + fadefrom[2] + ');"' : '') + '>' + x[1] + '</a></li>';
            }
            c.innerHTML = '<ul><li>Hızlı Erişim Listesi</li><li><ul>' + html + '</ul></li></ul>';
        }
    }
    linkleriIsle();
}

function lbGizle() {
    lbTimeout = window.setTimeout('lbGizleYardimci();', 100);
}

function lbGizleYardimci() {
    var lb;
    if (lb = document.getElementById('linkButton')) lb.parentNode.removeChild(lb);
}

function lbGizleIptal() {
    if (window.lbTimeout) window.clearTimeout(lbTimeout);
}

function linkleriIsle() {
    var links = document.getElementsByTagName('a');
    for (var i = 0; i < links.length; i++) {       
       if ((links[i].className).trim() == 'linkekle') hizlierisimLinki(links[i]);
   }
   var links2 = document.getElementsByTagName('span');
   for (var xi = 0; xi < links2.length; xi++) {
       if ((links2[xi].className).trim() == 'yazibaslik') baslikLink(links2[xi]);
   }
   var links3 = document.getElementsByTagName('a');
   for (var xxi = 0; xxi < links3.length; xxi++) {
       if ((links3[xxi].className).trim() == 'etiketekle') etiketLinki(links3[xxi]);
   }

}

function hizlierisimLinki(link) {
    link.onmouseout = function(e) {
        if (typeof lbGizle == 'function') lbGizle();
    };
    link.onmouseover = function(e) {
        if (typeof lbGizleIptal == 'function') lbGizleIptal();
        if (typeof lbGizleYardimci == 'function') lbGizleYardimci();
        var side = 'right';
        side = 'right';
        var lb = document.createElement('A');
        lb.id = 'linkButton';
        lb.appendChild(document.createTextNode(' '));
        lb.onmouseover = function(f) {
            if (typeof lbGizleIptal == 'function') lbGizleIptal();
        };
        lb.onmouseout = function(f) {
            if (typeof lbGizle == 'function') lbGizle();
        };
        if (typeof hizlierisimdeVarmi == 'function' && hizlierisimdeVarmi(this.href)) {
            //lb.className = 'lb_' + side + '_remove_blue';
            lb.title = 'Hızlı Erişim Listesinden Çıkar';
            lb.alt = lb.title;
            lb.innerHTML = '(-)';
            lb.href = 'javascript:void(hizlierisimdenCikar("' + this.href.replace(/"/g, '\"') + '"));';
        }
        else {
            //lb.className = 'lb_' + side + '_add_blue';
            lb.title = 'Hızlı Erişim Listesine Ekle';
            lb.alt = lb.title;
            lb.innerHTML = '(+)';
            lb.href = 'javascript:void(hizlierisimeEkle("' + this.href.replace(/"/g, '\"') + '","' + escape(this.innerHTML.replace(/(<[^>]+>)/ig, '').trim().replace(/"/g, '\"')) + '"));';
        }
        lb.onclick = function(f) {
            eval(unescape(this.href).replace(/^javascript:/, ''));
            return false;
        };
        this.parentNode.insertBefore(lb, (side == 'left' ? this : this.nextSibling));
    }
}

function baslikLink(link) {
    link.onmouseout = function(e) {
        if (typeof lbGizle == 'function') lbGizle();
    };
    link.onmouseover = function(e) {
        if (typeof lbGizleIptal == 'function') lbGizleIptal();
        if (typeof lbGizleYardimci == 'function') lbGizleYardimci();
        var side = 'right';
       
        var lb = document.createElement('A');
        lb.id = 'linkButton';
        lb.appendChild(document.createTextNode(' '));
        lb.onmouseover = function(f) {
            if (typeof lbGizleIptal == 'function') lbGizleIptal();
        };
        lb.onmouseout = function(f) {
            if (typeof lbGizle == 'function') lbGizle();
        };
        if (typeof hizlierisimdeVarmi == 'function' && hizlierisimdeVarmi(document.location.href)) {
            lb.title = 'Hızlı Erişim Listesinden Çıkar';
            lb.alt = lb.title;
            lb.innerHTML = '(-)';
            lb.href = 'javascript:void(hizlierisimdenCikar("' + document.location.href.replace(/"/g, '\"') + '"));';
        }
        else {
            lb.title = 'Hızlı Erişim Listesine Ekle';
            lb.alt = lb.title;
            lb.innerHTML = '(+)';
            lb.href = 'javascript:void(hizlierisimeEkle("' + document.location.href.replace(/"/g, '\"') + '","' + (escape(this.innerHTML).replace(/(<[^>]+>)/ig, '').trim().replace(/"/g, '\"')) + '"));';
        }
        lb.onclick = function(f) {
        eval(unescape(this.href).replace(/^javascript:/, ''));
            return false;
        };

        this.parentNode.insertBefore(lb, (side == 'left' ? this : this.nextSibling));
    }
}

function etiketLinki(link) {
    link.onmouseout = function(e) {
        if (typeof lbGizle == 'function') lbGizle();
    };
    link.onmouseover = function(e) {
        if (typeof lbGizleIptal == 'function') lbGizleIptal();
        if (typeof lbGizleYardimci == 'function') lbGizleYardimci();
        var side = 'right';
        if (link.parentNode.nodeName.match(/^(li|h[1-6])$/i)) {
            var parentText = link.parentNode.innerHTML.replace(/<[^>]+>/g, '').trim();
            var linkText = link.innerHTML.replace(/<[^>]+>/g, '').trim();
            if (parentText.indexOf(linkText) == 0) side = 'left';
        }
        if (link.parentNode.scrollWidth == link.scrollWidth) {
            side = 'left';
        }
        var tagname = this.href;
        tagname = tagname.replace(/^.*\/tag\//, '');
        tagname = tagname.replace(/\/.*$/, '');
        var lb = document.createElement('A');
        lb.id = 'linkButton';
        lb.appendChild(document.createTextNode(' '));
        lb.onmouseover = function(f) {
            if (typeof lbGizleIptal == 'function') lbGizleIptal();
        };
        lb.onmouseout = function(f) {
            if (typeof lbGizle == 'function') lbGizle();
        };
        
        if (typeof etiketVarMi == 'function' && etiketVarMi(tagname)) {
            lb.title = 'Etiket Listesinden Çıkar';
            lb.alt = lb.title;
            lb.innerHTML = '(-)';
            lb.href = 'javascript:void(etiketSil("' + this.href.replace(/"/g, '\"') + '"));';
        }
        else {
            lb.title = 'Etiket Listesine Ekle';
            lb.alt = lb.title;
            lb.innerHTML = '(+)';
            lb.href = 'javascript:void(etiketlerimeEkle("' + this.href.replace(/"/g, '\"') + '","' + escape(this.innerHTML.replace(/(<[^>]+>)/ig, '').trim().replace(/"/g, '\"')) + '"));';

        }
        lb.onclick = function(f) {
            eval(unescape(this.href).replace(/^javascript:/, ''));
            return false;
        };
        this.parentNode.insertBefore(lb, (side == 'left' ? this : this.nextSibling));
    }
}

//LOGIN FORM
var LOGIN_FORM_VISIBLE = false;

function login_form_style() {
    if (LOGIN_FORM_VISIBLE) {
        $("#loginkutusu").animate({ top: -80 }, 400);
        LOGIN_FORM_VISIBLE = false;
        return;
    }

    if (!LOGIN_FORM_VISIBLE) {
        $("#loginkutusu").animate({ top: 0 }, 400, function() { setTimeout(checkActivity, 8000) });
        LOGIN_FORM_VISIBLE = true;
        return;
    }
}

function checkActivity() {
    var login = document.getElementById('username').value;
    var pass = document.getElementById('password').value
    if ((login == "") && (pass == "")) {
        login_form_style();
    }
}

function checkLoginForm(error) {
    var login = document.getElementById('username').value;
    var pass = document.getElementById('password').value
    if ((login == "") && (pass == "")) {
        alert(error);
        checkActivity();
        return false;
    }
    return true;
}


