(function($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function() {
        var args_len = arguments[0].length;
        for (var i = args_len; i--;) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[0][i];
            cache.push(cacheImage);
        }
    }
})(jQuery)

$(document).ready(function() {
    $('#xmlthumbs img').click(function() {
        set_image('/bilbilder/' + $(this).attr("id") + '/' + $(this).attr("alt") + '.jpg');
    });
    //When you click on a link with class of poplight and the href starts with a # 
    $('.poplight').click(function() {
        var popup = '#' + $(this).attr("href");
        var width = $(this).attr("width");
        show_popup(popup, width);
        return false;
    });

    //Close Popups and Fade Layer
    $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
        $('#fade , .popup_block').fadeOut(function() {
            $('#fade, a.close').remove(); //fade them both out
        });
        return false;
    });
    $("#disa").click(function() {
        $("#disdiv").slideDown();
        return false;
    });
    $("a.confirm").click(function() {
        var c = confirm("Vill du verkligen radera?");
        return c;
    });

    $('#news').tinyscrollbar();

    if (do_show_popup == true) show_popup(showDIV);

    $("a[rel=gallery]").each(function() {
        var element = this;
        $(this).fancybox({
            'transitionIn': 'none',
            'transitionOut': 'none',
            'titlePosition': 'over',
            'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
                return '<span id=\"fancybox-title-over\">Bild ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
            }
        });
    });
    $("a.iframe").fancybox({
        'width': '75%',
        'height': '90%',
        'autoScale': false,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'type': 'iframe'
    });
    $("a.zoomimage").fancybox({
        'autoScale': true,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic'
    });

    $('#slides').slides({
        preload: true,
        preloadImage: 'images/loading.gif',
        generatePagination: true,
        play: 5000,
        pause: 2500,
        hoverPause: true
    });
    $('#ad_slides').slides({
        preload: true,
        preloadImage: 'images/loading.gif',
        play: 5000,
        pause: 2500,
        crossfade: true,
        animationComplete: function(current) {
            var ads = ['insignia', 'meriva','corsa','astra'];
            $('.ad_text div').hide();
            $('.ad_text div p.extra').hide();
            $('.ad_text div p.readmore').show();
	    $('.ad_text div.'+ads[current-1]).show();
            if(ads[current-1]=='corsa'){
                    $('.ad_text').animate({right:'330px'},100);
            }else{
                $('.ad_text').animate({right:'20px'},100);
            }
            
	},
        hoverPause: true
    });
    $('div.ad_text > div').click(function() {
        $(this).find('p.extra').toggle();
        $(this).find('p.readmore').toggle();
    });
    Modernizr.addTest("boxsizing", function() {
        return Modernizr.testAllProps("boxSizing");
    });
    $('#reload').click(function() {
        set_image($(this).attr('href'));
        return false;
    });

});

function filter_cars(attr, value) {
    if (value == -1) window.location = '/bilar/';
    else window.location = '/bilar/filter/' + attr + '=' + value + '/';
}

function set_image(src) {
    var img = $('#bigimage');
    $('#loader').fadeIn();
    $(img).fadeTo('fast',0.2);
    $(img).load(function() {
        $('#loader').hide();
        $(this).fadeIn();
        $(img).fadeTo('fast',1);
    }).attr('src', src);
}

function show_popup(popup, width, height) {
    if (!width) width = $(popup).width();
    if (!height) height = $(popup).height();

    //Fade in the Popup and add close button
    $(popup).fadeIn().css({
        'width': width
    }).prepend('<a href="#" class="close"><img src="images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');

    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
    var popMargTop = (height + 80) / 2;
    var popMargLeft = (width + 80) / 2;

    //Apply Margin to Popup
    $(popup).css({
        'margin-top': -popMargTop,
        'margin-left': -popMargLeft
    });

    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({
        'filter': 'alpha(opacity=80)'
    }).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 
}

function validateEmail(string) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var address = "";
    for (var i = 0; i < arguments.length; i++) {
        address = $("#" + arguments[i]).val();
        if (reg.test(address) == false) {
            alert('Du måste ange giltiga email-adresser, du skrev "' + address + '"');
            return false;
        }
    }
    return true;
}

function validate(string) {
    var input = "";
    for (var i = 0; i < arguments.length; i++) {
        input = $("#" + id).val();
        alert(input);
        if (input == "") {
            alert('Fyll i alla obligatoriska fält.');
            return false;
        }
    }
    return true;
}
