//Initialization code
document.write('<style type="text/css">.unique {display: none;}</style>');

// Spotlight script
addEvent(window, "load" , function () 
{
	var sTimer;
	var spotlight_nav = document.createElement('div');
	spotlight_nav.id = "spotlight_nav";
	spotlight_nav.appendChild(document.createElement('ul'));

	var banners = document.getElementById('banners');
    if( ! banners) {
        return false;
    }
	var banners_divs = banners.getElementsByTagName('div');

	var banner_array = [];

	for (b = 0,l=banners_divs.length; b < l; b++) 
	{
		banner_array.push(banners_divs[b]);
	}

	banner_array.sort(function() 
	{
		return (Math.round(Math.random())-0.5); 
	})

	for (b in banner_array) 
	{
		if(b=="remove"|| b=="indexOf")
		{
			continue;
		}	
		if (b != 0) {
			banner_array[b].className = "spotlight_image spotlight_image_hidden";
		} else {
			banner_array[b].className = "spotlight_image";
		}
		
		var b_a = document.createElement('a');
		b_a.href = "#" + banner_array[b].id;
		
		addEvent(b_a, "click", function(event) {
			event.preventDefault ? event.preventDefault() : event.returnValue = false;
			var new_active_banner = document.getElementById(this.href.substr(this.href.lastIndexOf('#')+1));
			for (ab in banner_array) {
				if (!banner_array[ab].className.match('spotlight_image_hidden')) {
					banner_array[ab].className += ' spotlight_image_hidden';
					break;
				}
			}
			new_active_banner.className = new_active_banner.className.replace('spotlight_image_hidden', '');
			clearInterval(sTimer);
			var li_loop = spotlight_nav.getElementsByTagName('li');
			for (b = 0,l=li_loop.length; b < l; b++) {			
				if (li_loop[b].className.match('active_indicator')) {
					li_loop[b].className = li_loop[b].className.replace('active_indicator','');
					break;
				}
			}
			this.parentNode.className += " active_indicator";
		})
	
		var b_li = document.createElement('li');
		b_li.className = "indicator" + (b==0?" active_indicator": "");
		b_li.appendChild(b_a);
	
		spotlight_nav.getElementsByTagName('ul')[0].appendChild(b_li);
	}
	

	if (banner_array.length > 1) 
	{
		banners.appendChild(spotlight_nav);
		sTimer = setInterval(function() {
			var li_loop = spotlight_nav.getElementsByTagName('li'), banner_next;
			for (b = 0,l=li_loop.length; b < l; b++) {
				var li_a = li_loop[b].getElementsByTagName('a')[0];
				if (li_loop[b].className.match('active_indicator')) {
					li_loop[b].className = li_loop[b].className.replace('active_indicator','');
					var banner_active = document.getElementById(li_a.href.substr(li_a.href.lastIndexOf('#')+1));
					banner_active.className += " spotlight_image_hidden";
					if (b == (l - 1)) {
						li_loop[0].className += " active_indicator";
						var first_li = li_loop[0].getElementsByTagName('a')[0];
						banner_next = document.getElementById(first_li.href.substr(first_li.href.lastIndexOf('#')+1));
						
					} else {
						li_loop[b+1].className += " active_indicator";						
						var next_li = li_loop[b+1].getElementsByTagName('a')[0];
						banner_next = document.getElementById(next_li.href.substr(next_li.href.lastIndexOf('#')+1));
					}
				banner_next.className = banner_next.className.replace(' spotlight_image_hidden', '');
				break;
				}
			}
		}, 5000);  // Change this number to adjust the banner rotation speed.  Each 1000 = 1 second.
	}
})

function addEvent( obj, type, fn ) {
  if ( obj!=null && obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else 
	  if(obj!=null)
		{obj.addEventListener( type, fn, false );}
}

function saveRecentlyViewed( title, path ) {
    var cookieContents = getCookie( 'recentlyViewed' );
    var currentPageEntry = "title:" + title + ",path:" + path;
    var newContents = "";
    if( cookieContents != '' ) {
        var tokens = cookieContents.split( '|~|' );
        var i; var upperBound;
        ( tokens.length > 5 ) ? upperBound = 6 : upperBound = tokens.length - 1;
        for ( i = 0; i < upperBound; i++ ) {
            if ( tokens[i] ) {
                if ( tokens[i] != currentPageEntry ) {
                    newContents += tokens[i] + '|~|';
                }
            }
        }
        newContents = currentPageEntry + '|~|' + newContents;
    }
    else {
    	newContents = currentPageEntry + '|~|';
    }
    setCookie( "recentlyViewed", encodeURIComponent(newContents), 365 );
}

function getCookie(cookieName) {
    var cookie = cookieName + "=";
    var documentCookie = document.cookie;
    if (documentCookie == null) {
        return "";
    }
    if (documentCookie.length > 0) {
        var startIndex = documentCookie.indexOf(cookie);
        if (startIndex != -1) {
            startIndex += cookie.length;
            var endIndex = documentCookie.indexOf(";", startIndex);
            if (endIndex == -1) {
                endIndex = documentCookie.length;
            }          
         return decodeURIComponent(documentCookie.substring(startIndex, endIndex))
        }
    }
    return "";
}

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=/";
}

if (!CP) var CP = {};

if (!CP.listing) CP.listing = {};
CP.listing = function( path, pathOptions, display, tag, unique ) {
	this.newStart = 0;
	this.totalRecords = 0;
	this.pageRecords = 0;
	this.pageNumber = 1;
	this.path = path;
	this.display = display;
	this.pathOptions = pathOptions;
	this.tag = tag;
	this.unique = unique;
	this.uniqueID = "#childIndex" + unique + " ";
	this.recordsPerPage = 12;
	this.numberPages = 1;
	this.zeroControls();
	return true;
};

CP.listing.prototype = {
	recordCounts: function() {
		$( this.uniqueID + '.pagerTotal' ).html( this.totalRecords );
		$( this.uniqueID + '.pagerBegin' ).html( this.newStart + 1 );
		$( this.uniqueID + '.pagerEnd' ).html( this.newStart + this.pageRecords );
		$( this.uniqueID + '.results-displayed' ).show();
		if ( this.totalRecords > this.recordsPerPage ) {
			this.numberPages = Math.ceil( this.totalRecords / this.recordsPerPage );
			this.controls();
		};
	},
	controls: function() {
		var self = this;
		for ( i = 2; i <= this.numberPages; i++ ) {
			$( self.uniqueID + '#pagerPageNumbers' ).append( '<li><a class="pagerPageLink" rel="' + i + '" href="#">' + i + '</a></li>' );
		};
		$( '.pagerPageLink' ).click( function( event ) { event.preventDefault(); self.jumpToPage( event ); } );
		$( '.pagerNext' ).click( function( event ) { event.preventDefault(); self.nextPage( event ); } );
		$( '.pagerPrev' ).click( function( event ) { event.preventDefault(); self.previousPage( event ); } );
		$( this.uniqueID + '.pagination-bottom .results-displayed' ).hide();
		$( this.uniqueID + '.results-pagination' ).show();
		$( '.pagerSeeAll' ).click( function( event ) { event.preventDefault(); self.doSeeAllListing( event ); } );
		$( '.pagerSeeAll' ).show();
		this.pageNumber > 1 ? $( '.pagerPrev' ).removeClass( 'disabled' ) : $( '.pagerPrev' ).addClass( 'disabled' );
		this.pageNumber == this.numberPages ? $( '.pagerNext' ).addClass( 'disabled' ) : $( '.pagerNext' ).removeClass( 'disabled' );
		$( '#pagerPageNumbers li a' ).each( function( index ) {
			this.rel == self.pageNumber ? $( this ).addClass( 'disabled' ) : $( this ).removeClass( 'disabled' );
		});	
	},
	zeroControls: function() {
		$( this.uniqueID + '#pagerPageNumbers' ).html( '<li><a class="pagerPageLink disabled" rel="1" href="#">1</a></li>' );
		$( this.uniqueID + '.results-pagination' ).hide();
		$( this.uniqueID + '.pagerPageLink' ).unbind();
		$( this.uniqueID + '.pagerNext' ).unbind();
		$( this.uniqueID + '.pagerPrev' ).unbind();
		$( this.uniqueID + '.pagerSeeAll' ).unbind();
	},
	updateControls: function() {
		var self = this;
		$( this.uniqueID + '.pagerTotal' ).html( this.totalRecords );
		$( this.uniqueID + '.pagerBegin' ).html( this.newStart + 1 );
		$( this.uniqueID + '.pagerEnd' ).html( this.newStart + this.pageRecords );
		this.pageNumber > 1 ? $( '.pagerPrev' ).removeClass( 'disabled' ) : $( '.pagerPrev' ).addClass( 'disabled' );
		this.pageNumber == this.numberPages ? $( '.pagerNext' ).addClass( 'disabled' ) : $( '.pagerNext' ).removeClass( 'disabled' );
		$( this.uniqueID + '#pagerPageNumbers li a' ).each( function( index ) {
			this.rel == self.pageNumber ? $( this ).addClass( 'disabled' ) : $( this ).removeClass( 'disabled' );
		});	
	},
	doPageListing: function() {
		var self = this;
		( this.pageNumber > 1 ) ? this.newStart = ( this.pageNumber - 1 ) * this.recordsPerPage : this.newStart = 0;
		this.pathOptions = '.pg.' + this.newStart + '.' + this.recordsPerPage + '.prod';
		$.getJSON( self.path + self.pathOptions, function( data ) {
			if ( data.length > 0 && data[0].results != null ) {
				self.totalRecords = data[0].results;
				data.shift();
				self.pageRecords = data.length;
				var out = CP.pager.BEGINROW;
				$.each( data, function( key, value ) {
					out += CP.pager.buildHit( value, self.display );
					if ( (key+1)%3 == 0 ) {
						out += CP.pager.ENDROW + CP.pager.BEGINROW; //TODO: Don't start a new row if on the last
					}
				} );
				out += CP.pager.ENDROW; //TODO: if it ends on a completed row, don't close again
				$( self.uniqueID + '.folderContents' ).html( out );
				self.updateControls();
			}
		});
	},
	doSeeAllListing: function() {
		var self = this;
		$( this.uniqueID + '.childindex' ).css( 'opacity', 0.3 );
		this.pathOptions = '.prod';
		$.getJSON( self.path + self.pathOptions, function( data ) {
			if ( data.length > 0 ) {
				self.totalRecords = data.length;
				self.pageRecords = data.length;
				self.newStart = 0;
				self.recordCounts();
				out = CP.pager.BEGINROW;
				$.each( data, function( key, value ) {
					out += CP.pager.buildHit( value, self.display );
					if ( ( key + 1 ) % 3 == 0 ) {
						out += CP.pager.ENDROW + CP.pager.BEGINROW; //TODO: Don't start a new row if on the last
					}
				} );
				out += CP.pager.ENDROW; //TODO: if it ends on a completed row, don't close again
				$( self.uniqueID + '.folderContents' ).html( out );
				$( self.uniqueID + '.pagerSeeAll' ).hide();
				$( self.uniqueID + '.results-pagination' ).hide();
				$( self.uniqueID + '.childindex' ).css( 'opacity', 1 );
			}
		});
	},
	doCategoryListing: function() {
		var uniqueID = this.uniqueID;
		var unique = this.unique;
		var display = this.display;
		$.getJSON( this.path + this.pathOptions, function( data ) {
			$( uniqueID + '.pagerTotal' ).html( data.length );
			if ( data.length > 0 ) {
				$( uniqueID + '.pagerActiveRange').show();
				$( uniqueID + '.pagerBegin').html( '1' );
				$( uniqueID + '.pagerEnd').html( data.length );
			}
			out = CP.pager.BEGINROW;
			$.each( data, function( key, value ) {
				out += CP.pager.buildCategoryHit( value, display );
				if ( ( key + 1 ) % 3 == 0 ) {
					out += CP.pager.ENDROW + CP.pager.BEGINROW ; //TODO: Don't start a new row if on the last
				}
			} );
			out += CP.pager.ENDROW; //TODO: if it ends on a completed row, don't close again
			$( uniqueID + '.folderContents' ).html( out );
			$( uniqueID + '.productLine').click( function( event ) { event.preventDefault(); eval( 'listing' + unique + '.expandProductLine( event )' ); } ); 
		});
	},
	expandProductLine: function( event ) {
		var self = this;
		var prodLinePath = event.currentTarget.rel  + "." + this.tag + ".prod";
		if(event.currentTarget.rel.indexOf(".bysolutionsprod")!=-1)
			{ prodLinePath = event.currentTarget.rel; }
		var customSolution = ( this.tag ).substring( 0, ( this.tag ).indexOf( '__' ) ) + '.' + ( this.tag ).substring( ( this.tag ).indexOf( '__' ) + 2 ) ;
		var uniqueID = this.uniqueID;
		var unique = this.unique;
		$.getJSON( prodLinePath, function( data ) {
			$( uniqueID + '.pagerTotal' ).html( data.length );
			if ( data.length > 0 ) {
				$( uniqueID + '.pagerActiveRange').show();
				$( uniqueID + '.pagerBegin').html( '1' );
				$( uniqueID + '.pagerEnd').html( data.length );
			}
			var out = CP.pager.BEGINROW;
			$.each( data, function( key, value ) {
				out += '<div class="col">';
				out += '<div class="indexCellContents">';
				out += '<a href=\'' + value.path + '.' + customSolution + '.html\'><img src="' + value.mediumThumbnail + '" width="110" height="110" alt=""></a>';
				out += '<h3><a href=\'' + value.path + '.' + customSolution + '.html\'>' + value.title + '</a></h3>';
				out += '' + value.description + '';
				out += '</div><!-- /indexCellContents -->';
				out += '</div><!-- /col -->';
				if ( ( key + 1 ) % 3 == 0 ) {
					out += CP.pager.ENDROW + CP.pager.BEGINROW ; //TODO: Don't start a new row if on the last
				}
			} );
			out += CP.pager.ENDROW; //TODO: if it ends on a completed row, don't close again
			$( uniqueID + '.folderContents' ).html( out );
			$( uniqueID + '#pagerBackProductLine' ).click( function( event ) { event.preventDefault(); eval( 'listing' + unique + '.backToProductLines( event )' ); } );
			$( uniqueID + '#pagerBackProductLine' ).show();
		});
	},
	backToProductLines: function() {
		$( this.uniqueID + '#pagerBackProductLine' ).hide();
		this.doCategoryListing();
	},
	nextPage: function( event ) {
		this.pageNumber++;
		this.doPageListing();
	},
	previousPage: function( event ) {
		this.pageNumber--;
		this.doPageListing();
	},
	jumpToPage: function( event ) {
		this.pageNumber = event.currentTarget.rel; 
		this.doPageListing();
	}	
};

if (!CP.pager) CP.pager = {};
CP.pager = {
	"BEGINROW" : "<div class='product-row clearfix'><div class='grid3'>",
	"ENDROW": "</div><!-- /grid3 --></div><!-- /product-row -->",
	"BEGINCOLUMN" : "<div class='col'><div class='thumbnails'>",
	"ENDCOLUMN": "</div></div><!-- /col -->",
	"BEGINLONGCOLUMN" : "<ul class='col'>",
	"ENDLONGCOLUMN": "</ul><!-- /col -->"
};
CP.pager.buildHit = function( value, display ) {
	switch ( display ) {
	case 0:
		out = '<div class="col"><div class="indexCellContents">' + '<a href=\'' + value.path + '.html\'>' + value.title + '</a></div><!-- /indexCellContents --></div><!-- /col -->';
		break;
	case 1:
		out = '<div class="col">';
		out += '<div class="indexCellContents">';
		out += '<a href=\'' + value.path + '.html\'><img src="' + value.mediumThumbnail + '" width="110" height="110" alt=""></a>';
		out += '<h3><a href=\'' + value.path + '.html\'>' + value.title + '</a></h3>';
		out += '' + value.description + '';
		out += '</div><!-- /indexCellContents -->';
		out += '</div><!-- /col -->';
		break;
	case 2:
		out = '<div class="col">';
		out += '<div class="indexCellContents">';
		out += '<h3><a href=\'' + value.path + '.html\'>' + value.title + '</a></h3>';
		out += '' + value.description + '';
		out += '</div><!-- /indexCellContents -->';
		out += '</div><!-- /col -->';
		break;
	default:
		out = '<div class="col"><div class="indexCellContents">' + '<a href=\'' + value.path + '.html\'>' + value.title + '</a></div><!-- /indexCellContents --></div><!-- /col -->';
	}
	return out;
};
CP.pager.buildFlyoutThumbHit = function( value ) {
	var out = '<div>';
	out += '<a href=\'' + value.path + '.html\'><img src="' + value.smallThumbnail + '" width="43" height="43" alt=""></a>';
	out += '<p><a href=\'' + value.path + '.html\'>' + value.title + '</a></p>';
	out += '</div>';
	return out;
};
CP.pager.buildFlyoutHit = function( value ) {
	var out = '<li><a class=\'has-arrow\' href=\'' + value.path + '.html\'>' + value.title + '</a></li>';
	return out;
};
CP.pager.buildCategoryHit = function( value, display ) {
	switch ( display ) {
	case 0:
		out = '<div class="col"><div class="indexCellContents">' + '<a href=\'' + value.path + '.html\'>' + value.title + '</a></div><!-- /indexCellContents --></div><!-- /col -->';
		break;
	case 1:
		out = '<div class="col">';
		out += '<div class="indexCellContents">';
		out += '<a class="productLine" rel="' + value.path + '" href="' + value.path + '"><img src="' + value.mediumThumbnail + '" width="110" height="110" alt=""></a>';
		out += '<h3><a class="productLine" rel="' + value.path + '" href=\'' + value.path + '.html\'>' + value.title + '</a> (' + value.count + ')</h3>';
		out += '' + value.description + '';
		out += '</div><!-- /indexCellContents -->';
		out += '</div><!-- /col -->';
		break;
	case 2:
		out = '<div class="col">';
		out += '<div class="indexCellContents">';
		out += '<h3><a class="productLine" rel="' + value.path + '" href=\'' + value.path + '.html\'>' + value.title + '</a> (' + value.count + ')</h3>';
		out += '' + value.description + '';
		out += '</div><!-- /indexCellContents -->';
		out += '</div><!-- /col -->';
		break;
	default:
		out = '<div class="col"><div class="indexCellContents">' + '<a href=\'' + value.path + '.html\'>' + value.title + '</a></div><!-- /indexCellContents --></div><!-- /col -->';
	}
	return out;
};
CP.pager.brandPaginationJumpPage = function( event ) {
	var brandPage = event.currentTarget.rel;
	$('.brandPaginationPage').each( function( index ) {
		index + 1 == brandPage ? $( this ).show() : $( this ).hide();
	});
};

if (!CP.events) CP.events = {};
CP.events = {
		"c" : ""
};
CP.events.openEventDetail = function( eventPath ) {
    var eventDetailsDialog = new CQ.IframeDialog({
        title: "Event Details",
        height: 410,
        width: 425,
        buttons: [ CQ.Dialog.CANCEL ],
        cancelText: "OK"
    });
    eventPath = eventPath.replace( /\//g, "!") ;
    eventPath = eventPath.replace( /:/g, "__") ;
    eventDetailsDialog.loadContent( "/content/public/en.eventdetail." + eventPath + ".html" );
    eventDetailsDialog.show();
};


if ( !CP.newsListing ) CP.newsListing = {};

CP.newsListing = function() {
	this.pageNumber = 1;
	return true;
};

if ( !CP.newsPager ) CP.newsPager = {};

CP.newsPager = {
	"beginSet" : ""
};

CP.newsPager.initControls = function( totalRecords, recordsPerPage ) {
	if ( totalRecords > recordsPerPage ) {
		$('.newlist .pagination-bottom .results-displayed').hide();
	}
	else {
		$('.newlist .results-pagination').hide();
	}
};

if ( !CP.bline ) CP.bline = {};

CP.bline = {
		"windowName" : "blineApp",
		"windowOptions" : "width=850,height=500,scrollbars=yes"
};

CP.bline.open = function ( event ) {
	window.open( event.currentTarget.href, this.windowName, this.windowOptions );
};

// Bulleted List of Links: Styling
$( function () { $( '.productdetailstextimage ul li a' ).parent().parent().each( function() { $( this ).addClass( 'arrowBullet' ) }); });                                      
// too greedy? previously: $( '#product-tabs div.textimage ul li a' )                                     
$( function () { $( '#pri div.textimage ul li a' ).parent().parent().each( function() { $( this ).addClass( 'arrowBullet' ) }); });

$(document).ready(function () {
	  if ($.browser.msie == true && $.browser.version.slice(0,1) == "7") {
	    var zIndexNumber = 2000;
	    $('div').each(function() {
	      if (this.id != 'sec') {
	      $(this).css('zIndex', zIndexNumber);
	      zIndexNumber -= 10;
	      }
	    });
	  }
	});



