// HP Creative Studio Main Navigation
// this code needs Mootools to work properly



var cs_pagestatus = 'category';			// 'category' or 'product'
var cs_navstatus = 'open';				// 'open' or 'closed'

var cs_main_page = 0;
var cs_main_page_max = 0;
var cs_width_products = 0;
var cs_visual_products = 0;
var cs_specialproduct = 0;

var cs_visual_products_width = 0;

var cs_products_virtual_length = 0;

var cs_product_remainder=0;
var cs_width_subtract=0;

function cs_setup_mainarea() {

	// ############# determine page and nav status
	cs_products = $$('*').filterByClass('csproduct');
	cs_categories = $$('*').filterByClass('cscategory');
	// ############# 3/25/2009 array of all product images
	cs_product_images=$$('*').filterByClass('csproduct').getElement('img');
    // ############# 3/31/2009 array of all category images    
	cs_product_categories=$$('*').filterByClass('cscategory').getElement('img');


	// ############# prune and clean object collection
	cs_itemholder = document.getElementById('cscataloginnerholder');
	if (cs_products.length > 0) {
		for (csa=0;csa<cs_products.length;csa++) {
			if (cs_products[csa].innerHTML == '') {						// remove empty products divs
				cs_itemholder.removeChild(cs_products[csa]);
			}
		}
	} else {
		for (csa=0;csa<cs_categories.length;csa++) {
			if (cs_categories[csa].innerHTML == '') {					// remove empty category divs
				cs_itemholder.removeChild(cs_categories[csa]);
			} else {
				if (cs_categories[csa].innerHTML.indexOf('http') != -1) {	// add targets to external links (categories only, since they have a mix of internal and external links)
					cs_itemlinks = $$(cs_categories[csa].getElementsByClassName('cscategoryimage')).getElementsBySelector('a');
					cs_itemlinks[0].setProperty('target', '_blank');
					cs_itemlinks = $$(cs_categories[csa].getElementsByClassName('cscategorytext')).getElementsBySelector('a');
					cs_itemlinks[0].setProperty('target', '_blank');
				}
			}
		}
	}

	// ############# re-determine number of items after removing empty divs
	cs_products = $$('*').filterByClass('csproduct');
	cs_categories = $$('*').filterByClass('cscategory');
	cs_product_images=$$('*').filterByClass('csproduct').getElement('img');
	cs_product_categories=$$('*').filterByClass('cscategory').getElement('img');

	// ############# initialize objects holder divs (length depends on number of objects), determine number of pages, add filter hint objects
	if (cs_products.length > 0) {										// products
		cs_visual_products_width = 785;
		// set filter display
		$('filterrightclosed').setStyle('visibility','visible');
		$('filterrightopen').setStyle('visibility','visible');
		//$('csmainfilterlegend1').setStyle('visibility','visible');
		cs_products_virtual_length = cs_products.length;

		// determine presence of special image, which is the size of 6 product icons
		cs_specialproduct = $$('*').filterByClass('csspecialimage');
		if (cs_specialproduct.length > 0) {
			cs_products_virtual_length = cs_products_virtual_length + 3;
		}
		cs_pagestatus = 'product';
		cs_main_page_max = Math.floor((cs_products_virtual_length-1)/10);
		cs_visual_products = cs_products_virtual_length;
		
	} else {																	// categories
		cs_visual_products_width = 785;

		cs_products_virtual_length = cs_categories.length;

		cs_main_page_max = Math.floor((cs_products_virtual_length-1)/10);
		cs_visual_products = cs_products_virtual_length;

	}

	// display correct "Page: "... links based on number of items
	cs_set_pagelinks();

	// ############# show arrow buttons if necessary
	if (cs_main_page_max > 0) {
		if (cs_pagestatus == 'product') {
			cs_arrow_vpos = 225;
		} else {
			cs_arrow_vpos = 220;
		}
		$('csmainarrowleftglow').setStyle('top',cs_arrow_vpos+'px');
		$('csmainarrowleftoff').setStyle('top',cs_arrow_vpos+'px');
		$('csmainarrowlefthit').setStyle('top',cs_arrow_vpos+'px');
		$('csmainarrowrightglow').setStyle('top',cs_arrow_vpos+'px');
		$('csmainarrowrightoff').setStyle('top',cs_arrow_vpos+'px');
		$('csmainarrowrightoff').setStyle('visibility','visible');
		$('csmainarrowrighthit').setStyle('top',cs_arrow_vpos+'px');
		$('csmainarrowrighthit').setStyle('visibility','visible');
	}

	// ############## 4/16/09 find visible projects to show images
	if(cs_pagestatus=='product'){
		//set two indexes - one for the top row of images visible, one for the bottom row
		var firstIndex=0;
		cs_product_remainder = cs_visual_products%10;
		if(cs_product_remainder>5){
			cs_product_remainder=5;	
		}
		var secondIndex=(Math.floor(cs_visual_products/10)*5)+(cs_product_remainder);
		//subtract 2 from second index if there's a special hero image
		if(cs_specialproduct.length>0) {
		   firstIndex=1;
		   secondIndex-=1;
		}
		loadTheImages(firstIndex,secondIndex);
	} else {
		// ################ 4/16/09 find visible categories to show images
		//set up two indexes for when we break it to two rows
		var firstIndex=0;
		cs_product_remainder = cs_visual_products%10;
		if(cs_product_remainder>5){
			cs_product_remainder=5;	
		}
		var secondIndex=(Math.floor(cs_visual_products/10)*5)+(cs_product_remainder);
	}
	
	// ############# reveal object holder with first page of objects
	if (cs_pagestatus == 'product') {												// products display
		// set initial product table
		$('cscataloginnerholder').setStyle('height','380px');
		cs_product_remainder = cs_products_virtual_length%10;
		if(cs_product_remainder>0){
			cs_width_subtract=(Math.ceil(cs_product_remainder/2)*157);	
		} else {
			cs_width_subtract=0;	
		}
		if(cs_product_remainder>5){
			cs_product_remainder=5;	
		}
		// establish width in number of products
		cs_width_products = Math.ceil(cs_products_virtual_length/2);

		if(cs_width_products*157 <= 785){
			$('cscataloginnerholder').setStyle('width',785+'px');
		} else {
			$('cscataloginnerholder').setStyle('width',((cs_width_products*157)+(cs_product_remainder*157)-cs_width_subtract)+'px');
		}

		$('cscatalogouterholder').setStyle('left','66px');
		$('cscatalogouterholder').setStyle('top','77px');
		$('cscatalogouterholder').setStyle('opacity','0');
		$('cscatalogouterholder').setStyle('visibility','visible');

		// check if the URL contains a filter instruction, if not just display catalog and go
		cs_filtercall = extract_parameter('filter');
		if (cs_filtercall != null) {												// this page was called from another page with a filter parameter
			// run filter (filter code will call cs_display_catalog())
			cs_filter_catalog(cs_filtercall,'parameter','');
			cs_filtername = unescape(extract_parameter('title'));
			// add name of clicked-on filter to breadcrumb
			cs_currentheader = $('csmainheader').innerHTML;
			cs_currentheader = cs_currentheader + '<span class="csmainbreadcrumbwhite">' + cs_filtername + '&nbsp;&gt;&nbsp;</span>';
			$('csmainheader').setHTML(cs_currentheader);
		} else {
			// no filter parameter, show catalog
			cs_display_available_filters();
			cs_display_catalog();
		}

		// display correct checkboxes
		cs_display_available_checkboxes();
	} else {																		// category display
		// set initial product table
		$('cscataloginnerholder').setStyle('height','380px');
		cs_product_remainder = cs_products_virtual_length%10;
		if(cs_product_remainder>0){
			cs_width_subtract=(Math.ceil(cs_product_remainder/2)*157);	
		} else {
			cs_width_subtract=0;	
		}
		if(cs_product_remainder>5){
			cs_product_remainder=5;	
		}
		// establish width in number of products
		cs_width_products = Math.ceil(cs_products_virtual_length/2);
		if(cs_width_products*157 <= 785){
			$('cscataloginnerholder').setStyle('width',785+'px');		
		} else {
			$('cscataloginnerholder').setStyle('width',((cs_width_products*157)+(cs_product_remainder*157)-cs_width_subtract)+'px');
		}
//+(Math.ceil((cs_product_remainder*157)/2)

		$('cscatalogouterholder').setStyle('left','66px');
		$('cscatalogouterholder').setStyle('top','72px');
		$('cscatalogouterholder').setStyle('opacity','0');
		$('cscatalogouterholder').setStyle('visibility','visible');

		cs_display_catalog();

	}
}

var FX_catalogreveal;
var FX_catalogreveal_previous = false;

// ############# catalog reveal animation
function cs_display_catalog() {
	if (FX_catalogreveal_previous) {
		FX_catalogreveal.stop();
	}
	// display product table
	FX_catalogreveal = new Fx.Style('cscatalogouterholder', 'opacity', {
		duration: 2000,
		fps: 12,
		transition: Fx.Transitions.Cubic.easeOut
	});
	FX_catalogreveal.start(0,1);
	FX_catalogreveal_previous = true;
}


// ############# populate numbered list of links to pages
var cs_maxpages_shown_inline = 4;										// number of page links to be shown inline in page, if there are more pages, we switch to a small layer popup for page number links
var cs_maxpages_shown_total = 0;

function cs_set_pagelinks() {
	cslinks = '';
	//3/31/09 pagination now only goes to 5, then adds last number
	var lowerLimit=0;
	var upperLimit=0;
	var showDots=true;
	if(cs_main_page_max>4){
		if(cs_main_page!=0){
			cslinks = cslinks + '<a href="javascript:cs_main_go('+(cs_main_page-1)+')"><img src="media\/images\/chrome\/mainarea\/prevarrow_on.gif" width="5" height="8" alt="Next" border="0" \/><\/a>     ';	
		} else {
			cslinks = cslinks + '<span style="color: #666;"><img src="media\/images\/chrome\/mainarea\/prevarrow_off.gif" width="5" height="8" alt="Next" border="0" \/><\/span>     ';	
		}
		if(((cs_main_page+1)>3)&&cs_main_page_max!=5){
			cslinks += '<a href="javascript:cs_main_go(0)">1<\/a> <span class="dots">...<\/span> ';
			if((cs_main_page-2)>1){
				lowerLimit=cs_main_page-2;
			} else {
				if((cs_main_page-1)>1){
					lowerLimit=cs_main_page-1;	
				} else {
					lowerLimit=cs_main_page;	
				}
			}
		}
		if(lowerLimit+4>=cs_main_page_max){
			upperLimit=(cs_main_page_max-1);
		} else {
			upperLimit=lowerLimit+4;
		}
		if(upperLimit==(cs_main_page_max-1)){
			showDots=false;
			if(cs_main_page_max!=5){
				lowerLimit+=1;
			}
		}
		if(upperLimit-lowerLimit<4){
			if(upperLimit-lowerLimit<3){
				if(upperLimit-lowerLimit<2){
					if(upperLimit-lowerLimit<1){	
						lowerLimit-=3;		
					} else {
						lowerLimit-=2;
					}
				} else {
					lowerLimit-=1;	
				}
			}
		}
	} else {
		lowerLimit=0;
		upperLimit=cs_main_page_max;
	}
	for(csa=lowerLimit;csa<=upperLimit;csa++){
		if (csa != cs_main_page) {
			cslinkstart = '<a href="javascript:cs_main_go('+csa+')">';
			cslinkend = '<\/a>';
		} else {
			cslinkstart = '';
			cslinkend = '';
		}
		cslinks = cslinks + cslinkstart + (csa+1) + cslinkend + ' ';
	}
	if(cs_main_page_max>4){
		if(cs_main_page==cs_main_page_max){
			cslinks+=(cs_main_page_max+1);
		} else {
			if(!showDots){
				cslinks = cslinks + '<a href="javascript:cs_main_go('+cs_main_page_max+')">'+(cs_main_page_max+1)+'<\/a>';		
			} else {
				cslinks = cslinks + '<span class="dots">...<\/span><a href="javascript:cs_main_go('+cs_main_page_max+')">'+(cs_main_page_max+1)+'<\/a>';		
			}
		}
		if(cs_main_page<cs_main_page_max){
			cslinks = cslinks + '     <a href="javascript:cs_main_go('+(cs_main_page+1)+')"><img src="media\/images\/chrome\/mainarea\/nextarrow_on.gif" width="5" height="8" alt="Next" border="0" \/><\/a>';
		} else {
			cslinks = cslinks + '     <span style="color: #666"><img src="media\/images\/chrome\/mainarea\/nextarrow_off.gif" width="5" height="8" alt="Next" border="0" \/><\/span>';	
		}
	}
	if(cs_main_page_max>0) {
		$('csmainpagelinksnumbers').setHTML(cslinks);
	} else {
		$('csmainpagelinksnumbers').setHTML('');
	}
}

function loadTheImages(firstIndex,secondIndex){
	//loop through each row, set the src of the image to the class, set class to loaded
	//set up a variable w/number of images to increment, based on hero image, size of imgs etc
	//alert(firstIndex + ", " + secondIndex);
	var theIncrement=5;
	if(cs_specialproduct.length>0&&cs_main_page==0) {
		theIncrement=3;
	} else {
		theIncrement=5;	
	}
	for(var x=firstIndex;x<firstIndex+theIncrement;x++){
		if(cs_product_images[x]&&cs_product_images[x].className!=""&&cs_product_images[x].className!="imgLoaded"){
			var tempClass=cs_product_images[x].className;
			cs_product_images[x].setAttribute('src',tempClass);
			cs_product_images[x].className="imgLoaded";
		}
	}
	for(var y=secondIndex;y<secondIndex+theIncrement;y++){
		if(cs_product_images[y]&&cs_product_images[y].className!=""&&cs_product_images[y].className!="imgLoaded"){
			var tempClassY=cs_product_images[y].className;
			cs_product_images[y].setAttribute('src',tempClassY);
			cs_product_images[y].className="imgLoaded";
		}
	}	
}

// ########################## info handlers

var cs_mouseinproduct = false;
var cs_mouseininfo = false;

function attachEvents(){
	document.onmouseup=cs_close_info2;
	$$(".csproductimage").addEvents({
		'mouseover':function(){
			cs_product_rollover(this);
		},'focus':function(){
			cs_product_rollover(this);
		},'mouseout':function(){
			cs_product_rollout();
		},'blur':function(){
			cs_product_rollout();
		}
	});
	$$(".csquickinfo").addEvents({
			'mouseout':function(){
				cs_quickinfo_rollout()
			},
			'blur':function(){
				cs_quickinfo_rollout()
			}
	});
}

// ############# called from product rollover, pull meta info and display info panel
function cs_product_rollover(cs_product_object) {
	cs_product_object=cs_product_object.getParent('div');
	if(cs_product_object.id!='csfilterhint' && cs_product_object.getFirst('div').className!='csspecialimage'){
		//alert(cs_product_object.getFirst('a').onmousedown);
		var cslink;
		//cs_mouseinproduct = true;
		cs_offset = (window.getWidth() - 886) /2;
		//show the quickinfo box
		cs_product_object.getElements('div.csquickinfo').setStyle('display','inline');

		cs_product_object.getElements('div.csquickinfo').addEvent('click',function(){
			cs_product_click(cs_product_object);
		});
	}
}
// ############# called from click on quickinfo rollover (above function
function cs_product_click(cs_product_object){
	$('cspopback2').setStyle('display','block');
	$('cspopback2').setStyle('opacity',0);
	$('cspopback2').setStyle('visibility','visible');
	FX_popfilterreveal = new Fx.Style('cspopback2', 'opacity', {
		duration: 2000,
		fps: 12,
		transition: Fx.Transitions.Cubic.easeOut
	});
	if (ie4) {
		popfilteropacity = 0.5;
	} else {
		popfilteropacity = 1;
	}
	FX_popfilterreveal.start(0,popfilteropacity);
	csmeta = cs_product_object.getElements('div.csproductmeta');
	cslink=cs_product_object.getElements('a');
    if(csmeta&&csmeta!=null&&csmeta!=""){
		$('csproductinfo').setHTML(csmeta[0].innerHTML + '<img src="media/images/chrome/mainarea/close_btn.gif" width="14" height="14" alt="Close" id="infoclose" onmousedown="cs_close_info2();" \/>'); 
		cs_offset = (window.getWidth() - 886) /2;
	
		if (cs_product_object.getLeft() > 600+cs_offset) {						// is this the right-most info panel
			cs_offset2 = 100;
		} else {
			cs_offset2 = 0;
		}
		if($('csproductinfo').getFirst('div').getFirst('img').className!="imgLoaded"){
			var tempClass=$('csproductinfo').getFirst('div').getFirst('img').className;
			$('csproductinfo').getFirst('div').getFirst('img').setAttribute('src',tempClass);
			$('csproductinfo').getFirst('div').getFirst('img').className="imgLoaded";
		}
		$('csproductinfo').setStyle('visibility','visible');
		$('csproductinfo').setStyle('display','block');
	}	
}
// ############# called from object rollout, wait 1/2 sec and then close rollover
function cs_product_rollout() {
	cs_mouseinproduct = false;
	setTimeout(cs_close_info,500);
}

// ############# called from 1/2 sec timer, hide rollover layers
function cs_product_rollout() {
	cs_close_info();
}

// ############# called from 1/2 sec timer, hide rollover layers
function cs_close_info() {
	$$('.csquickinfo').setStyle('display','none');
}
function cs_close_info2(){
	$('csproductinfo').setStyle('display','none');
	$('cspopback2').setStyle('visibility','hidden');
	$('cspopback2').setStyle('display','none');
}

// ############# remove flag, and set timer to close info panel
function cs_info_rollout() {
	cs_close_info();
}
function cs_quickinfo_rollout() {
	cs_close_info();
}



// ########################## checkbox rollover handlers

var cs_mouseincheckbox = false;
var cs_mouseincheckhelp = false;
var cs_alwaysclosetimer;

// ############# called from product rollover, pull meta info and display info panel
function cs_check_rollover(cs_product_object) {
	clearTimeout(cs_alwaysclosetimer);
	cs_mouseincheckbox = true;
	csmeta = cs_product_object.getElements('div.checkboxfiltermeta');

	$('cscheckboxinfo').setHTML(csmeta[0].innerHTML);
	cs_offset = (window.getWidth() - 886) /2;

	cs_offset2 = -20;

	$('cscheckboxinfo').setStyle('left',cs_product_object.getLeft()-cs_offset+cs_offset2+'px');
	$('cscheckboxinfo').setStyle('top',cs_product_object.getTop()-100+'px');
	$('cscheckboxinfo').setStyle('visibility','hidden');
	$('cscheckboxinfo').setStyle('display','block');
	$('cscheckboxinfoarrow').setStyle('left',cs_product_object.getLeft()-cs_offset+cs_offset2+10+'px');
	$('cscheckboxinfoarrow').setStyle('top',cs_product_object.getTop()-117+'px');
	$('cscheckboxinfoarrow').setStyle('visibility','visible');
	$('cscheckboxinfo').setStyle('visibility','visible');
	cs_alwaysclosetimer = setTimeout(cs_alwaysclose_checkinfo,5000);
}

// ############# called from object rollout, wait 1/2 sec and then close rollover
function cs_check_rollout() {
	cs_mouseincheckbox = false;
	setTimeout(cs_close_checkinfo,500);
}

// ############# called from 1/2 sec timer, hide rollover layers
function cs_close_checkinfo() {
	if (!cs_mouseincheckhelp  &&  !cs_mouseincheckbox) {
		$('cscheckboxinfo').setStyle('display','none');
		$('cscheckboxinfoarrow').setStyle('visibility','hidden');
	}
}
function cs_alwaysclose_checkinfo() {
	$('cscheckboxinfo').setStyle('display','none');
	$('cscheckboxinfoarrow').setStyle('visibility','hidden');
}

// ############# flag if mouse rolls over info panel within the 1/2 sec timer
function cs_checkinfo_rollover() {
	cs_mouseincheckhelp = true;
}

// ############# remove flag, and set timer to close info panel
function cs_checkinfo_rollout() {
	cs_mouseincheckhelp = false;
	setTimeout(cs_close_checkinfo,300);
}





// ######################## arrow/slide handlers

// called from numbered page links
function cs_main_go(cspage) {
	// determine direction
	if (cspage > cs_main_page) {
		csdist = cspage - cs_main_page;
		cs_main_go_right_core(csdist);
	} else {
		csdist = cs_main_page - cspage;
		cs_main_go_left_core(csdist);
	}
}

function cs_main_go_left() {
	cs_main_go_left_core(1);
}

function cs_main_go_right() {
	cs_main_go_right_core(1);
}

function cs_main_go_left_core(csn) {
	cs_oldpos = cs_main_page;
	cs_main_page = cs_main_page - csn;
	//set up indexes to load images if not already loaded
	//again, check for hero image and change math based on this
	firstIndex=cs_main_page*5;
	cs_product_remainder = cs_visual_products%10;
	if(cs_product_remainder>5){
		cs_product_remainder=5;	
	}
	secondIndex=(Math.floor(cs_visual_products/10)*5)+(cs_product_remainder)+(cs_main_page*5);
	if(cs_specialproduct&&cs_specialproduct.length>0&&cs_main_page!=0) {
		firstIndex-=1;
		secondIndex-=3;
	} else if (cs_specialproduct&&cs_specialproduct.length>0&&cs_main_page==0) {
		firstIndex=1;
		secondIndex-=2;
	}
	loadTheImages(firstIndex,secondIndex);
	if (cs_main_page == 0) {
		// disable left arrow
		$('csmainarrowleftoff').setOpacity(0);
		$('csmainarrowlefthit').setStyle('visibility','hidden');
	}
	if (cs_main_page < cs_main_page_max) {
		// enable right arrow
		$('csmainarrowrightoff').setOpacity(1);
		$('csmainarrowrighthit').setStyle('visibility','visible');
	}
	FX_main_slide = new Fx.Style('cscataloginnerholder', 'margin-left', {
		duration: 1000, 
		transition: Fx.Transitions.Cubic.easeInOut
	});
	FX_main_slide.start(-cs_oldpos * cs_visual_products_width,-cs_main_page * cs_visual_products_width);
	cs_set_pagelinks();
}

function cs_main_go_right_core(csn) {
	cs_oldpos = cs_main_page;
	cs_main_page = cs_main_page + csn;
	//set up indexes to load images if not already loaded
	firstIndex=cs_main_page*5;
	cs_product_remainder = cs_visual_products%10;
	if(cs_product_remainder>5){
		cs_product_remainder=5;	
	}
	secondIndex=(Math.floor(cs_visual_products/10)*5)+(cs_product_remainder)+(cs_main_page*5);
	if(cs_specialproduct&&cs_specialproduct.length>0&&cs_main_page!=0) {
		firstIndex-=1;
		secondIndex-=3;
	} else if (cs_specialproduct&&cs_specialproduct.length>0&&cs_main_page==0) {
		firstIndex=1;
		secondIndex-=2;
	}
	loadTheImages(firstIndex,secondIndex);
	if (cs_main_page == cs_main_page_max) {
		// disable right arrow
		$('csmainarrowrightoff').setOpacity(0);
		$('csmainarrowrighthit').setStyle('visibility','hidden');
	}
	if (cs_oldpos == 0) {
		// enable left arrow
		$('csmainarrowleftoff').setOpacity(1);
		$('csmainarrowlefthit').setStyle('visibility','visible');
	}
	FX_main_slide = new Fx.Style('cscataloginnerholder', 'margin-left', {
		duration: 1000, 
		transition: Fx.Transitions.Cubic.easeInOut
	});
	FX_main_slide.start(-cs_oldpos * cs_visual_products_width,-cs_main_page * cs_visual_products_width);
	cs_set_pagelinks();
}


var FX_mainarrowrollover;

function cs_main_arrow_rollon(csarrow) {
	if (FX_mainarrowrollover) {
		FX_mainarrowrollover.stop();
	}
	$('csmainarrow'+csarrow+'glow').setOpacity(0);
	FX_mainarrowrollover = new Fx.Style('csmainarrow'+csarrow+'glow', 'opacity', {
		duration: 500, 
		transition: Fx.Transitions.Cubic.easeOut
	});
	FX_mainarrowrollover.start(0,1);
}

function cs_main_arrow_rolloff(csarrow) {
	if (FX_mainarrowrollover) {
		FX_mainarrowrollover.stop();
	}
	$('csmainarrow'+csarrow+'glow').setOpacity(0);
	FX_mainarrowrollover = new Fx.Style('csmainarrow'+csarrow+'glow', 'opacity', {
		duration: 150, 
		transition: Fx.Transitions.Cubic.easeOut
	});
	FX_mainarrowrollover.start(1,0);
}




// #################### filter handlers

cs_filteropen = new Array();
cs_filteropen['right'] = false;
cs_filtervalue = new Array();
cs_filtervalue['right'] = '';

cs_checkstatus = new Array();
cs_checkstatus['left1'] = 'off';
cs_checkstatus['left2'] = 'off';
cs_checkstatus['left3'] = 'off';
cs_checkvalue = new Array();
cs_checkvalue['left1'] = 'att-diyfree';
cs_checkvalue['left2'] = 'att-diypay';
cs_checkvalue['left3'] = 'att-custompay';

// open filter dropdowns
function cs_openfilter(cs_filter) {
	// find all options for filter
	cs_options = $$('*').filterByClass('filter'+cs_filter+'option');
	cs_options_open = 0;
	for (csa=0;csa<cs_options.length;csa++) {
		if (cs_options[csa].getStyle('display') == 'block') {
			cs_options_open++;
		}
	}	
	cs_height = 22 * cs_options_open + 2;

	if (cs_filteropen[cs_filter]) {		// close filter
		cs_closefilter(cs_filter);
	} else {							// open filter
		$('filter'+cs_filter+'openholder').setStyle('margin-top',-cs_height+'px');
	
		FX_filter_slide = new Fx.Style('filter'+cs_filter+'open', 'height', {
			duration: 600, 
			transition: Fx.Transitions.Cubic.easeOut
		});
		FX_filter_slide.start(1,cs_height);
		FX_filter_innerslide = new Fx.Style('filter'+cs_filter+'openholder', 'margin-top', {
			duration: 600, 
			transition: Fx.Transitions.Cubic.easeOut
		});
		FX_filter_innerslide.start(-cs_height,0);
		cs_filteropen[cs_filter] = true;
	}
}

// close filter dropdowns
function cs_closefilter(cs_filter) {
	// find all options for filter
	cs_options = $$('*').filterByClass('filter'+cs_filter+'option');
	cs_options_open = 0;
	for (csa=0;csa<cs_options.length;csa++) {
		if (cs_options[csa].getStyle('display') == 'block') {
			cs_options_open++;
		}
	}	
	cs_height = 22 * cs_options_open + 2;

	FX_filter_slide = new Fx.Style('filter'+cs_filter+'open', 'height', {
		duration: 600, 
		fps: 12,
		transition: Fx.Transitions.Cubic.easeOut
	});
	FX_filter_slide.start(cs_height,1);
	FX_filter_innerslide = new Fx.Style('filter'+cs_filter+'openholder', 'margin-top', {
		duration: 600, 
		fps: 12,
		transition: Fx.Transitions.Cubic.easeOut
	});
	FX_filter_innerslide.start(0,-cs_height);
	cs_filteropen[cs_filter] = false;
}


// only on page load, go through list of all items in the page and disable all checkboxes if there are not items with that class
function cs_display_available_checkboxes() {
	cs_items = $$('*').filterByClass('csproduct');
	// create a list of all classes that exist within the items in the page that are display:block
	cs_displayed_list = new Array();
	for (csa=0;csa<cs_items.length;csa++) {
//		cs_classes = cs_items[csa].getProperties('class');
// vista issue:		cs_classes_set = cs_classes.class.split(' ');

		cs_classes = cs_items[csa].getProperty('class');
		cs_classes_set = cs_classes.split(' ');

		for (csb=0;csb<cs_classes_set.length;csb++) {
			cs_displayed_list[cs_classes_set[csb]] = 'yes';
		}
	}
	// see if any of the checkboxes has to be disabled since none of the current items has the necessary class
	if (cs_displayed_list['att-diyfree'] == null) {
		$('check-att-diyfree').getElement('.check').setStyle('margin-top','-21px');
		$('check-att-diyfree').getElement('div[class=checkboxinnertext]').setStyle('color','#cccccc');
		cs_checkstatus['left1'] = 'off';
	} else {
		$('check-att-diyfree').getElement('.check').setStyle('margin-top','-43px');
		$('check-att-diyfree').getElement('div[class=checkboxinnertext]').setStyle('color','#ffffff');
		cs_checkstatus['left1'] = 'empty';
	}
	if (cs_displayed_list['att-diypay'] == null) {
		$('check-att-diypay').getElement('.check').setStyle('margin-top','-21px');
		$('check-att-diypay').getElement('div[class=checkboxinnertext]').setStyle('color','#cccccc');
		cs_checkstatus['left2'] = 'off';
	} else {
		$('check-att-diypay').getElement('.check').setStyle('margin-top','-43px');
		$('check-att-diypay').getElement('div[class=checkboxinnertext]').setStyle('color','#ffffff');
		cs_checkstatus['left2'] = 'empty';
	}
	if (cs_displayed_list['att-custompay'] == null) {
		$('check-att-custompay').getElement('.check').setStyle('margin-top','-21px');
		$('check-att-custompay').getElement('div[class=checkboxinnertext]').setStyle('color','#cccccc');
		cs_checkstatus['left3'] = 'off';
	} else {
		$('check-att-custompay').getElement('.check').setStyle('margin-top','-43px');
		$('check-att-custompay').getElement('div[class=checkboxinnertext]').setStyle('color','#ffffff');
		cs_checkstatus['left3'] = 'empty';
	}
}


// go through list of all items in the page and enable only filter options that do currently not appear on the page
function cs_display_available_filters() {
	if (cs_pagestatus == 'product') {
		cs_items = $$('*').filterByClass('csproduct');
	} else {
		cs_items = $$('*').filterByClass('cscategory');
	}

	// create a list of all classes that exist within the items in the page that are display:none (those are the only ones the user will be able to pick)
	cs_displayed_list = new Array();
	cs_displayed_list_counter = 0;
	for (csa=0;csa<cs_items.length;csa++) {
		if (cs_items[csa].getStyle('display') == 'none') {
			cs_displayed_list_counter++;
			cs_classes = cs_items[csa].getProperty('class');
			cs_classes_set = cs_classes.split(' ');
			for (csb=0;csb<cs_classes_set.length;csb++) {
				cs_displayed_list[cs_classes_set[csb]] = 'yes';
			}
		}
	}

	// if no items are display:none, loop through all items with display:block and show all possible filter options
	if (cs_displayed_list_counter == 0) {
		for (csa=0;csa<cs_items.length;csa++) {
			if (cs_items[csa].getStyle('display') == 'block') {
				cs_classes = cs_items[csa].getProperty('class');
				cs_classes_set = cs_classes.split(' ');
				for (csb=0;csb<cs_classes_set.length;csb++) {
					cs_displayed_list[cs_classes_set[csb]] = 'yes';
				}
			}
		}
	}
	
	// disable dropdown options if they are currently shown
	cs_dropdownoptions = $$('*').filterByClass('filterrightoption');
	for (csa=0;csa<cs_dropdownoptions.length;csa++) {
		csonclick = cs_dropdownoptions[csa].getProperty('onclick');

		if (csonclick.indexOf('att') != -1) {
			csoptionclass = csonclick.substring(csonclick.indexOf('att'),csonclick.length);
			csoptionclass = csoptionclass.substring(0,csoptionclass.indexOf("'"));
			if (cs_displayed_list[csoptionclass] == null) {
				cs_dropdownoptions[csa].setStyle('display','none');
			} else {
				cs_dropdownoptions[csa].setStyle('display','block');
			}
		}
	}
}

var cs_lastfiltercall = '';

function cs_filter_catalog(cs_filterclass,cs_filter,cs_option_name) {
	
	cs_disabled = false;

	// don't allow right filter to run if the request is identical to the last one
	cs_newcall = cs_filterclass+':'+cs_filter+':'+cs_option_name;
	if (cs_lastfiltercall == cs_newcall  &&  cs_filter == 'right') {
		cs_disabled = true;
	} else {
		cs_lastfiltercall = cs_newcall;
	}

	// don't filter anything if the checkbox was disabled
	if (cs_filter.indexOf('left') != -1) {
		cs_alwaysclose_checkinfo();
		if (cs_checkstatus[cs_filter] == 'off') {
			cs_disabled = true;
		}
	}

	if (!cs_disabled) {
		clearTimeout(cs_filtertimer);
	
		// hide product table
		$('cscatalogouterholder').setStyle('opacity','0');
	
		// hide arrows
		$('csmainarrowleftoff').setStyle('visibility','hidden');
		$('csmainarrowlefthit').setStyle('visibility','hidden');
		$('csmainarrowrightoff').setStyle('visibility','hidden');
		$('csmainarrowrighthit').setStyle('visibility','hidden');
	
		cs_visual_products = 0;
	
		// set new selected name for dropdown
		if (cs_filter == 'right') {
			cs_filtervalue[cs_filter] = cs_filterclass;
			$('filter'+cs_filter+'closed').setHTML(cs_option_name);
		}

		// show new checkbox status
		if (cs_filter.indexOf('left') != -1) {
			if (cs_checkstatus[cs_filter] == 'empty') {
				cs_checkvalue[cs_filter] = cs_filterclass;
				cs_checkstatus[cs_filter] = 'checked';
				$('check-'+cs_filterclass).getElement('.check').setStyle('margin-top','-63px');
			} else {
				cs_checkvalue[cs_filter] = '';
				cs_checkstatus[cs_filter] = 'empty';
				$('check-'+cs_filterclass).getElement('.check').setStyle('margin-top','-43px');
			}
		}
	

		// collect all class attributes to be filtered by
		cscheckset = new Array();														// array of all allowable classes for this filter pass
		cscheckset[0] = '';																// default value if none of the checkboxes or dropdown options has been selected
		csy = 0;

		// checkboxes
		for (csx=1;csx<4;csx++) {
			if (cs_checkstatus['left'+csx] == 'checked') {
				cscheckset[csy++] = cs_checkvalue['left'+csx];
			}
		}

		// dropdown
		csdropdownvalue = '';
		if (cs_filtervalue['right'] != '') {
			csdropdownvalue = cs_filtervalue['right'];
		}

		// parameter filter call
		if (cs_filter.indexOf('parameter') != -1) {
			csdropdownvalue = cs_filterclass;
		}


		cs_products = $$('*').filterByClass('csproduct');
	
		cs_special_status = false;
		for (csa=0;csa<cs_products.length;csa++) {

			cs_filtertemp = false;													// true if filter hits
			if (cscheckset[0] != '') {
				for (a=0;a<cscheckset.length;a++) {
					if (cs_products[csa].hasClass(cscheckset[a])) {
						cs_filtertemp = true;
					}
				}
			} else {																// always true if none of the checkboxes was selected
				cs_filtertemp = true;
			}
			
			if (csdropdownvalue != '') {
				if (!cs_products[csa].hasClass(csdropdownvalue)) {
					cs_filtertemp = false;
				}
			}

			if (cs_filtertemp) {													// if product is allowed for this item, dispaly item
				cs_products[csa].setStyle('display','block');
				//bhaley adding code to load these images
				if(cs_product_images[csa]&&cs_product_images[csa].className!=""&&cs_product_images[csa].className!="imgLoaded"){
					var tempClass=cs_product_images[csa].className;
					cs_product_images[csa].setAttribute('src',tempClass);
					cs_product_images[csa].className="imgLoaded";
				}
				cs_visual_products++;
			} else {
				cs_products[csa].setStyle('display','none');
			}
		}
		// determine presence of special image, which is the size of 6 product icons
		cs_specialproduct = $$('*').filterByClass('csspecialimage');
		if (cs_specialproduct.length > 0) {
			if (cs_specialproduct[0].getParent().getStyle('display') == 'block') {
				cs_special_status = true;
				cs_visual_products = cs_visual_products + 3;					// special item images are 3x2 standard item sizes, add difference
			}
		}
		cs_product_remainder = cs_products_virtual_length%10;
		if(cs_product_remainder>0){
			cs_width_subtract=(Math.ceil(cs_product_remainder/2)*157);	
		} else {
			cs_width_subtract=0;	
		}
		if(cs_product_remainder>5){
			cs_product_remainder=5;	
		}
		cs_width_products = Math.ceil((cs_visual_products)/2);
		if(cs_width_products*157 <= 785){
			$('cscataloginnerholder').setStyle('width',785+'px');
		} else {
			$('cscataloginnerholder').setStyle('width',((cs_width_products*157)+(cs_product_remainder*157)-cs_width_subtract)+'px');
		}
		cs_main_page_max = Math.floor((cs_visual_products-1)/10);
		if (cs_main_page_max > 0) {
			$('csmainarrowrightoff').setStyle('visibility','visible');
			$('csmainarrowrightoff').setStyle('opacity','1');
			$('csmainarrowrighthit').setStyle('visibility','visible');
		}
	
		// reset catalog all the way to the left
		cs_main_page = 0;
		$('cscataloginnerholder').setStyle('margin-left','0');
		
		cs_set_pagelinks();
	
		if (cs_filteropen['right']) {
			cs_closefilter('right');
		}
		
		cs_display_catalog();
		cs_display_available_filters();
	}
}


var cs_mouse_on_filter = false;
var cs_filtertimer;


function cs_filter_ontimer(cs_filter) {
	clearTimeout(cs_filtertimer);
	cs_mouse_on_filter = true;
}

function cs_filter_offtimer(cs_filter) {
	cs_mouse_on_filter = false;
	cs_filtertimer = setTimeout(cs_filter_timerover_close,2000);
}

function cs_filter_timerover_close() {
	if (!cs_mouse_on_filter) {
		if (cs_filteropen['left']) {
			cs_closefilter('left');
		}
		if (cs_filteropen['right']) {
			cs_closefilter('right');
		}
	}
}


function cs_filter_pop_open(cs_catobj) {
	$('cspopholder').setHTML($('cscategorymeta'+cs_catobj).innerHTML);
	// how tall is the new popup?
	cs_pop_items = $('cscategorymeta'+cs_catobj).innerHTML.split('filterpopoption');
	cs_pop_height = 107+(17*(cs_pop_items.length-1));
	// place it and show it
	$('cspopholder').setStyle('top',(200-(cs_pop_height/2))+'px');
	$('cspopback').setStyle('display','block');
	$('cspopback').setStyle('opacity',0);
	$('cspopback').setStyle('visibility','visible');
	$('cspopholder').setStyle('display','block');
	$('cspopholder').setStyle('opacity',0);
	$('cspopholder').setStyle('visibility','visible');
	FX_popfilterreveal = new Fx.Style('cspopback', 'opacity', {
		duration: 2000,
		fps: 12,
		transition: Fx.Transitions.Cubic.easeOut
	});
	if (ie4) {
		popfilteropacity = 0.5;
	} else {
		popfilteropacity = 1;
	}
	FX_popfilterreveal.start(0,popfilteropacity);
	FX_popfilterholderreveal = new Fx.Style('cspopholder', 'opacity', {
		duration: 1000,
		fps: 12,
		transition: Fx.Transitions.Cubic.easeOut
	});
	FX_popfilterholderreveal.start(0,1);
}

function cs_filter_pop_close(pn) {
	$('cspopback').setStyle('visibility','hidden');
	$('cspopholder').setStyle('visibility','hidden');
	$('cspopback').setStyle('display','none');
	$('cspopholder').setStyle('display','none');
}

var cs_filter_pop_object;
var cs_filter_pop_destination;
function cs_filter_pop_clickdisplay(obj) {
//	if (cs_filter_pop_object != null) {
//		$(cs_filter_pop_object).setStyle('background-image','url(media/images/chrome/mainarea/pop_line_off.png)');
//	}
//	cs_filter_pop_object = obj;
//	$(obj).setStyle('background-image','url(media/images/chrome/mainarea/pop_line_on.png)');
}

function cs_filter_pop_click(newurl) {
	cs_filter_pop_destination = newurl;
}

function cs_filter_pop_submit() {
	if (cs_filter_pop_destination != null) {
		window.location.href = cs_filter_pop_destination;
	}
}
function callOmn(cs_partner,cs_cat,cs_subcat,cs_name){
	s_sendAnalyticsEventKeyInt('','ipg20:smbpt:ext-link:'+cs_partner+':'+cs_cat+':'+cs_subcat+':'+cs_name,'smbpt|lextcont|l25|'+cs_partner+'|'+cs_cat+'|'+cs_subcat+'|'+cs_name,'event30');
	s_setJumpid('re_r11400/us/en/smb/ipg/ipg20_smbpt_ext-link_'+cs_partner+'_'+cs_cat+'_'+cs_subcat+'_'+cs_name);
}

function setupPreviews(){
	var tempImage=$("preview").src;
	$("dataarea").getElements('div.datarow').addEvent('mouseover',function(event){
		$("preview").src="media/images/projects/"+(this.id);
	});
	$("dataarea").addEvent('mouseout',function(event){
		$("preview").src=tempImage;										   
	});
}