$(document).ready(function(){
    // Select checkboxes within parent form
    $('a[name=select]').click(function(){
        $(this).parents('form').find('input[type=checkbox]').attr('checked', 'checked');
    });

    // Deselect checkboxes within parent form
    $('a[name=deselect]').click(function(){
        $(this).parents('form').find('input[type=checkbox]').removeAttr('checked');
    });

    // Toggle checkboxes within parent form
    $('a[name=toggle]').toggle(
        function() {$(this).parents('form').find('input[type=checkbox]').attr('checked', 'checked');},
        function() {$(this).parents('form').find('input[type=checkbox]').removeAttr('checked');}
    );

    // Confirmation
    $('a[name=confirm]').click(function() {
        ans = confirm('Da li ste sigurni?');
        if(!ans) return false;
        return true;
    });
    $('input[name=confirm]').click(function() {
        ans = confirm('Da li ste sigurni?');
        if(!ans) return false;
        return true;
    });

    // Flash rounded corners
    $('div#flash > div, #errorExplanation').corner("3px");
    $('div#flash').css({ 'left': centerBox('#flash') });
    $('a#flash_close').click( function(){
        $(this).parent().fadeOut();
    });

    // ivan comment: ovo ucitava u admin panelu, mozes slobodno nekako da iskomentarises :-)
    $('a#order').fancybox({
        'overlayOpacity': 0.65,
        'showCloseButton': true,
        'transitionIn': 'fade',
        'transitionOut': 'fade'
    });

    $(window).resize(function() {
        $('div#flash').css({ 'left': centerBox('#flash') });
    });

    $('a[name=rate]').click(function() {
        var rate = $(this).text();
        $('#rating').val(rate);
        $(this).parent().siblings('.current-rating').css('width', rate*20+'%');
    });

    $("ul#products li").equalHeights();
    $('#productImages div').hide(); //hide images for now
});

// cycle didn't work always in Chrome, more precisely when images didn't load first
// used this as a solution:
// http://stackoverflow.com/questions/544993/official-way-to-ask-jquery-wait-for-all-images-to-load-before-executing-something
$(window).load(
    function() {
        $('#productImages div').show();
        $('#productImages div').after('<ul class="thumbs">').cycle({
            fx:     'fade',
            speed:  1500,
            timeout: 4000,
            pager:  '.thumbs',

            pagerAnchorBuilder: function(idx, slide) {
                return '<li><a href="#"><img src="' + slide.src + '" width="69" height="69" /></a></li>';
            }
        });
    }
);

function centerBox(id){
    var winWidth = $(window).width()/2;
    var boxWidth = $(id).width()/2;
    return winWidth-boxWidth;
}

/**
 * Equal Heights Plugin
 * Equalize the heights of elements. Great for columns or any elements
 * that need to be the same size (floats, etc).
 *
 * Version 1.0
 * Updated 12/10/2008
 *
 * Copyright (c) 2008 Rob Glazebrook (cssnewbie.com)
 *
 * Usage: $(object).equalHeights([minHeight], [maxHeight]);
 *
 */

(function($) {
    $.fn.equalHeights = function(minHeight, maxHeight) {
        tallest = (minHeight) ? minHeight : 0;
        this.each(function() {
            if($(this).height() > tallest) {
                tallest = $(this).height();
            }
        });
        if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
        return this.each(function() {
            $(this).height(tallest).css("overflow","auto");
        });
    }
})(jQuery);