/**
 * The DC object is the single global object used by the DC Library.  It
 * can be setup to contain utility functions for initialization, setting up 
 * namespaces & inheritance.
 * 
 * @module dc_util.js
 * @title  DC
 */

// test if DC namespace exists
if(window["DC"] == null) {
	DC = {};
}

var errorMsg=new String();	
var anyErrors=0;

//holds current background image
var bgImage;
var sendFriendConfig = {
	overlayHeight: '305px',
	overlayWidth: '628px',
	sendAnother: true,
	submission: {
		id: 'hdm.696',
		url:'http://hdonline.rsys1.net/servlet/campaignrespondent'
	},
	callback: function(media) {
		stafMedia = media;
	},
	getParams: function(params, media) {
		var nav = DarkCustom.getNavigation();
		params.community = comSiteName;
		params.view = comPageUrl;
		params.media = (media && media.mediaId) || '0';
		params.param1 = media && media.mediaId ? 'detail' : 'gallery';
		params.param2 = media && media.contentType ? media.contentType : 'all';
		params.param3 = '#' + HD.util.Common.timestamp() + Math.floor(Math.random()*10000);
		return params;
	}
};

/**
 * DC.util is the namespace used for all Dark Custom utility functions
 * @class DC.util
 * @type Object
 * @static
 */
DC.util = {
	
	/**
	 * init function preloads images and other assets, fires the initial call to set the background, and assigns click events to initial page elements to change backgrounds, 
	 * @type Object
	 * @static
	 */
	init : function(){
		//set video player skin params
		HD.util.Player.View.prototype.setStyle('&frontcolor=999999&lightcolor=ffffff');
	
		//Preload all background images
		DC.util.changeBackground();
		
		var setHighlight = function(e) { 
			if(!HD.hasClass(this, 'hdActive')){
				
				if(HD.hasClass(this, this.id)){
					HD.replaceClass(this, this.id, this.id+"On")
				}		
				else{
					HD.replaceClass(this, this.id+"On", this.id)
				}
			}
		} 
			
		// Set nav listeners
		HD.getByClass('hdNavigation', 'li', 'navBar', function(el) {
		
			YAHOO.util.Event.addListener(this, "mouseover", setHighlight);
			YAHOO.util.Event.addListener(this, "mouseout", setHighlight);
			
		});
	},
	
	showSelectBoxes : function(){
		var selectElements = YAHOO.util.Dom.getElementsBy(function(){return true;}, 'select');		
		for(var x=0; x<selectElements.length; x++){			
			YAHOO.util.Dom.setStyle(selectElements[x], 'visibility', 'visible');			
		}				
	},
	
	hideSelectBoxes : function() {
		var selectElements = YAHOO.util.Dom.getElementsBy(function(){return true;}, 'select');
		var overlaySel = YAHOO.util.Dom.getElementsBy(function(){return true;}, 'select','submissionContent');
		for(var x=0; x<selectElements.length; x++){			
			if(selectElements[x] != overlaySel[0]){
				YAHOO.util.Dom.setStyle(selectElements[x], 'visibility', 'hidden');
			}				
		}			
	},
	

	/**
	 * changeBackground changes the background image based on math.random in the range of the array of available images.
	 * @type Object
	 * @static
	 */
	changeBackground : function (){
	
		//set base image path
		var imgPath = "/en_US/Media/images/Content/Pictorial/dark_custom_v3/";
		
		//create imgPaths array
		var imgPaths = new Array();
		var tilePaths = new Array();
		var tiles = new Array();
		
		// load paths into array
		imgPaths[0] = "";
		imgPaths[1] = imgPath + "img_bg_DC_deadmechanic.jpg";
		imgPaths[2] = imgPath + "img_bg_DC_drippage.jpg";
		imgPaths[3] = imgPath + "img_bg_DC_gearz.jpg";
		imgPaths[4] = imgPath + "img_bg_DC_mahogonydreamz.jpg";
		imgPaths[5] = imgPath + "img_bg_DC_polaroidz.jpg";
		imgPaths[6] = imgPath + "img_bg_DC_scribb-lore.jpg";
		imgPaths[7] = imgPath + "img_bg_DC_ARROWZ.jpg";
		
		tilePaths[0] = "";
		tilePaths[1] = imgPath + "deadmechanicTile.jpg";
		tilePaths[2] = imgPath + "drippageTile.jpg";
		tilePaths[3] = imgPath + "gearzTile.jpg";
		tilePaths[4] = imgPath + "mahogonydreamzTile.jpg";
		tilePaths[5] = imgPath + "polaroidzTile.jpg";
		tilePaths[6] = imgPath + "scribb-loreTile.jpg";
		tilePaths[7] = "";
		
		tiles[0] = "";
		tiles[1] = '#161616' ;
		tiles[2] = '#000000 url('+ tilePaths[2] +') repeat left top';
		tiles[3] = '#000000';
		tiles[4] = '#1b0f03';
		tiles[5] = '#000000 url('+ tilePaths[5] +') repeat-x left top';
		tiles[6] = '#000000 url('+ tilePaths[6] +') repeat left top';
		tiles[7] = '#000000';
		
	
		
		//create random number and use it to select an image path
		var rand = Math.floor(Math.random()*7+1);
		var randImage = imgPaths[rand];
		var randTile = tiles[rand];
		var htmlEl = YAHOO.util.Dom.getElementsBy(function(){return true},'html');
		var tbodyEl = YAHOO.util.Dom.getElementsBy(function(){return true},'table');
		var tableIntEl = YAHOO.util.Dom.getElementsByClassName('hdWrapper','table');
		
		YAHOO.util.Dom.setStyle('bgtile','background','transparent');
		YAHOO.util.Dom.setStyle(tableIntEl,'background','transparent');
		
		//compare stored bgImage to new randImage picked
		if ( bgImage == randImage ){
			rand = Math.round((rand / 2) + 1);
			//store background image and properties
			image = imgPaths[rand];
			bgProperties =  'transparent url(' + image + ') no-repeat center top';
			//store tile properties
			tileProperties = tiles[rand];
			//set background image and corresponding tile
			//YAHOO.util.Dom.setStyle('bgTile','background',tileProperties);
			YAHOO.util.Dom.setStyle('contentCenter','background',bgProperties);
			//set space at bottom to same as content bg
			YAHOO.util.Dom.setStyle(htmlEl,'background',tileProperties);
			YAHOO.util.Dom.setStyle(tbodyEl,'background',tileProperties);
			YAHOO.util.Dom.setStyle(tableIntEl,'background','transparent');
			//reset bgImage to new image
			bgImage = image;
		}
		else {
			//store background image and properties
			image = randImage;
			bgProperties =  'transparent url(' + image + ') no-repeat center top';
			//store tile properties
			tileProperties = randTile;
			//set background image and corresponding tile
			//YAHOO.util.Dom.setStyle('bgTile','background',tileProperties);
			YAHOO.util.Dom.setStyle('contentCenter','background',bgProperties);
			YAHOO.util.Dom.setStyle(htmlEl,'background',tileProperties);
			YAHOO.util.Dom.setStyle(tbodyEl,'background',tileProperties);
			YAHOO.util.Dom.setStyle(tableIntEl,'background','transparent');
			//reset bgImage to new image
			bgImage = image;
		}
	}
}

YAHOO.util.Event.onDOMReady(function(){

	//var myLogReader = new YAHOO.widget.LogReader("myLogger"); //logger
	
	//add required YUI class to body tag
	var bodyEl = document.getElementsByTagName('body');
	YAHOO.util.Dom.addClass(bodyEl,'yui-skin-sam');
		
	//check for center content div and then init util script (runs preloader and assigns initial bkg image randomly to contentCenter div
	YAHOO.util.Event.onAvailable('contentCenter',function(){
		DC.util.init();
	});
});