/***************************************************** 
										products.js
****************************************************** 

         Image Swapping Utility Used On:
				 
					      - /products/boards.html
					      - /sales/products/boards_sales.html
							  - /products/accessories.html
								
*****************************************************/

function getProducts(){
	var productsResDir = getBaseDir() + 'products/res/';
	var salesResDir    = getBaseDir() + 'sales/products/res/';
	
	if(!document.products) {
		document.products = new Object();
		document.products.boards = new Object();
		document.products.ripple = new Object();
		document.products.boards_sales = new Object();
		document.products.accessories = new Object();
		
		// ripples.html
		var r = document.products.ripple;
		var r_resDir = productsResDir + 'ripple/';
		r.flame                    = r_resDir + 'ripple-flame.jpg';
		r.checker                  = r_resDir + 'ripple-checker.jpg';
		r.jellybean                = r_resDir + 'ripple-jellybean.jpg';
		r.teeth			           = r_resDir + 'ripple-teeth.jpg';
		r.cyclone                  = r_resDir + 'ripple-cyclone.jpg';
		
		// boards.html
		var b = document.products.boards;
		var b_resDir = productsResDir + 'boards/';
		b.silver                   = b_resDir + 'silver-board.jpg';
		b.checker                  = b_resDir + 'checkers-board.jpg';
		b.sketch                   = b_resDir + 'sketch-board.jpg';
		b.red_and_silver           = b_resDir + 'red_silver-board.jpg';
		b.graffiti                 = b_resDir + 'grafitti-board.jpg';
		b.scrolls_and_skulls       = b_resDir + 'scrolls_and_skulls-board.jpg';
		b.hibiscus                 = b_resDir + 'hibiscus-board.jpg';
		b.black_and_white          = b_resDir + 'black_and_white-board.jpg';
		
		// boards_sales.html
		var b_sales = document.products.boards_sales;
		var b_salesResDir = salesResDir + 'boards/';
		b_sales.silver             = b_salesResDir + 'silver_board_sales.jpg';
		b_sales.checker            = b_salesResDir + 'checker_board_sales.jpg';
		b_sales.sketch             = b_salesResDir + 'sketchy_board_sales.jpg';
		b_sales.red_and_silver     = b_salesResDir + 'red_silver_board_sales.jpg';
		b_sales.graffiti           = b_salesResDir + 'graffiti_board_sales.jpg';
		b_sales.scrolls_and_skulls = b_salesResDir + 'scrolls_and_skulls_sales.jpg';
		b_sales.hibiscus           = b_salesResDir + 'hibiscus_board_sales.jpg';
		b_sales.black_and_white    = b_salesResDir + 'blackandwhite_board_sales.jpg';
		b_sales.green              = b_salesResDir + 'green_board_sales.jpg';
		b_sales.blue_skulls        = b_salesResDir + 'blue_skulls-sales.jpg';
		
		// accessories.html
		var acc = document.products.accessories;
		var accResDir = productsResDir + 'accessories/';
		
		acc.deckplates           = new Object();
		acc.helmets_orange_green = new Object();
		acc.helmets_black        = new Object();
		acc.bags                 = new Object();
		acc.wheels_black         = new Object();
		acc.wheels_pink          = new Object();
		acc.wheels_green         = new Object();
		acc.wheels_red           = new Object();
		
		acc.deckplates.image = accResDir + 'deckplates.jpg';
		acc.deckplates.descr = '<h3><img src="res/accessories/titles/title-deckplates.png" alt="Street Surfing Deck Plates for The Wave" width="229" height="37" style="margin-left:-5px;" onload="fixPNG(this)"></h3><p>Now you can upgrade your Wave board with our new line of deck plate sets.  Each set comes with instructions and mounting screws.  Just think of it as &ldquo;bling for your board&rdquo;!</p>';
		
		acc.helmets_orange_green.image    = accResDir + 'helmets_orange_green.jpg'; 
		acc.helmets_orange_green.descr    = '<h3><img src="res/accessories/titles/title-helmets.png" alt="Signature Street Surfing Helmets" width="229" height="37" style="margin-left:-5px;" onload="fixPNG(this)"></h3><p>We, at Street Surfing, want to keep riders safe whenever they ride The Wave.  That is why we developed our own CPSC approved helmet.  Use it when riding The Wave, or any other skate product.  It has a comfortable fit with an adjustable chin strap and plenty of vents to keep your head cool while riding.  Outrageous graphics keep this helmet a &ldquo;head&rdquo; of the crowd.</p>';
		
		acc.helmets_black.image    = accResDir + 'helmets_black.jpg'; 
		acc.helmets_black.descr    = '<h3><img src="res/accessories/titles/title-helmets.png" alt="Signature Street Surfing Helmets" width="229" height="37" style="margin-left:-5px;" onload="fixPNG(this)"></h3><p>We, at Street Surfing, want to keep riders safe whenever they ride The Wave.  That is why we developed our own CPSC approved helmet.  Use it when riding The Wave, or any other skate product.  It has a comfortable fit with an adjustable chin strap and plenty of vents to keep your head cool while riding.  Outrageous graphics keep this helmet a &ldquo;head&rdquo; of the crowd.</p>';
		
		acc.bags.image       = accResDir + 'bags.jpg'; 
		acc.bags.descr       = '<h3><img src="res/accessories/titles/title-bags.png" alt="Street Surfing Carry Bags" width="229" height="37" style="margin-left:-5px;" onload="fixPNG(this)"></h3><p>Take The Wave wherever you go and carry it around in style. The Wave carry bags make it so easy to carry The Wave and they look good, too. The bags even have a place to hold a cell phone or mp3 player.  Bags come in two styles, classic black and red and a grey camouflage.  Check them out in the StreetSurfing online store.</p>'; 
		
		acc.wheels_black.image = accResDir + 'wheels_black.jpg';
		acc.wheels_black.descr = '<h3><img src="res/accessories/titles/title-wheels.png" alt="Black Street Surfing Wheels for The Wave" width="229" height="37" style="margin-left:-5px;" onload="fixPNG(this)"></h3><p>Street Surfing wheels look good and feel good too. &nbsp;They are specially made for optimum performance of the Wave and will last longer than the average inline skate wheel. Each pair of wheels comes with ABEC 5 bearings and wheel spacers. </p>';
		
		acc.wheels_pink.image = accResDir + 'wheels_pink.jpg';
		acc.wheels_pink.descr = '<h3><img src="res/accessories/titles/title-wheels.png" alt="Pink Street Surfing Wheels for The Wave" width="229" height="37" style="margin-left:-5px;" onload="fixPNG(this)"></h3><p>Street Surfing wheels look good and feel good too. &nbsp;They are specially made for optimum performance of the Wave and will last longer than the average inline skate wheel. Each pair of wheels comes with ABEC 5 bearings and wheel spacers. </p>';
		
		acc.wheels_green.image = accResDir + 'wheels_green.jpg';
		acc.wheels_green.descr = '<h3><img src="res/accessories/titles/title-wheels.png" alt="Green Street Surfing Wheels for The Wave" width="229" height="37" style="margin-left:-5px;" onload="fixPNG(this)"></h3><p>Street Surfing wheels look good and feel good too. &nbsp;They are specially made for optimum performance of the Wave and will last longer than the average inline skate wheel. Each pair of wheels comes with ABEC 5 bearings and wheel spacers. </p>';
		
		acc.wheels_red.image = accResDir + 'wheels_red.jpg';
		acc.wheels_red.descr = '<h3><img src="res/accessories/titles/title-wheels.png" alt="Red Street Surfing Wheels for The Wave" width="229" height="37" style="margin-left:-5px;" onload="fixPNG(this)"></h3><p>Street Surfing wheels look good and feel good too. &nbsp;They are specially made for optimum performance of the Wave and will last longer than the average inline skate wheel. Each pair of wheels comes with ABEC 5 bearings and wheel spacers. </p>';
		
	} else {
	  // fall through
	}
	return document.products;
}		
function initProductsPage(type){
	var inProduct = getProductFromURL();
	var page_type = getPageType();
	var products = getProducts();
	
	//console.log('initProductsPage for type: ' + type);
	
	// preload all the images for this page_type for fast switching
	if(page_type == 'accessories'){
		// go one level deeper to get image paths out of object
		for ( var node in products[page_type] ){
		  preloadImages(products[page_type][node].image);
		}
	} else {		
		for( var img in products[page_type]){
			preloadImages(products[page_type][img]);
		}		
	}
	// if theres a product definition riding in the url, set it here
	if (inProduct){ swapProduct(inProduct); }
	else { setDefaultProduct(page_type);	}
}
function swapProduct(inProduct){
	var page_type = getPageType();
	var products = getProducts();	
	var cont = getContainer();
	
	if(page_type == 'accessories'){
		var cont_2 = document.getElementById('bottom_panel_left');
		cont.innerHTML = '<img src="'+products[page_type][inProduct].image+'" width="720" height="215" />';
		cont_2.innerHTML = products[page_type][inProduct].descr;
	} else {
		cont.innerHTML = '<img src="'+products[page_type][inProduct]+'" width="720" height="215" />';
	}
}

function getContainer(){
	var cont = document.getElementById('productContainer');
	return cont;
}
function setContainer(){
	var cont = getContainer();
	cont.style.width = "720px";
	cont.style.height = "215px";
	cont.align = "center";
	return cont;
}
function setDefaultProduct(page_type){
	var cont = getContainer();
	var path = location.pathname;
	var default_image = "";
	
	switch(page_type){
		case 'boards':
		  default_image = 'res/boards/silver-board.jpg';
			cont.innerHTML = '<img src="'+ default_image +'" width="720" height="215" />';
			break;	
		case 'ripple':
		  default_image = 'res/ripple/ripple-checkers.jpg';
			cont.innerHTML = '<img src="'+ default_image +'" width="720" height="215" />';
			break;	
		case 'boards_sales':
		  default_image = 'res/boards/silver_board_sales.jpg';
			cont.innerHTML = '<img src="'+ default_image +'" width="720" height="215" />';
			break;	
		case 'accessories':
		  var def = document.products.accessories.deckplates;
		  var cont_2 = document.getElementById('bottom_panel_left');
			cont.innerHTML = '<img src="'+ def.image +'" width="720" height="215" />';
			cont_2.innerHTML = def.descr;
			break;	
		default:	
		  //console.log('this page type has not been defined yet');
			break;
	}
	
}
function getProductFromURL(){
	// check for url argument specifying which board  or product we have
	// i.e. takes '?board=green' and splits it up to return 'green'
  var args = new Object;
	var query = location.search.substring(1);
	var pos = query.indexOf('=');
	var argName = query.substring(0, pos);
	var value = query.substring(pos+1);
	if (argName == 'board' || argName == 'prod'){ return value; }
	else { return null; }
}
function getPageType(){
	var path = location.pathname;
	var page_type = '';
	if(path.search(/boards.html/i) > 0){	page_type = 'boards'; }		
	else if (path.search(/boards_sales.html/i) > 0){ page_type = 'boards_sales';	}	
	else if (path.search(/accessories.html/i) > 0){ page_type = 'accessories';	}	
	else if (path.search(/ripples.html/i) > 0){ page_type = 'ripple';	}
	else { 
	  // console.log('getPageType -> page type unknown'); 
	}
	return page_type;
}

function preloadImages()
{
  if(document.images)
  {
    if(!document.imageArray) document.imageArray = new Array();
    var i,j = document.imageArray.length, args = preloadImages.arguments;
    
    for(i=0; i<args.length; i++)
    {
      if (args[i].indexOf("#")!=0)
      {
        document.imageArray[j] = new Image;
        document.imageArray[j++].src = args[i];
      }
    }
  }
}