/*******************************************************
 * Copyright (C) 2006 Blend Interactive, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 **********************************************************/
/**********************************************************
 * Blend Javascript Rollovers V 1.0.0
 *
 * To install: 
 * -- Include the script in the page: 
 * <script src="javascripts/rollovers.js" type="Javascript" />
 *
 * -- Create the rollover image pair with _off and _on in the names.
 * e.g. "home_off.gif" and "home_on.gif"
 *
 * -- Add the image tag with a class of "rollover":
 * <img src="images/home_off.gif" class="rollover" />
 *********************************************************/

var ROLLOVER_CLASSNAME = 'hover';
var OFF_SUFFIX = '_off';
var ON_SUFFIX = '_on';

/**********************************************************
 * Here's the meat of the operation - this function sets 
 * up mouseover functions on all IMG tags with a 
 * classname of ROLLOVER_CLASSNAME.
 *********************************************************/
function enableMenuImageRollovers()
{
	var imgs = getElementsByTagAndClassName('img',ROLLOVER_CLASSNAME);
	
	for(var i = 0; i < imgs.length; i++)
	{
		var img=imgs[i];
		
		//Create a couple of new image objects.
		var imgOn = new Image();
		var imgOff = new Image();
		
		//Set their source (this triggers the browser to load them).
		imgOn.src=img.src.replace(OFF_SUFFIX,ON_SUFFIX);
		imgOff.src=img.src.replace(ON_SUFFIX,OFF_SUFFIX);
		
		//Bind them to the original image.
		img.imgOn = imgOn;
		img.imgOff = imgOff;
		
		//Switch to them on mouseover/mouseout.
		img.onmouseover=function() {
			this.src=this.imgOn.src;
		}
		
		img.onmouseout=function() {
			this.src=this.imgOff.src;
		}
	}

}


/**********************************************************
 * A handy utility function that lets you find elements 
 * with a certain className, which is a common need.
 * This will properly support cases where there are 
 * multiple class names assigned to an object.
 *********************************************************/
function getElementsByTagAndClassName(tagName, className)
{
	var items = new Array();
	var elems = document.getElementsByTagName(tagName);
	for(var i = 0; i < elems.length; i++)
	{
		var elem = elems[i];
		var classNames = elem.className.split(" ");
		for (var j = 0; j < classNames.length; j++)
		{
			if(classNames[j] == className)
			{
				items.push(elem);
			}
		}
	}
	return items;
}

function clearForm(x) {
  document.getElementById(x).value= "";
}


/**********************************************************
 
 	These functions implement cookie handling, 
 	as presented by Peter-Paul Koch at QuirksMode.org, 
 	to track filtered choices between users.
 	
 *********************************************************/

function setCookie(name, value, days)
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function clearCookie(name) {
	csetCookie(name,"",-1);
}

function getElementsByTagAndClassName(tagName, className) {
    var items = new Array;
    var elems = document.getElementsByTagName(tagName);
    for (var i = 0; i < elems.length; i++) {
        var elem = elems[i];
        var classNames = elem.className.split(" ");
        for (var j = 0; j < classNames.length; j++) {
            if (classNames[j] == className) {
                items.push(elem);
            }
        }
    }
    return items;
}

function itemFilter() {
	var products = $(".Product");
	products.fadeTo(0.001, 0.3);
	$(".Product img").fadeTo(0.001, 0.3);
	
//	products.css('filter','progid:DXImageTransform.Microsoft.Alpha(opacity=50)');
	//$(".Product img").css('filter','progid:DXImageTransform.Microsoft.BasicImage(grayScale=1)');
	
//	products.css('filter','progid:DXImageTransform.Microsoft.Blur(pixelradius=3)');
	$('#filter_list').html('');
	var list = $("input.filter_check");

	$('.only').html('');   // 'Turns-off' the visual indicator when not checked.
//alert(document.getElementsByClassName);
	filterItems=new Array();
	cookieValues = new Array(); // Begin array for cookie handling.
	for(var i = 0; i < list.length; i++)
	{
		var item = list[i];
		if (item.checked)
		{
		
			//$("div." + item.value).show();
			$('.only').html('showing only: ');  // 'Turns-on' the visual indicator when filters checked.
			
			$('#filter_list').html($('#filter_list').html() + ' <img src="/app/design/images/icons/' + item.value + '.gif" />');
			$('#filter_list')
			var productflag = '.' + item.value;
			//			alert(productflag);

			products = products.filter(productflag);
			filterItems[filterItems.length]=productflag;
			
			cookieValues[i]="1";
		} else {
			cookieValues[i]="0"; // Only need to know if option is checked or not. 
		}
	}
	
	//products.show('');
	products.fadeTo(0.001,1.0);
	//products.each(function(i){var x = ''; for(j in this){ x=x+j+'=' + this[j] + "\n";}; alert(x);this.fadeTo(0.001,1.0);});
	products.each(function(i){$(this).find("img").fadeTo(0.001,1.0);});

	//products.css('filter','progid:DXImageTransform.Microsoft.Alpha(opacity=100)');
	//products.each(function(i) {alert(i);});
	var values = cookieValues.join('-'); // Implode array.
	setCookie('FILTER',values,1);		 // Set cookie 
}

function enableFilter()
{
    var prevC = getCookie('FILTER');
    if (prevC)
    {
        var pC_arr = new Array();
        var fs = $("input.filter_check");
        pC_arr = prevC.split('-');
        $('.only').html('');
        for(var j=0;j<fs.length;j++)
        {
                if(pC_arr[j]=='1')
                {
                        fs[j].checked=true;
                        $('.only').html('showing only: ');
                        $('#filter_list').html($('#filter_list').html() + ' <img src="/app/design/images/icons/' + fs[j].value + '.gif" />');
                }
        }
    }
	$("input.filter_check").click(itemFilter);
}

function triple_B_accordion(){
	$(".Submenu").hide();
	$(".hide").hide();
        	
	$(".Menu").click(function(){
		$(".Menu").siblings(".Submenu").hide("normal");
		
		var MenuElements = $(this).get();
		var menu_id = MenuElements[0];	
		var slide_id = "#slide_"+menu_id.id.slice(7);
		
		/* This section allows showing a fade-in/out slide, per LI element. */
//		var SlideElements = $(slide_id).get();
//		var slide = SlideElements[0];
				
//		if(slide.style.display=="block"){
//			$(slide_id).fadeOut();
//		}

		if (this.className.indexOf("Selected") > -1) {
			$(this).removeClass("Selected");
			return false;
		}
				
		$(".hide").fadeOut();
		$(slide_id).fadeIn("slow");
		$(".Menu").removeClass("Selected");
		$(this).addClass("Selected").siblings(".Submenu").show("normal");
		return false;
	});
};

function enableFAQExpanders()
{
    $('#brands_accordion').accordion({
        active: true,
        header: '.heading',
        navigation: true,
        event: 'mouseover',
        fillSpace: false,
        animated: 'slide'
    });
/*
  var blocks = getElementsByTagAndClassName('div','ExpandableBrandListings');
  for(var i=0; i < blocks.length ; i++)	
  {
    var block = blocks[i];
    var question = block.getElementsByTagName('strong')[0];
    var answer = block.getElementsByTagName('ul')[0];
    question.innerHTML = "<a href=\"#\">" + question.innerHTML + "</a>";
    var href = question.getElementsByTagName('a')[0];
    href.answer = answer;
    href.onclick=function(){

		
			if(this.className=="")
			{
				this.className="TurnDown";
			}
			else
			{
				this.className="";
			}



			if (this.answer.style.display=='none')
			{
				this.answer.style.display;
			}
			else
			{
				Effect.BlindUp(this.answer);
			}
			return false;      
    };
    answer.style.display='none';
  }
*/
}





function bindBehaviors()
{
    enableMenuImageRollovers();
    triple_B_accordion();
//  EnableHoverByClassAndTag("TopNav", "li");
  //EnableHoverByClassAndTag("TopNavigation", "li");
 // EnableHoverByClassAndTag("AdditionalContentInner", "a");you
    enableFilter();
    if ($("input.filter_check").length > 0)
    {
        itemFilter();
    }
}

//window.onload=bindBehaviors;
$('document').ready(bindBehaviors);