// Define imgwidth, txtheight and slidecount globaly
var imgwidth        = 628;
var txtheight       = 100;
var slidecount      = 0; // this one shouldn't change

if ( jQuery.browser.msie == true && jQuery.browser.version == "6.0") {
    txtheight = txtheight+6;
}

var feed = [
    "http://maps.google.ch/maps/ms?oe=UTF8&hl=de&ie=UTF8&msa=0&msid=111397535434809996605.0004619ed4e7453b3a6d1&output=kml", // Europe
    "http://maps.google.ch/maps/ms?oe=UTF8&hl=de&ie=UTF8&msa=0&msid=111397535434809996605.0004619f074084db3c2fe&output=kml", // America
    "http://maps.google.ch/maps/ms?oe=UTF8&hl=de&ie=UTF8&msa=0&msid=111397535434809996605.00046213ae8d16fb53daf&output=kml", // Near East
    "http://maps.google.ch/maps/ms?oe=UTF8&hl=de&ie=UTF8&msa=0&msid=111397535434809996605.0004619f04a917d97d543&output=kml", // Oceania
    "http://maps.google.ch/maps/ms?oe=UTF8&hl=de&ie=UTF8&msa=0&msid=111397535434809996605.0004619efed55167a1f99&output=kml", // Asia
    "http://maps.google.ch/maps/ms?oe=UTF8&hl=de&ie=UTF8&msa=0&msid=111397535434809996605.0004619f019de55b36333&output=kml" // Africa
];

var proxyhost = "/themes/buhlmann/js/proxy.php?url=";

jQuery(document).ready(function(){
    // Make them shown (visibility initially set to hidden because of noscript case) | SW
    if ( jQuery( '#pointer' ) ) jQuery( '#pointer' ).css(
        { 'visibility' : 'visible' }
    );
    if ( jQuery( '#controls' ) ) jQuery( '#controls' ).css(
        { 'visibility' : 'visible' }
    );
    
    var imgouter        = '#img_slide_outer';
    var imgpre          = "img_";
    var imgcount        = countEl( '#' + imgouter + ' img' );

    var textouter       = '#text_slide_outer';
    var textpre         = 'teaser_';
    var textcount       = countEl( '.tease' );

    if ( jQuery( '#actual' ).length > 0 ){
        jQuery( '#actual' ).text( "1" );
    }
    if ( jQuery( '#all' ).length > 0 ) {
        jQuery( '#all' ).text( countEl( '.tease' ) );
    } 

    if ( jQuery( '#next' ) ) {
        jQuery( '#next' ).bind('click', function(e) {
            // Setting up the "next" (down) event
            e.preventDefault();
            slideIt( 'down' );
        });
    }
    if ( jQuery( '#prev' ) ) {
        jQuery( '#prev' ).bind( 'click', function(e) {
            // Setting up the "prev" (up) event
            e.preventDefault();
            slideIt( 'up' );
        });
    }

    /* Add "padding"-Class to content-images ( check align and stuff ), required for use with ie6 */
    if ( jQuery( "#linkbox ul li p" ).length > 0 ) {
        jQuery( "#linkbox ul li p" ).each(function(i, element) {
            $(element).hover(
                function() {
                    $( element ).css({'text-decoration' : 'none', 'cursor' : 'pointer' });
                },
                function() {
                    $( element ).css({'text-decoration' : 'underline', 'cursor' : 'pointer' });
                });
            
        });
    }
    
    if ( jQuery( "#fck p img" ).length > 0 ) {
        jQuery( "#fck p img" ).each(function(i, element) {
            if ( $(element).attr( 'align' ) == 'left' ) {
                $(element).attr( 'class', 'leftimg' );
            }
            if ( $(element).attr('align') == 'right' ) {
                $(element).attr( 'class', 'rightimg' );
            }
        });
    }
    
    if ( jQuery('#map').length > 0 ) {
        // Do this, if a id="map" container exists.
        
        jQuery( "#feed" ).append('<div id="anchornav"><\/div><br class="clear" \/>');

        createList(0);
        initMap();

        jQuery('.setCenter').click(function(){ });
    }
});
function getActual( outer ) {
    // gets the actual position
    return parseInt(jQuery( outer ).attr( 'title' ).split( 'a_' )[1]);
}

function setActual( actual ) {
    // sets the actual position
    jQuery( '#actual' ).text( actual );
}
function countEl( els ) {
    // counts element's by it's (css-)selector
    return jQuery( els ).length;
}
function slideIt( direction )
{
    if ( direction == "up") {
        // set-up the state
        if (slidecount <= 0) {
            slidecount = $('#wide_inner img').length;
        }
        slidecount--;
    } else {
        if (slidecount >= $('#wide_inner img').length-1) {
            slidecount = -1;
        }
        slidecount++;
    }

    slideimg = (slidecount) * imgwidth * -1;
    slidetxt = (slidecount) * txtheight * -1;

    setActual(slidecount+1); // Sets the actual pointer while animation starts

    jQuery('#wide_inner').animate({
        'left' : slideimg + 'px'
    }, 800, 'easeinout' );

    jQuery('#text_inner').animate({
        'top' : slidetxt + 'px'
    }, 800, 'easeinout' );
}

/* Map Functions (Distributor) */
function initMap() {
    jQuery('#map').jmap('init', {'mapType':'map','mapCenter':[46.881404,8.318302], 'mapZoom':1, 'mapShowjMapsIcon':false});

    jQuery(feed).each( function(i, element) {
        jQuery('#map').jmap('AddFeed', { 'feedUrl': element })
    });
}
function stripTags(str) {
    return str.replace(/<\/?[^>]+>/gi, " ").replace(/^\s+/, "").replace(/\s+$/, '');
}
function createList(i) {

    var date = new Date();
     var fd = feed[i];
     var tmp = fd.substr(fd.indexOf("msid")+5);
     var spl = tmp.split("&");
     var mid = spl[0];

     var url = proxyhost + escape(fd);
     // var url = proxyhost + escape(fd) + "&amp;date=" + date.getTime();
     // var url = proxyhost + fd;
    

    jQuery.get(url, function(xml) {
        var json = jQuery.xml2json(xml);
        var delimit = "";
        var idname = json.Document.name.split(" ").join("-");
        
        if ( i < (feed.length-1) ) {
            delimit = "&nbsp;|&nbsp;";
        }
        
        jQuery( "#anchornav" ).append( '<a href="#' + idname + '">' + json.Document.name + "</a>" + delimit  );
        jQuery( "#feed" ).append( "<div class='box' id='" + idname + "'></div> " );
        jQuery( "#" + idname ).append( "<h3><span>" + json.Document.name + "<\/span><a class='toplink' href='#wrap'>top<\/a><br class='fullclear'\/><\/h3>" );
        jQuery( "#" + idname ).append( "<div class='padding' id='" + idname + "_inner'></div>" );
        jQuery( "#" + idname + "_inner" ).append("<h4>"+ json.Document.description +"<\/h4>");

        jQuery(json.Document.Placemark).each( function ( i, element ) {
            var c = 'right';
            if ( i%2 == 0 ) c = 'left';

            var coordinates = element.Point.coordinates.split(",");
            var lon = coordinates[0];
            var lat = coordinates[1];

            var name = element.name.replace(/\s/g, "+");
            var desc = stripTags(element.description).replace(/\s/g, "+");
 /*
           jQuery( "#" + idname + "_inner" ).append(
                "<a  href='http://maps.google.com/maps?f=q&source=s_q&hl=de&geocode=&q="+name+"+"+desc+"&sll="+lat+"&ll="+lon+"&mrt=kmlkmz&z=14' target='_blank'><strong>"+element.name+"<\/strong><\/a><br \/>\n"
            );
*/

            jQuery( "#" + idname + "_inner" ).append(
                "<a  href='http://maps.google.ch/maps/ms?oe=UTF8&hl=de&ie=UTF8&msa=0&ll="+lat+","+lon+"&msid="+mid+"' target='_blank'><strong>"+element.name+"<\/strong><\/a><br \/>\n"
            );

            jQuery( "#" + idname + "_inner" ).append( element.description +"<br \/>\n");
        });

        if (i < feed.length -1) {
            i++
            createList(i);
        }
    });
}