/**************************************
# Klant:            Tunturi
# Gemaakt op:       14-10-2009 
# Gewijzigd op:     25-11-2009
# Auteur:           Thom van den Hork <thomvandenhork@gmail.com>
# Bestandsnaam:     init.js
**************************************/

var $image_container, $images, $currentImage;

// Weergeven van de schaduw achter de category items
$(document).ready(function(){
    $(document)[0].oncontextmenu = function() {return false;}
    
    // Externe Links
    $('a[rev=ext]').attr('target','_blank');
    
    // Schaduw bij de categorie namen
    $('#category-menu a').dropShadow({
        left: 1,
        top: 1,
        blur: 1
    });    
 
    $('#sub-navigation .first-child a').attr('href',ABS_URI);

    $('#navigation li a[href$=/ext_dealers/]').each(function(){
        $(this).attr('href','http://accell.orange-juice.nl/');
        $(this).attr('target','_blank');
    });

    $('input.reseller').click(function(){
       location.href = ABS_URI+'resellers/';
       return false;
    });
    
    // Weergeven van de schaduw achter de header image tekst
    $('#header-image-text').dropShadow({
            left: 1,
            top: 1,
            blur: 1
    });
    
    // Image slider 
    $image_container = $('#header-image');
    
    if($image_container.size() > 0) {
        $images         = $image_container.find('img');
        $currentImage   = $image_container.find('img:last');
        $currentImage.addClass('last-child');
        
        if($images.size() > 1) {
            pauze();    
        }
    }
    
    
    // MenuButtons 
    $('#category-menu li > a').hover(
        function(){
            $(this).parent().css('background','#'+$(this).attr('rel'));        
        }, 
        function(){
            if(!$(this).parent().hasClass('active')) {
                $(this).parent().removeAttr('style');
            }
    });
    
    // Current Category
    $('#category-menu li.active > a').each(function(){
        $(this).parent().css('background','#'+$(this).attr('rel')); 
    });
    
    // Product Compare Form
    $('form.clear-product-form').each(function(){
        if($(this).find('input').size()>0){
            $(this).find('a').click(function(){
                $(this).parents('form').submit();
                return false;
            });
        } return true;
    });
    
    $('form.select-product-form').each(function(){
        $(this).find('select').change(function(){
            $(this).parents('form').submit();
        }) 
    });
    
    // Print link
    $('a[rel=print]').click(function(){
        window.print();
        return false; 
    });
    
    // Checkboxes 
    var checkboxes = $('#product-overview input[type=checkbox]'); 
    checkboxes.click(function(){
        if($('#product-overview input[type=checkbox]:checked').size()==3) {
            checkboxes.each(function(){
                if(!$(this).attr('checked')){
                    $(this).attr('disabled','disabled');
                }    
            });
        } else {
            checkboxes.removeAttr('disabled');
        }
    });
    
    // Zorgt ervoor dat compare button disabled is
    $('#product-overview input[type=submit].compare').click(function(){
         return ($('#product-overview input[type=checkbox]:checked').size()>0);
    })
    
    // Zoek Machine
    var search_input_value;
    var search_input = $('#search input');
    
    search_input.click(function(){
        search_input_value = $(this).val();
        $(this).val('');         
    });
    
    search_input.blur(function(){
        if($(this).val() == '') {
            $(this).val(search_input_value);
        }
    });
    
    brochureReplace.init();
});

var slideImage = function() {
    $currentImage.removeClass('last-child');
    $currentImage = $image_container.find('img:last');
    $currentImage.addClass('last-child');
    $currentImage.animate({opacity:0},1500,null,function(){
        $image_container.prepend($currentImage);
        $currentImage.removeAttr('style');
        pauze();
    });    
}


// Pauze 5 sec, daarna roept ie slideImage() aan
var pauze = function() {
    setTimeout("slideImage()",5000);
}

var brochureReplace = {
	targetReplace: 	null,
	iframe:			null,
	
	init: function() {
		this.targetReplace 		= $('#brochure-replace');
		this.iframe				= $('<object>');
		$('body').removeClass('no-menu');
		this.replace();
		
	},
	
	replace: function() {
		this.iframe.attr('data', BROCHURE_FOLDER);
		this.iframe.attr('type','text/html');
		this.targetReplace.html(this.iframe);
	}
}