/**
 * @author Matej Keglevič <matej@8bit.si>
 */
Util = {};
 
Util.add_ajax_to_url = function(url) {
    if ( typeof url == "string" )
    {
        if ( url.indexOf('?') === -1 )
        {
            // add ?ajax
            url += '?ajax';
        }
        else
        {
            // add &ajax
            url += '&ajax';
        }
        
        url += "&ignoreMe=" + new Date().getTime();
    }
    
    return url;
};

Util.load_thickbox = function() {
    tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
};

Util.set_ajax = function() {
    /* PAGINATION */
    $('#content .pagination a').bind('click', function() {
        $.get(Util.add_ajax_to_url(this.href), function(responseText) {
            $('#content').html(responseText);
            Util.set_ajax();
            Util.load_thickbox();
        });
        
        return false;
    });
    
    /* HEADER */
    $('#content h1 a').bind('click', function() {
        $.get(Util.add_ajax_to_url(this.href), function(responseText) {
            $('#content').html(responseText);
            Util.set_ajax();
            Util.load_thickbox();
        });
        
        return false;
    });
    
    /* PRODUCT */
    $('#content ul li dl.product dt a').bind('click', function() {
        $.get(Util.add_ajax_to_url(this.href), function(responseText) {
            $('#content').html(responseText);
            Util.set_ajax();
            Util.load_thickbox();
        });
        
        return false;
    });
    
    /* ADD CART */
    $('#content ul li div.price a').bind('click', function() {
        $.get(Util.add_ajax_to_url(this.href), function(responseText) {
            $('#head a#cart dl dd').html(responseText);
        });
        
        return false;
    });
};

Util.form_submit = function() {
    var options = { 
        success: Util.form_submit_succes,
        url: Util.add_ajax_to_url($('#sidebar form').attr('action'))
    }; 
 
    $('#sidebar form').ajaxForm(options); 
};

Util.form_submit_succes = function(response, status) {
    $('#content').html(response);
    //Util.set_ajax();
    Util.load_thickbox();
};

$(document).ready(function() {
    /* ADD CART */
    $('#content ul li div.price a').bind('click', function() {
        $.get(Util.add_ajax_to_url(this.href), function(responseText) {
            $('#head a#cart dl dd').html(responseText);
        });
        
        return false;
    });
    
    Util.form_submit();
    
    $('input#q').focus(function() {
        if ($(this).attr('value') == 'Poišči izdelek') {
            $(this).attr('value', '');
        } else {
            $(this).select();            
        }
        
        
        $(this).blur(function() {
            var v = trim($(this).attr('value'));
            
            if (v.length == 0) {
                $(this).attr('value', 'Poišči izdelek');
            }
        })
    });
    
    $('.iframe').hide();
    
    $('#main #content div.extra_info div').hide();
    
    $('#main #content div.extra_info h3 a').click(function() {
        if ($(this).parent().parent().find('div').is(':hidden')) {
            $(this).parent().parent().find('div').fadeIn('slow');
        } else {
            $(this).parent().parent().find('div').hide();
        }
        
        return false;
    });
    
    //$('#main #main_navi ul[class!="show"] li[class!="toggle"]').hide();
    
    $.each($('#main #main_navi ul'), function(index, ul) {
        if (!$(ul).find('li.toggle').find('a').is('.tilt')) {
            var $li = $(ul).find('li[class!="toggle"]');
            $($li).hide();
        }
    });
    
    $('#main #main_navi ul li.toggle a').click(function() {
        if ($(this).parent().parent().parent().find('li[class!="toggle"]').is(':hidden')) {
            $(this).addClass('tilt');
            $(this).parent().parent().parent().find('li[class!="toggle"]').fadeIn('slow');
        } else {
            $(this).removeClass('tilt');
            $(this).parent().parent().parent().find('li[class!="toggle"]').hide();
        }
        
        return false;
    });
       
    // Util.set_ajax();
    
    /* GLAVNI MENI */
    /*
    $('#sidebar_navi ul li[class!="l"] a').click(function() {
        $.get(Util.add_ajax_to_url(this.href), function(responseText) {
            $('#content').html(responseText);
            Util.set_ajax();
            Util.load_thickbox();
        });
        
        return false;
    });*/
    
    /* KATEGORIJE */
    /*
    $('#main #main_navi ul li[class!="toggle"] a').click(function() {
        $('#main #main_navi ul a').removeClass('click');
        $(this).addClass('click');
        
        $.get(Util.add_ajax_to_url(this.href), function(responseText) {
            $('#content').html(responseText);
            Util.set_ajax();
            Util.load_thickbox();
        });
        
        return false;
    });*/
    
    /* CART */
    /*
    $('#head a#cart').bind('click', function() {
        $.get(Util.add_ajax_to_url(this.href), function(responseText) {
            $('#content').html(responseText);
            Util.set_ajax();
            Util.load_thickbox();
        });
        
        return false;
    });*/
});