$(document).ready( function(){
    
    // handle category colors
    $("#top_categories a:odd, #bottom_categories a:odd, #MyMenu a:odd").addClass("even");
    
    // handle subcategory block colors
    if( $("#subcategory_items_container") )
    {
        $("#subcategory_items_container div.subcatitem:odd").addClass("odd");
        $("#subcategory_items_container div.subcatitem:nth-child(3n)").addClass("third");
    }
    
    // handle item list bgcolor
    if( $("#advertisement_items_container li") )
    {
        $("#advertisement_items_container div.borderadvertisement:odd").addClass("even");
    }
    
    // handle advertisement detail property color
    if( $("#advertisement_detail_container dt") )
    {
        $("#advertisement_detail_container dt:odd, #advertisement_detail_container dd:odd").addClass("even");
    }
    
    // handle advertisement detail images
    if( $('#img_container li img') )
    {
        // bind mouseover to the small images
        $('#img_container li img').bind("mouseover", function(e) 
        {
            // get the mouseover image src
            var imagedata = this.src;
            // rewrite the mouseover image src
            var newimagedata = imagedata.replace( '/format1/', '/format2/' );
            var newbigimagedata = imagedata.replace( '/format1/', '/' );
            // update the main image src
            $('#main_image_div a').attr( 'href', newbigimagedata );
            $('#main_image_div img').attr( 'src', newimagedata );
        });
        // bind click to the big image to open lightbox for bottom images.
        $('#main_image_div img').bind("click", function(e) 
        {
            var imagedata = this.src;
            var newbigimagedata = imagedata.replace( '/format2/', '/' );
            var file1 = newbigimagedata.match(/[^\/]*$/);
            $('#img_container li a[href$="' + file1 + '"]').removeAttr( 'rel' );
        
            $(function() {
                // Use this example, or...
                $('a[rel*=lightbox]').lightBox(
                    {
                        overlayBgColor: '#333',
                        overlayOpacity: 0.8,
                        imageLoading: '/bsl/img/jquery/lightbox/loading.gif',
                        imageBtnClose: '/bsl/img/jquery/lightbox/close.gif',
                        imageBtnPrev: '/bsl/img/jquery/lightbox/prev.gif',
                        imageBtnNext: '/bsl/img/jquery/lightbox/next.gif',
                        containerResizeSpeed: 350,
                        txtImage: 'Afbeelding',
                        txtOf: 'van'
                       }
                ); 
            });
            
            $('#img_container li a[href$="' + file1 + '"]').attr( 'rel', 'lightbox' );
        });
    }
    
    // handle advertisement price type selection
    // if price_type price (20) is selected we need to show the price field.
    if( $('input[name=price]') && $('select[name=price_type_code_cid]').children("option:selected").attr("value") != 20 )
    {
        $('input[name=price]').attr( 'disabled', 'disabled' );
        $('input[name=price]').css( 'background', '#f9f9f9' );
    }
    if( $('select[name=price_type_code_cid]') && $('input[name=price]') )
    {
        $('select[name=price_type_code_cid]').change(function () { 
            // if the selected value = 20 ( price ) we need to show the price field.
            if( this.value == 20 )
            {
                $('input[name=price]').removeAttr( 'disabled' );
                $('input[name=price]').css( 'background', '#FFFFE3' );
            }
            // if selected value != 20 we need to disable the field again.
            else
            {
                $('input[name=price]').attr( 'disabled', 'disabled' );
                $('input[name=price]').css( 'background', '#f9f9f9' );
            }
        });
    }
    
    // handle terms and conditions logic
    /*if( $('#agree_terms_conditions').is(':visible') )
    {
        //$('button[@name=next]').toggle();
        $('input[@name=agree_terms]').change( function() 
        {
            $('button[@name=next]').toggle();
        } );
    }*/
    
    if( $('input[name=AdvertisementReactionButton]').is(':visible') )
    {
        $('#formContainer').toggle();
        $('input[name=AdvertisementReactionButton]').click( function()
        {
            if( $('#formContainer').is(":hidden") )
            {
                $('#formContainer').slideDown('slow');
            }
        } );
    }
    
    // terms and conditions iframe 
    $('#termsConditionsLink').click( function()
    {
        document.getElementById( "PopupPageFrame" ).src = "/pagina/algemene-voorwaarden.html?layout=popup";
        $("#termsConditionsContainer").fadeIn('slow');
    } );
    
    
    // change payment method
    $('input[name=psp_pr_id]').change( function(){
        $( 'p[id^="paymentType_"]' ).hide();
        $( '#paymentType_' + $(this).val() ).show();
    } );
    
});

function PopupClose()
{
    $("#termsConditionsContainer").fadeOut('slow');
}

