/* 
 * scherf.js
 * Description:
 */

/*******************************************************************
****  JS Library
*******************************************************************/

/***  Define custom Flag icons  ***********************************/

    // Create our "tiny" marker icon
    var flagIcon = new GIcon(G_DEFAULT_ICON);
//    flagIcon.image = "http://192.168.30.159/projekte/GG_JAKU/fileadmin/template/img/flag.png";
//    flagIcon.shadow = "http://192.168.30.159/projekte/GG_JAKU/fileadmin/template/img/flag_shadow.png";
//    flagIcon.image = window.location.pathname + "fileadmin/template/img/gmaps_flag.png";
//    flagIcon.shadow = window.location.pathname + "fileadmin/template/img/gmaps_flag_shadow.png";
    flagIcon.image = "fileadmin/template/img/gmaps_flag.png";
    flagIcon.shadow = "fileadmin/template/img/gmaps_flag_shadow.png";
    flagIcon.iconSize = new GSize(71, 57);
    flagIcon.shadowSize = new GSize(71, 57);
    flagIcon.iconAnchor = new GPoint(0, 57);
    flagIcon.infoWindowAnchor = new GPoint(0, 57);

    // Set up our GMarkerOptions object
    markerOptions = {icon:flagIcon};


/***  Render Adresses in gmaps  ***********************************/

    var zoom = 2;
    function showAddress(address) {
        geocoder.getLatLng(
            address,
            function(point) {
                if (!point) {
                    //alert(address + " not found");
                } else {
//                    map.setCenter(point, zoom);
                    map.setCenter(point, 9);
                    // MArker hinzufuegen
                    var marker = new GMarker(point,markerOptions);
                    map.addOverlay(marker);
                    // Zoom Kontrolle
//                    var mapControl = new GSmallZoomControl();
                    var typeControl = new GMapTypeControl();
                    var mapControl = new GLargeMapControl ();
                    map.addControl(typeControl);
                    map.addControl(mapControl);

                    GEvent.addListener(marker, "click", function() {
                        marker.openInfoWindowHtml(address);
                    });
                };
            }
        );
    }



/*******************************************************************
****  jQuery
*******************************************************************/

$(document).ready(function(){

    // Initialize variables

    // margin handling produktweltewn
    $('.produktwelten .item:nth-child(3n)').css('margin-right','0px');


    // Rounded corner elements
    $('#identifier').corner('4px');


    // Accordion
    $(".accordion").accordion({
        header: '.acc-trigger',
        active: 'none'
    });

        // Meta-Nav
    $('#nav_main li').hover(
        function(){
            $(this).find('.level-2').fadeIn();
            $(this).siblings().find('.level-2').fadeOut();
        },
        function(){}
    );

    $('#nav_main .level-2').hover(
        function(){},
        function(){
            $(this).fadeOut();
        }
    ); 
    
    // Meta-Nav
    $('#nav_meta li').hover(
        function(){
            $(this).find('.level-2').fadeIn();
            $(this).siblings().find('.level-2').fadeOut();
        },
        function(){}
    );

    $('#nav_meta .level-2').hover(
        function(){},
        function(){
            $(this).fadeOut();
        }
    );  

    // Galerien
    var countItems = $('.gallery .item').length;
    var setItemWidth = '';

    // How many items to display?
    if ((countItems % 3) == 0){
        setItemWidth = '221px';
        $('.gallery .item:nth-child(3n)').css('margin-right','0px');
    } else if ((countItems % 4) == 0){
        setItemWidth = '157px';
        $('.gallery .item:nth-child(4n)').css('margin-right','0px');
    } else if ((countItems % 2) == 0){
        setItemWidth = '348px';
        $('.gallery .item:nth-child(2n)').css('margin-right','0px');
    } else {
        setItemWidth = '221px';
        $('.gallery .item:nth-child(3n)').css('margin-right','0px');
    }

    $('.gallery .item').each(
        function(){
            var group = $(this).find('.group').html();
            var title = $(this).find('.title').html();
            var target = $(this).find('.target').html();
            var description = $(this).find('.description').html();
            var catInfoLink = '<a class="info" href="'+target+'" rel="'+group+'">&gt; Infos</a>';
            var catLink = '<span class="text" >'+title+'</span>';
            var catImg = '<img src="'+target+'" alt="'+title+'" width="'+setItemWidth+'" />';

            $(this).css('width',setItemWidth);
            $(this).css('overflow','hidden');
            $(this).find('.images a').each(
                function(){
                    $(this).attr('rel',group);
                }
            );
//            $(this).find('.images img').each(
//                function(){
//                    var new_tag = '<a href="'+$(this).attr('src')+'" rel="'+group+'" title="'+$(this).attr('title')+'"><img src="'+$(this).attr('src')+'" alt="'+title+'"/></a>';
//                    $(this).after(new_tag);
//                    $(this).remove();
//                }
//            )
            $(this).prepend(catLink+catImg+catInfoLink);
        }
    );
        
    // Fancybox
    $(".gallery .item a").fancybox({
        'hideOnContentClick'    : true,
        'frameHeight'           : 500,
//        'autoScale'             : true,
        'titlePosition'         : 'inside',
        'padding'               : 16,
        'cyclic'                : true
    });


    // Slideshow;
    $('.scrollable').scrollable({
        size: 1,
        clickable: false,
        loop: true
    }).navigator();

    // "New" Labels"
    $('.neu.aktion .csc-textpic-imagewrap').append('<img class="new-label" src="fileadmin/template/img/neue_aktion_s.png" alt="NEU!" />')
    $('.neu.produkt .csc-textpic-imagewrap').append('<img class="new-label" src="fileadmin/template/img/neue_aktion_s.png" alt="NEU!" />')

    // Clearing
    $('#content').after('<div class="clear"></div>');
});

