/**
* utilities.js
*******************************************************************/
function openMostEverythingBox(id){
	var menus = new Array ("subnav-most-emailed","subnav-most-viewed","subnav-most-searched");
	if (document.getElementById) {
		obj=document.getElementById(id);
		obj.className='open';

		var x;
		for (x in menus)
		{
/* close all menus except the menu being opened (menus[x]==id) and any menu containing the menu being opened (menu name is contained in the menu name of a submenu -- subnav-rc contains cubnav-rc-il -- so id.indexOf(menus[x])==-1)*/
		if (!(menus[x]==id)) {
			if (id.indexOf(menus[x])==-1) {
				obj=document.getElementById(menus[x]);
				obj.className='closed';
				}
			}
		}

		return false;
		}
}

/* function to alter background colors of table*/
function alternate(id){
 if(document.getElementsByTagName){  
   var table = document.getElementById(id);  
   var rows = table.getElementsByTagName("tr");  
   for(i = 0; i < rows.length; i++){          
 //manipulate rows
	if (rows[i].className != "th2") {
     if(i % 2 == 0){
       rows[i].className = "table-cell-even";
     }else{
       rows[i].className = "table-cell-odd";
     }      
	}
   }
 }
}

/* safari sniff for fieldset problem */
if (navigator.userAgent.indexOf ('Safari') != -1) {
document.write('<style>fieldset{border:none;margin:20px 0;}</style>');
}

/* populate the data when pressing the check box (Use same address used for registration) */
function populateData(id) { //id=1: called in the onClick event; id!=1: call on the Edit page
	if (document.getElementById('billing-sameasdemographics').checked) {
		document.getElementById('billing-firstname').value = document.getElementsByName('demographics-firstname')[0].value;
		document.getElementById('billing-firstname').disabled = true;
		
		document.getElementById('billing-lastname').value = document.getElementsByName('demographics-lastname')[0].value;
		document.getElementById('billing-lastname').disabled = true;
		
		document.getElementById('billing-jobtitle').value = document.getElementsByName('demographics-jobtitle')[0].value;
		document.getElementById('billing-jobtitle').disabled = true;
		
		document.getElementById('billing-company').value = document.getElementsByName('demographics-company')[0].value;
		document.getElementById('billing-company').disabled = true;
		
		document.getElementById('billing-address1').value = document.getElementsByName('demographics-address1')[0].value;
		document.getElementById('billing-address1').disabled = true;
		
		if (document.getElementsByName('demographics-address2')[0] != null) 
			document.getElementById('billing-address2').value = document.getElementsByName('demographics-address2')[0].value;
		document.getElementById('billing-address2').disabled = true;
		
		document.getElementById('billing-city').value = document.getElementsByName('demographics-city')[0].value;
		document.getElementById('billing-city').disabled = true;
		
		document.getElementById('billing-zipcode').value = document.getElementsByName('demographics-zipcode')[0].value;
		document.getElementById('billing-zipcode').disabled = true;
		
		if (document.getElementsByName('demographics-zipcode2')[0] != null)
			document.getElementById('billing-zipcode2').value = document.getElementsByName('demographics-zipcode2')[0].value;
		document.getElementById('billing-zipcode2').disabled = true;
		
		document.getElementById('billing-state').value = document.getElementsByName('demographics-state')[0].value;
		document.getElementById('billing-state').disabled = true;
		
		document.getElementById('billing-country').value = document.getElementsByName('demographics-country')[0].value;	
		document.getElementById('billing-country').disabled = true;
	
		document.getElementById('billing-phone').value = document.getElementsByName('demographics-phone')[0].value;
		document.getElementById('billing-phone').disabled = true;
		
		document.getElementById('billing-email').value = document.getElementsByName('demographics-email')[0].value;
		document.getElementById('billing-email').disabled = true;
		
		document.getElementById('billing-confirmemail').value = document.getElementsByName('demographics-confirmemail')[0].value;
		document.getElementById('billing-confirmemail').disabled = true;
		
	} else if (id == 1) {
		document.getElementById('billing-firstname').value = '';
		document.getElementById('billing-firstname').disabled = false;
		
		document.getElementById('billing-lastname').value = '';
		document.getElementById('billing-lastname').disabled = false;
		
		document.getElementById('billing-jobtitle').value = '';
		document.getElementById('billing-jobtitle').disabled = false;
		
		document.getElementById('billing-company').value = '';
		document.getElementById('billing-company').disabled = false;
		
		document.getElementById('billing-address1').value = '';
		document.getElementById('billing-address1').disabled = false;
		
		document.getElementById('billing-address2').value = '';
		document.getElementById('billing-address2').disabled = false;
		
		document.getElementById('billing-city').value = '';
		document.getElementById('billing-city').disabled = false;
		
		document.getElementById('billing-zipcode').value = '';
		document.getElementById('billing-zipcode').disabled = false;
		
		document.getElementById('billing-zipcode2').value = '';
		document.getElementById('billing-zipcode2').disabled = false;
		
		document.getElementById('billing-state').value = '';
		document.getElementById('billing-state').disabled = false;
		
		document.getElementById('billing-country').value = 'UNITED STATES';	
		document.getElementById('billing-country').disabled = false;
	
		document.getElementById('billing-phone').value = '';
		document.getElementById('billing-phone').disabled = false;
		
		document.getElementById('billing-email').value = '';
		document.getElementById('billing-email').disabled = false;
		
		document.getElementById('billing-confirmemail').value = '';
		document.getElementById('billing-confirmemail').disabled = false;
	}			
}

function addOnLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') oldonload = null;
	window.onload = function() {
		if (oldonload) {
			oldonload();
		}
		eval(func);
	}
}

var rotate_defaultfrequency = 10000; // The default frequency (which can be passed to the rotateContentSetup method)
var rotate_currentindex = new Object(); // Keeps track of the current index for each
var rotate_frequency = new Object();
var rotate_numassets = new Object();
var rotate_timeoutid = new Object();
function rotateContent(assetlistrotatingid, increment) {
	// Update the current number
	rotate_currentindex[assetlistrotatingid] = rotate_currentindex[assetlistrotatingid] + increment;
	if (rotate_currentindex[assetlistrotatingid] < 1) rotate_currentindex[assetlistrotatingid] = rotate_numassets[assetlistrotatingid];
	if (rotate_currentindex[assetlistrotatingid] > rotate_numassets[assetlistrotatingid]) rotate_currentindex[assetlistrotatingid] = 1;

	// Set the display style on each item (visible or hidden))
	for (var i = 1; i <= rotate_numassets[assetlistrotatingid]; i++) {
		document.getElementById("rotatingcontent-" + assetlistrotatingid + "-" + i).style.display = (i == rotate_currentindex[assetlistrotatingid] ? "inline" : "none");
	}

	// Setup the next automatic rotate
	rotate_timeoutid[assetlistrotatingid] = window.setTimeout("rotateContent(\"" + assetlistrotatingid + "\", 1)", rotate_frequency[assetlistrotatingid]);
}
function rotateContentManual(assetlistrotatingid, increment) {
	// Clear the next automatic rotate
	if (rotate_timeoutid[assetlistrotatingid]) window.clearTimeout(rotate_timeoutid[assetlistrotatingid]);
	// Rotate now
	rotateContent(assetlistrotatingid, increment);
}
function rotateContentSetup(assetlistrotatingid, frequency, numassets) {
	rotate_currentindex[assetlistrotatingid] = 1;
	rotate_frequency[assetlistrotatingid] = frequency;
	rotate_numassets[assetlistrotatingid] = numassets;
	rotate_timeoutid[assetlistrotatingid] = window.setTimeout("rotateContent(\"" + assetlistrotatingid + "\", 1)", rotate_frequency[assetlistrotatingid]);
}

/**
* jquery.functions.js
*******************************************************************/

function getMinutesAgo(timestamp) {
	time_diff = request_time - timestamp;
	minutes_ago = Math.floor(time_diff / 60);
		
	if (minutes_ago <= 1) {
		return '1 minute ago';
	} else {
		return minutes_ago+' minutes ago';
	}
}

function navOver() {
	if (!$(this).hasClass('top-level')) {
		$(this).next('div.box').slideDown();
	}
}
function navOut(eventObj) {
	if (!$(eventObj.relatedTarget).hasClass('content') && !$(eventObj.relatedTarget).hasClass('box')) {
		$(this).next('div.box').slideUp('fast');
	}
}
function navBoxOver() {
	return;
}
function navBoxOut(eventObj) {
	if ($(eventObj.relatedTarget).attr('id') != $(this).siblings('a').attr('id')) {
		$(this).slideUp('fast');
	}
}

/**
* On DOM Load
*******************************************************************/

$(document).ready(function(){
	
	// Timestamps
	$('span[time]').each(function(){
		time_diff = request_time - $(this).attr('time');
		minutes_ago = Math.floor(time_diff / 60);
		
		if (minutes_ago <= 1) {
			$(this).text('1 minute ago');
		} else {
			$(this).text(minutes_ago+' minutes ago');
		}
	});
	
	// Top Navigation
// 	if ($('div.navigation').length > 0) {
// 		var hoverIntentConfig = {
// 			over: navOver,
// 			timeout: 500,
// 			out: navOut
// 		};
// 		var hoverOutIntentConfig = {
// 			over: navBoxOver,
// 			timeout: 500,
// 			out: navBoxOut
// 		};
// 		$('div.navigation li a').hoverIntent(hoverIntentConfig);
// 		$('div.navigation div.box').hoverIntent(hoverOutIntentConfig);
// 	}
	// Fancy Box
	if ($('a.fancybox').length > 0) {
		$("a.fancybox").fancybox({
			'titleShow' : false
		});	
	}
	// Text Size
	if ($('#articleCSS').length > 0) {
		$('#articleCSS li').hover(
			function(){
				if ($(this).hasClass('active')) $(this).children('a').css('cursor', 'default');
			},
			function(){
				if ($(this).hasClass('active')) $(this).children('a').css('cursor', 'pointer');
			}
		);
		$('#articleCSS li').click(function(){
			if ($(this).hasClass('active')) return;
			var id_split = $(this).attr('id').split('-');
			$.cookie('storyCSSToggle', id_split[1]);
			jQuery.storyCSSToggle();
		});
		jQuery.storyCSSToggle = function() {
			if ($.cookie('storyCSSToggle')) {
				var type = $.cookie('storyCSSToggle');
				$('ul#articleCSS li').each(function(){
					if ($(this).attr('id') == 'articleCSS-'+type) {
						$('div.articleContainer').addClass(type+'Text');
						$(this).removeClass('inactive').addClass('active');
						$img = $(this).find('img');
						if ($img.attr('src').indexOf('_active') == -1) {
							var current_img_src_split = $img.attr('src').split('.');
							$img.attr('src', current_img_src_split[0]+'_active.'+current_img_src_split[1]);
						}
					} else {
						var this_type = $(this).attr('id').replace('articleCSS-', '');
						$('div.articleContainer').removeClass(this_type+'Text');
						$(this).removeClass('active').addClass('inactive').find('img').attr('src', '/media/ui/textsize_'+this_type+'.gif');
					}
				});
			}
		}
		if (typeof jQuery.cookie == 'undefined') {
			$.getScript('/custom/js/jquery.cookie.js', function(){
				jQuery.storyCSSToggle();
			});
		} else {
			jQuery.storyCSSToggle();	
		}
	}
});

