// Install dummy functions, so that logging does not break the code if Firebug is not present
if(typeof(console) == "undefined") {
	var console = function(){
		var e = function(){}, console = {
			log: e, debug: e, info: e, warn: e, error: e, assert: e, dir: e, dirxml: e, trace: e,
			group: e, groupEnd: e, time: e, timeEnd: e, profile: e, profileEnd: e, count: e
		};
		return console;
	}();
}

function popAtomWindow(winObj) {
	/*
	USAGE EXAMPLE:
	
	popAtomWindow({
		url:'http://exodus.atom.com/site/signup',
		name:'newsLetterSignupPop',
		width:720,
		height:560,
		centered:1
	});
	
	centered:1 for centered window
	centered:0 for non-centered window
	*/

	var atomWidth = 30;
	var atomHeight = 30;
	if (winObj.centered == 1) {
		atomWidth = (window.screen.width/2) - ((winObj.width/2) + 10);
		atomHeight = (window.screen.height/2) - ((winObj.height/2) + 50);
	}
	var atomPopupWindow = window.open(winObj.url,winObj.name,'status=no,height=' + winObj.height + ',width=' + winObj.width + ',resizable=yes,left=' + atomWidth + ',top=' + atomHeight + ',screenX=' + atomWidth + ',screenY=' + atomHeight + ',toolbar=no,menubar=no,scrollbars=no,location=no,directories=no');
	atomPopupWindow.focus();
}

function sendToLogin() {
	var theUrl = "";
	theUrl += "http://community.atom.com/SignUp.aspx";
	theUrl += "?returnPath=" + encodeURIComponent(document.location.href);
	theUrl += "&communityUcid=3EFBFFFF000DA11C0002FFFFFB3E";
	window.location = theUrl;
}

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // NOTE Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

// load Related Videos scroller
function relatedVideosScroller() {
	// fill the list to a number divisible by 3 so that all items can be scrolled to
	var relItemsCount = $j('.relatedThumbs li').length % 3;
	if (relItemsCount > 0) {
		for (var i=0; i<relItemsCount; i++) {
			$j('.relatedThumbs ul li:last').after('<li></li>');
		}
	}
	// init carousel
    $j(".relatedThumbs").jCarouselLite({
        btnNext: ".relatedNext",
        btnPrev: ".relatedPrev",
        circular: false,
        scroll: 3
    });
	$j("#relatedText").html(defaultRelatedText);
	$j("#relatedText").truncate(relatedMaxCharacters);
	$j("#relatedText").css({visibility:"visible"});
}

// nav scripts
window.loaded = false;
function loadNav(){
	// check the ad size
	if (!window.loaded) {

		// search atom
		$j('#searchAtom').click(function(){
			$j('#navsearch-form').submit();
			return false;
		});
	
		// search web
		$j('#searchWeb').click(function(){
			var searchUrl = 
				"http://search.live.com/results.aspx?q=" + escape($j('#mainSearch').attr('value')) + "&mkt=en-us&FORM=VCM013";
			window.open(searchUrl);
			return false;
		});
	
		// clear default text in search
		$j('#mainSearch').focus(function(){
			var search = $j(this).addClass('search-entered');
			if (search.attr('value') == search.attr('defaultValue')) {
				search.attr('value','');
			}
		});
	
		$j('#mainSearch').blur(function(){
			var search = $j(this);
			if (search.attr('value') == '') {
				search.attr('value',search.attr('defaultValue')).removeClass('search-entered');
			}
		});

		// capture email link for popup
		$j('#topnav-email').click(function(){
			window.popAtomWindow({
				url:this.href,
				name:'newsLetterSignupPop',
				width:720,
				height:560,
				centered:1
			});
			return false;
		});

		// fade effects for social media buttons
		var fot = 300,
			alltt = $j('#socialmedia a div.tooltip-hover').bind('mouseleave', function(){ $j(this).stop(false,true).fadeOut(fot); });

		// tool-tip hovering
		$j('#socialmedia a').bind('mouseenter', function(){
				alltt.stop(false,true).fadeOut(0);
				$j(this).find('div.tooltip-hover').stop(false,true).fadeIn(fot);
			}).bind('mouseleave', function(){
				$j(this).find('div.tooltip-hover').stop(false,true).fadeOut(fot);
			});

		// hide all tool-tips when hovering over search
		$j('#navsearch').bind('mouseenter', function(){ alltt.stop(false,true).fadeOut(fot); });
		
		$j('#socialmedia a div.tooltip-hover').each(function(){
			$j(this).css('left',(47 - parseInt($j(this).width(),10))+'px');
		});

		// adjust the banner
		if ($j('#top-banner-ad')
				&& $j('#top-banner-ad').width() < 730 
				&& $j('#top-banner-ad .adExpand').length < 1 
				&& $j('#leaderboard').length > 0) {
			if ($j('#nav-hot-stuff').length > 0) {
				$j('#top-banner-ad').addClass('narrow');
				$j('#leaderboard').html($j('#nav-hot-stuff').remove().html()).show();
				$j('#leaderboard-spacer').show();
			} else {
				// if there's no "Hot Stuff" content, center the ad instead
				if ($j('#adblock-hotstuff').length > 0) {
					$j('#adblock-hotstuff').remove();
					$j('#adblock table').width('100%')
						.find('td')
							.css({'text-align':'center'})
							.attr('align','center');
					$j('#top-banner-ad').addClass('wide');
				}
			}
		}
		window.loaded = true;
	}
}
$j(document).ready(loadNav);
