$(function() {
    
    /* ------------------------------------------------------------------------------------------------ */
    // Only display images when needed
    $("img:not('.notlazy')").lazyload({
        placeholder : "/assets/images/placeholder-grey.gif",
        effect : "fadeIn"
    });
    
    /* ------------------------------------------------------------------------------------------------ */
    // Lightbox    
    $("a.lightbox").fancybox({
        'titlePosition' 	: 'inside',
        'transitionIn'		: 'elastic',
		'transitionOut'		: 'fade'
    });
    
    /* ------------------------------------------------------------------------------------------------ */
    // Tooltip
    $('#hp-slider a[href][title]').qtip(
                                        {
                                            content: {
                                                text: true // Use each elements title attribute
                                            },
                                            fixed: true,
                                            position: {
                                                corner: {
                                                    target: 'topMiddle',
                                                    tooltip: 'center'
                                                },
                                                adjust: {
                                                    x: 0,
                                                    y: -25
                                                }
                                            },        
                                            style: { 
                                                padding: 10,
                                                background: '#000000',
                                                color: 'white',
                                                textAlign: 'center',
                                                border: {
                                                   width: 2,
                                                   radius: 5,
                                                   color: '#666666'
                                                },
                                                name: 'dark' 
                                            }
                                        }
                                    );
   
    /* ------------------------------------------------------------------------------------------------ */
    // Search panel
    
    // Expand
	$("#search-open").click(function(){
		$(".search-area").slideDown("slow");	
	});	
	
	// Collapse
	$("#search-close").click(function(){
		$(".search-area").slideUp("slow");	
	});		
	
	// Switch
	$("#search-toggle a").click(function () {
		$("#search-toggle a").toggle();
	});


    /* ------------------------------------------------------------------------------------------------ */
    // SEARCH FUNCTIONS
    if ($('input:radio[name=product]:checked').val() == 'blinds') { blindssearch(); }
    else if ($('input:radio[name=product]:checked').val() == 'curtains') { m2osearch(); }
    else if ($('input:radio[name=product]:checked').val() == 'chairs') { chairssearch(); }
    else if ($('input:radio[name=product]:checked').val() == 'hb') { hbsearch(); }
    else if ($('input:radio[name=product]:checked').val() == 'bv') { bvsearch(); }
    else if ($('input:radio[name=product]:checked').val() == 'bs') { bssearch(); }
    else if ($('input:radio[name=product]:checked').val() == 'cush') { cushsearch(); }
    else if ($('input:radio[name=product]:checked').val() == 'tb') { tbsearch(); }
    else if ($('input:radio[name=product]:checked').val() == 'fab') { fabsearch(); }
    else { m2osearch(); }    
    
    $('input:radio[name=product]').change(function() {        
        //If it's a blinds selection we need to replace the html in the div
        if ($(this).val() == 'blinds') { blindssearch(); }        
        if ($(this).val() == 'curtains') { m2osearch(); }        
        if ($(this).val() == 'chairs') { chairssearch(); }
        if ($(this).val() == 'hb') { hbsearch(); }
        if ($(this).val() == 'bv') { bvsearch(); }
        if ($(this).val() == 'bs') { bssearch(); }
        if ($(this).val() == 'cush') { cushsearch(); }
        if ($(this).val() == 'tb') { tbsearch(); }
        if ($(this).val() == 'fab') { fabsearch(); }
    });        
    
    function blindssearch() {
        //Hide all other search fieldsets            
        $(".m2o-search").css("display", "none");        
        //Show the relevant fields
        $(".blinds-search").css("display", "block");        
        //Change the URL of the search
        $("#MainSearch").attr('action', '/blinds/results');
    }
    
    function m2osearch() {
        //Hide all other search fieldsets            
        $(".blinds-search").css("display", "none");        
        //Show the relevant fields
        $(".m2o-search").css("display", "block");        
        //Change the URL of the search
        $("#MainSearch").attr('action', '/curtains/results');
    }
    
    function chairssearch() {
        //Hide all other search fieldsets            
        $(".blinds-search").css("display", "none");        
        //Show the relevant fields
        $(".m2o-search").css("display", "block");        
        //Change the URL of the search
        $("#MainSearch").attr('action', '/chairs/results');
    }
    
    function hbsearch() {
        //Hide all other search fieldsets            
        $(".blinds-search").css("display", "none");        
        //Show the relevant fields
        $(".m2o-search").css("display", "block");        
        //Change the URL of the search
        $("#MainSearch").attr('action', '/headboards/results');
    }
    
    function bvsearch() {
        //Hide all other search fieldsets            
        $(".blinds-search").css("display", "none");        
        //Show the relevant fields
        $(".m2o-search").css("display", "block");        
        //Change the URL of the search
        $("#MainSearch").attr('action', '/basevalance/results');
    }
    
    function bssearch() {
        //Hide all other search fieldsets            
        $(".blinds-search").css("display", "none");        
        //Show the relevant fields
        $(".m2o-search").css("display", "block");        
        //Change the URL of the search
        $("#MainSearch").attr('action', '/bedspreads/results');
    }
    
    function cushsearch() {
        //Hide all other search fieldsets            
        $(".blinds-search").css("display", "none");        
        //Show the relevant fields
        $(".m2o-search").css("display", "block");        
        //Change the URL of the search
        $("#MainSearch").attr('action', '/cushions/results');
    }
    
    function tbsearch() {
        //Hide all other search fieldsets            
        $(".blinds-search").css("display", "none");        
        //Show the relevant fields
        $(".m2o-search").css("display", "block");        
        //Change the URL of the search
        $("#MainSearch").attr('action', '/tiebacks/results');
    }
    
    function fabsearch() {
        //Hide all other search fieldsets            
        $(".blinds-search").css("display", "none");        
        //Show the relevant fields
        $(".m2o-search").css("display", "block");        
        //Change the URL of the search
        $("#MainSearch").attr('action', '/fabric/results');
    }


});






