
/* Assign onclick event to all clickable tiles */

function findAllButtons()
{
    /* 
     * create array of all buttons and assign onclick events to each one
     * matching class name 'tile-button' 
    */
    var tempButtons = document.getElementsByTagName('a');
    var tempName;
	
	// find links if in area tags
	
    for (var i = 0; i < tempButtons.length; i++)
    {
        // if not ie
        if (window.innerWidth)
        {
			tempName = tempButtons[i].getAttribute('class');
        }
        // if ie
        else
        {
           	tempName = tempButtons[i].getAttribute('className');
        }
      
        if (tempName != null && tempName.match(/target/i))
        {
            // assign slideshow function to each button
            tempButtons[i].onclick = makeRequest;
        }
    }
}


/* Handles xml slideshow requests */

function makeRequest()
{
	// alert('request initiated');
	// use an object to hold tile profile and color	
	var tile = new Object;
	
	// get the tile's color
	tile.color = this.getAttribute('alt');
	
	// search through string and transform ascii code to a character
	tile.color = tile.color.replace(/é/, 'e');
	
	tile.profile = null;
	
	// loop through radiogroup and grab tile profile on just the color-selections page
	if (document.forms[0])
	{
		for (i=0;i<document.forms[0].tileProfile.length;i++) 
		{
			if (document.forms[0].tileProfile[i].checked) {
				tile.profile = (document.forms[0].tileProfile[i].id);
				//alert('tile-profile: ' + tile.profile + ' tile.color: ' + tile.color);
			}
		}
	}
	// get values for pre-blends and custom blends
	else
	{
		tile.color = this.getAttribute('alt');
		tile.profile = this.getAttribute('profile');
		// alert('tile-profile: ' + tile.profile + ' tile.color: ' + tile.color);
	}
	// remove special characters from new tiles

	if (tile.color.substring(0,2) == '*_')
	{
		// get the last character in the color
		lastChar = tile.color.lastIndexOf(tile.color.charAt(tile.color.length));
		// remove the prefix 
		tile.color = tile.color.substring(2, lastChar);
	}
	
	tile.filename = tile.profile + '-' + tile.color;
	
	// create a new xmlhttprequest object
	if (window.XMLHttpRequest)
	{
		var request = new XMLHttpRequest();
	}
	
	// code for IE
	else if (window.ActiveXObject)
	{
		request = new ActiveXObject("Microsoft.XMLHTTP")
	}
	request.onreadystatechange = function()
	{
		if (request.readyState == 4)
		{
			if (request.status == 200)
			{
				//alert('path: ' + request.responseText);
				if (request.responseText == '')
				{
					/* original function, need better usability
					if ( $('#notice').css('display') == 'none')
					{
						$('#notice').slideDown(); // show error message on this 
						$('#notice-handle').html('Click to Close');
					}
					*/
					// create an overlay
					$('body').append('<div id="overlay2"></div>');
					
					// set the height
					if (window.innerWidth)
					{
						totalY = window.scrollMaxY + window.innerHeight;
					}
					else 
					{
						totalY = document.body.scrollHeight + document.documentElement.scrollTop;	
					}
						
					$('#overlay2').css('height', totalY);
					
					$('#overlay2').bind('click',function()
					{
						// $(this).remove();	
						// $('#sorry').remove();
						
					});
					// $('#sorry').css('display','none');
					var appendSorry = '<div id="sorryHolder"></div>';
					var appendTheRest = '<div id="sorry"><p><img src="images/bg-sorry-logo.jpg" /><strong>We’re sorry</strong> but the tile you’re interested in does not currently have a slideshow. You can choose from the following list. Please come back soon as we’re adding new slideshows weekly.</p> \
					<hr /> \
					<div class="first"> \
					<h3>Spanish-S</h3> \
					<ul> \
					<li><a href="#" class="target2" path="/xml/spanish/amazon_green/spanish-amazon_green">Amazon Green</a></li> \
					<li><a href="#" class="target2" path="/xml/spanish/bay_blue/spanish-bay_blue">Bay Blue</a></li> \
					<li><a href="#" class="target2" path="/xml/spanish/cocoa/spanish-cocoa">Cocoa</a></li> \
					<li><a href="#" class="target2" path="/xml/spanish/coffee/spanish-coffee">Coffee</a></li> \
					<li><a href="#" class="target2" path="/xml/spanish/coffee_fume/spanish-coffee_fume">Coffee Fum&#233;</a></li> \
					<li><a href="#" class="target2" path="/xml/spanish/eucalyptus/spanish-eucalyptus">Eucalyptus</a></li> \
					<li><a href="#" class="target2" path="/xml/spanish/fusion_bronze/spanish-fusion_bronze">Fusion Bronze</a></li> \
					<li><a href="#" class="target2" path="/xml/spanish/galeras/spanish-galeras">Galeras</a></li> \
					<li><a href="#" class="target2" path="/xml/spanish/galeras_fume/spanish-galeras_fume">Galeras Fum&#233;</a></li> \
					</ul> \
					<ul> \
					<li><a href="#" class="target2" path="/xml/spanish/marlin/spanish-marlin">Marlin</a></li> \
					<li><a href="#" class="target2" path="/xml/spanish/fusion_patina/spanish-fusion_patina">Fusion Patina</a></li> \
					<li><a href="#" class="target2" path="/xml/spanish/peach_fume/spanish-peach_fume">Peach Fum&#233;</a></li> \
					<li><a href="#" class="target2" path="/xml/spanish/red/spanish-red">Red</a></li> \
					<li><a href="#" class="target2" path="/xml/spanish/savannah/spanish-savannah">Savannah</a></li> \
					<li><a href="#" class="target2" path="/xml/spanish/savannah_fume/spanish-savannah_fume">Savannah Fum&#233;</a></li> \
					<li><a href="#" class="target2" path="/xml/spanish/terra_nova/spanish-terra_nova">Terra Nova</a></li> \
					<li><a href="#" class="target2" path="/xml/spanish/ultramarine/spanish-ultramarine">Ultramarine</a></li> \
					</ul> \
					</div> \
					<div class="extra"><h3>Royal</h3> \
					<ul> \
					<li><a href="#" class="target2" path="/xml/royal/amazon_green/royal-amazon_green">Amazon Green</a></li> \
					<li><a href="#" class="target2" path="/xml/royal/peach/royal-peach">Peach</a></li> \
					<li><a href="#" class="target2" path="/xml/royal/red/royal-red">Red</a></li> \
					<li><a href="#" class="target2" path="/xml/royal/ultramarine/royal-ultramarine">Ultramarine</a></li> \
					</ul> \
					</div> \
					<div class="extra"><h3>Mission</h3> \
					<ul> \
					<li><a href="#" class="target2" path="/xml/mission/red/mission-red">Red</a></li> \
					</ul> \
					</div> \
					<p id="last-link"><a href="#" id="off-switch">Click here to close this window</a></p></div>';
					
					var mocha = '<div id="sorry"><p><img src="images/bg-sorry-logo.jpg" /><strong>We’re sorry</strong> but the tile you’re interested in does not currently have a slideshow. You can choose from the following list. Please come back soon as we’re adding new slideshows weekly.</p> \
					<hr /> \
					<div class="first"> \
					<h3>Pavers</h3> \
					<ul> \
					<li><a href="#" class="target2" path="/xml/pavers/dorado/pavers-dorado">El Dorado</a></li> \
					<li><a href="#" class="target2" path="/xml/pavers/salmon/pavers-salmon">Salmon</a></li> \
					</ul><p id="last-link"><a href="#" id="off-switch">Click here to close this window</a></p> \
					</div>';
					
					var ritz = '<div id="sorry"><p><img src="images/bg-sorry-logo.jpg" /><strong>We’re sorry</strong> but the tile you’re interested in does not currently have a slideshow. You can choose from the following list. Please come back soon as we’re adding new slideshows weekly.</p> \
					<hr /> \
					<div class="first" style="width: 250px;"> \
					<h3>Pre-blends &amp; Custom Blends</h3> \
					<ul style="width: 200px;"> \
					<li><a href="#" class="target2" path="/xml/pre-blends/cartagena/pre-blends-cartagena">Cartagena Blend</a></li> \
					<li><a href="#" class="target2" path="/xml/pre-blends/naples/pre-blends-naples">Naples Blend</a></li> \
					<li><a href="#" class="target2" path="/xml/pre-blends/st_augustine/pre-blends-st_augustine">St. Augustine Blend</a></li> \
					<li><a href="#" class="target2" path="/xml/pre-blends/tayrona/pre-blends-tayrona">Tayrona Blend</a></li> \
					<li><a href="#" class="target2" path="/xml/custom-blends/custom/custom-blends-custom">Spanish-S Custom Blends</a></li> \
					<li><a href="#" class="target2" path="/xml/custom-blends/custom-barrel/custom-blends-custom-barrel">Mission Barrel Custom Blends</a></li> \
					</ul><p id="last-link"><a href="#" id="off-switch">Click here to close this window</a></p> \
					</div>';
					
					// append the new element and center it in the window
					$('body').append(appendSorry);
					
					// set css for overlay
					
					//$('#sorry').css({ position: "absolute", zIndex: 200, top: 0 });
					var sorry = document.getElementById('sorryHolder');
					var sorryHeight = 500;
					var sorryWidth = 550;
					
					// if brower is not ie
					if (window.innerWidth)
					{
						var windowHeight = window.innerHeight;
						var windowWidth = window.innerWidth;
						// alert(window.pageYOffset);
						var centeredHeight = (windowHeight/2) - (sorryHeight/2) + window.pageYOffset;
						var centeredWidth = (windowWidth/2) - (sorryWidth/2) + window.pageXOffset;
					}
					
					// if browser is ie
					else 
					{
						var windowHeight = document.documentElement.clientHeight;
						var windowWidth = document.documentElement.clientWidth;
						
						var centeredHeight = (windowHeight/2) - (sorryHeight/2) + document.documentElement.scrollTop;
						var centeredWidth = (windowWidth/2) - (sorryWidth/2) + document.documentElement.scrollLeft;
					}
					
					sorry.style.marginTop = centeredHeight + 'px';
					sorry.style.marginLeft = centeredWidth + 'px';	
					sorry.style.display = 'block';
					var sPath = window.location.pathname;
					var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
					if(sPage == 'brick-pavers.php')
					{
						$('#sorryHolder').html(mocha);
						$('#off-switch').bind('click',function() 
						{
							$('#sorryHolder').remove();										   
						});
					}
					else if(sPage == 'blends.php')
					{
						$('#sorryHolder').html(ritz);
						$('#off-switch').bind('click',function() 
						{
							$('#sorryHolder').remove();										   
						});
					}
					else
					{
						$('#sorryHolder').html(appendTheRest);
					}
					// $('#sorry').css('display','block').fadeIn('slow');
					// show the slideshow
					// sorry.style.display = 'block';
					
					// plumbing for remove function call if neededd
					$('#sorry').bind('click', function()
					{ 
						// removal code there						
					});
					
					// remove overlay and sorry div
					$('#off-switch').bind('click',function() 
					{ 
						$(this).parent().parent().remove();
						$('#overlay2').remove();
						return false;
					});
					
					// set up request to load slideshows
					$('.target2').bind('click', function()
					{
						var path = $(this).attr('path');
						startSlideshow(path);
						return false;
					});
					
				
				}
				else
				{
					path = '/xml/' + request.responseText
					//alert(path);
					startSlideshow(path);
				}
			}
		}
	}
	request.open('POST','xml/xml_builder.php', true);
	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
	request.send('color=' + tile.color + '&profile=' + tile.profile + '&filename=' + tile.filename);
	return false;
}


/* Handles db deletion requests */

function deleteThisEntry(news_id) {
	if(confirm('Are you sure you want to delete this entry? Clicking Ok will delete the record forever and is irreversible.'))
	{
		window.location.href = 'news_delete.php?news_id='+news_id;
	}
	else
	{
		alert('The selected record has NOT been deleted.');
	}
}



/*-----------------------------------------------------------------------------
--------- JQUERY :: ON DOCUMENT READY -----------------------------------------
------------------------------------------------------------------------------*/


$jq(document).ready(function() {
	
	/* Controls the hide/show panel on the roofing_tiles.php */
	
	$jq('#hideProductInfo').click(function() {
		var src= $jq(this).children([0]).attr('src') 
		if ( src.match(/hide/gi) != null) {src = src.replace(/hide/gi,'show'); }
		else src = src.replace(/show/gi, 'hide');
		$jq(this).children([0]).attr('src', src);		
		$jq(this).prev().slideToggle();
		return false;
	});
	
	
	
	/* Controls behaviors on update news page */
	
	$jq('#news-update a').toggle(function()
	{
		$jq(this).next().slideDown();
		$jq(this).parent().css('background', '#efefef');
	},
	function()
	{
		$jq(this).next().slideUp();
		$jq(this).parent().css('background', '');
	});
	
	/* controls roofing_tiles.php slideshow */
	
	
	/* Adds rollover effect for ie6 on products selector page */
	
	$jq('.product-selector').hover(function()
	{
		$jq(this).css('background', '#efefef');
		$jq(this).css('cursor', 'pointer');
	}, function()
	{
		$jq(this).css('background', '');
		$jq(this).css('cursor', 'default');
	});
	
	/* IE6 Fix :: Turns images into functional links */
	
	$('#NAV-tiles').click(function() {
		window.location.href = '/tile-profiles.php';										
	});
	
	$('#NAV-accessories').click(function() {
		window.location.href = '/accessories.php';										
	});
	
	$('#NAV-pavers').click(function() {
		window.location.href = '/brick-pavers.php';										
	});
	
	
	// when dom loads hide all divs
	$jq('.question').next().addClass('hide');
	//$jq('.question').css('color', '#666');
	
	
	/* hides all answers to question on faq.php */
	
	// toggle divs open/closed	
	$jq('.question').toggle(function() {
		$jq(this).next().fadeIn();
		//$jq(this).css('color', '#000');
	}, function() {
		$jq(this).next().fadeOut();
		//$jq(this).css('color', '#666');
	});
	
	$jq('.question').mouseover(function() { /* IE6 FIX */
		$jq(this).css('cursor','pointer');
	});
	
	$jq('.question').mouseout(function() { /* IE6 FIX */
		$jq(this).css('cursor','default');
	});

	/* add tablestriping on faq.php */
/*
	$jq(".tile-weights tr").each(function(i){
		if(i%2 == 0)
		{
			$(this).addClass('even');
		}
	});
	*/
	
	
	
});

	var lastTarget = ''; // target div to hide/show
	var lastImage = ''; // last nav image clicked
	
	$jq(document).ready(function() {
	
		lastTarget = $jq('#div-' + 'spanish');
		lastImage = $jq('.spanish' + ' img');
		$jq('#productInfoNav a').click(function(){
		// alert($(this).children('img').attr('src'));
			var target = $jq(this).attr('class'); // target div name
			//alert(target);
			
			// if the divs are different
			if (lastTarget.html() != $jq('#div-' + target).html())
			{
				var lastSource = lastImage.attr('src'); // 
				lastSource = lastSource.replace(/-selected/gi,'');
				lastImage.attr('src', lastSource);
				lastTarget.fadeOut('fast', function() {
				$jq('#div-' + target).fadeIn('slow');
				});

				var currentSource = $jq(this).children('img').attr('src');
				currentSource = currentSource.replace(/\.gif/gi,'-selected.gif');
				$jq(this).children('img').attr('src', currentSource)
			}
			
			lastTarget = $jq('#div-' + target);
			lastImage = $jq(this).children('img');
			//alert(lastImage.val());
			return false;

  		 });
	});


$jq(document).ready(function()
{
	$jq('#first-step input').click(function()
	{
		//alert( $jq(this).attr('value') );
		if ($jq(this).attr('value') == 'colors-all')
		{
			$jq('.glazed').css('display', 'block').fadeIn('slow');
			$jq('.matte').css('display', 'block').fadeIn('slow');
		}
		else if ($jq(this).attr('value') == 'colors-glazed')
		{
			$jq('.glazed').css('display', 'block').fadeIn('slow');
			$jq('.matte').css('display', 'none').fadeOut('slow');
		}
		else if($jq(this).attr('value') == 'colors-matte')
		{
			$jq('.glazed').css('display', 'none').fadeOut('slow');
			$jq('.matte').css('display', 'block').fadeIn('slow');
		}
	});

	/*
	---- show/hide effects for blends.php page 
	*/
		$('#m_amap area').bind('mouseover', function()
		{
			// set the source of the accessory-holder div to the matching image on the area map
			// store the id value
			var id = $(this).attr('id');
			var idValue = id.match(/\d+/gi);
			var altValue = $(this).attr('alt');
			var extra = $(this).attr('extra');
			$('#accessory-holder img').attr('src','images/accessories/img-acc-' + idValue + '.jpg');
			$('#accessory-holder p#accessory-name').html(altValue);
			$('#accessory-holder p#accessory-extra').html(extra);
		});
		
		$('#m_amap area').bind('mouseout', function()
		{
			// set the source of the accessory-holder div back to original image
			$('#accessory-holder img').attr('src', 'images/accessories/img-accessory-holder.gif');
			$('#accessory-holder p').html('');
			
		});
		
		$('#m_amap area').bind('click', function()
		{
			// prevent default behavior
			return false;
		});
});

