/**
 * Domready jQuery Event
 */
$(document).ready(function() {
    // Add Google Analytics page tracker for external links
    $('a.url').click(function(){
        _gaq.push(['_trackEvent', 'External Website', this, document.title]);
    });

    // Add Google Analytics page tracker for external links
    $('a.file').click(function(){
        _gaq.push(['_trackEvent', 'Download', this, document.title]);
    });
});

$(document).ready(function(){
    // start colorbox like mediabox
    $("a[rel^='lightbox']").each(function(){
        // match all rel attributes to match array
        var regex = /\[([^\s\]]*)(?:\s([^\s\]]*)(?:\s([^\s\]]*))?)?/;
        var match = regex.exec($(this).attr('rel'));
        
        // if there ist only one attribute start the colorbox normal
        if(match[2]==null || match[2]=="") {
            $(this).colorbox({
                maxHeight:"90%", 
                maxWidth:"90%", 
                opacity:0.2,
                current:"",
                scrolling: false
            });
            return;
        }
        
        // if there are two attributes start the colorbox with height and width
        if(match[3]==null || match[3]=="") {
            $(this).colorbox({
                innerWidth:match[1], 
                innerHeight:match[2], 
                opacity:0.2,
                current:"",
                scrolling: false
            });
            return;
        }
        
        // if there are three attributes start the colorbox with height and width and as a iframe
        $(this).colorbox({
            innerWidth:match[2], 
            innerHeight:match[3], 
            iframe:true, 
            opacity:0.2,
            current:"",
            scrolling: false
        });
    }).bind('cbox_complete', function() {
        var cboxTitleHeight = $('#colorbox').find('#cboxTitle').first().height();
        if(cboxTitleHeight > 18) {
            $.colorbox.resize({'height':$('#colorbox').outerHeight() + cboxTitleHeight - 18});
        }
    });
});

/**
 * EXT:comments functions
 */

function tx_comments_pi1_readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1, c.length);
        }
        if (c.indexOf(nameEQ) == 0) {
            return unescape(c.substring(nameEQ.length,c.length)).replace(/\+/, ' ');
        }
    }
    return false;
}

function tx_comments_pi1_setUserDataField(name) {
    var field = document.getElementById('tx_comments_pi1_' + name);
    try {
        if (field && field.value == '') {
            var value = tx_comments_pi1_readCookie('tx_comments_pi1_' + name);
            if (typeof value == 'string') {
                field.value = value;
            }
        }
    }
    catch (e) {}
}

function tx_comments_pi1_setUserData() {
    tx_comments_pi1_setUserDataField('firstname');
    tx_comments_pi1_setUserDataField('lastname');
    tx_comments_pi1_setUserDataField('location');
    tx_comments_pi1_setUserDataField('email');
    tx_comments_pi1_setUserDataField('homepage');
}

