/*
 * RobsChoice.com Javascript functions - application specific (not generic)
 */

function pageInit()
/*
 * Called form the body's onload event.
 */
{
	// set a last_visited_date cookie
	var visit_date = new Date();
	setCookie('last_visited_year', visit_date.getUTCFullYear() );
	setCookie('last_visited_month', visit_date.getUTCMonth() );
	setCookie('last_visited_day', visit_date.getUTCDate() );

	// setup scrolling for skyscraper ad
	addEvent(window, 'scroll', _skyscraperAdHandleWindowScroll);

	//testMe();
	
	return;
}

function openFeedbackDialog()
{
	return openModalDialog(550, 200, 'Feedback', 'feedback', '/modal/ModalContent-Feedback.aspx?selectedPage=' + escape(document.URL) );
}

function openTellAFriendDialog()
{
	var dialogTitle = 'Share this page with a Friend';
	var url = '/modal/ModalContent-TellAFriend.aspx?selectedPage=' + escape(document.URL);
	return openModalDialog(645, 320, dialogTitle, 'toEmail', url);
}

function openNotifyDialog(selectedCategory)
{
	var dialogTitle = 'Notify Me';
	var url = '/modal/ModalContent-NotifyMe.aspx?selectedCategory=' + escape(selectedCategory);
	return openModalDialog(550, 200, dialogTitle, 'emailAddress', url);
}

function openAlertMeDialog(selectedCategory)
{
	var dialogTitle = 'Alert Me';
	var url = '/modal/ModalContent-AlertMe.aspx?selectedCategory=' + escape(selectedCategory);
	return openModalDialog(550, 200, dialogTitle, 'emailAddress', url);
}

function openMessageDialog(message)
{
	return openModalDialog(400, 90, message, 'okbutton', '/modal/ModalContent-Message.aspx');
}

function validateAndSubmitTellAFriend()
/*
* Called from the TellAFriend modal dialog - WebKit browsers (Safri and Chrome) can't run JavaScript that is dynamically loaded.
*/
{
	if (document.getElementById('toEmail').value.length > 5 
			&& document.getElementById('fromEmail').value.length > 5
			&& document.getElementById('emailBody').value.length > 10
			&& document.getElementById('fromName').value.length > 0 ) {
		
		// valid
		submitModalForm(document.getElementById('modalDialogForm'), '/modal/ModalContent-TellAFriend.aspx', 'Message sent.', true);
	} else {
		alert('All fields are required.');
	}
	return false; // critical to return false to prevent normal submit behaviour on some browsers
}

function showYouTubeVideo(videoTitle, youTubeURL)
{
	var winOptions = 'width=720, height=550, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no';
	var url = '/include/show_video.aspx?videoTitle='+escape(videoTitle)+'&youTubeURL='+escape(youTubeURL);
	window.open(url, '_blank', winOptions);
	return;
}

function _skyscraperAdHandleWindowScroll()
// scrolls the right col skyscraper ad so always visible when user scrolls
{
	var mainColHeight = document.getElementById("reviewCol").clientHeight;
	var adDiv = document.getElementById("skyscraperAdDiv");
	var adHeight = adDiv.clientHeight + 60; // extra space is to avoid problems with out of control scrolling
	
	var adTopMargin = getScrollTop() - document.getElementById("pageHeaderRow").clientHeight;
	if (adTopMargin < 0) adTopMargin = 0;
	
	if ((adTopMargin + adHeight) < mainColHeight) {
		adDiv.style.marginTop = adTopMargin+"px";
	} else if ((mainColHeight - adHeight) > 0) {
		adDiv.style.marginTop = (mainColHeight - adHeight)+"px";
	}
}
