var ord = Math.floor(Math.random()*999999999999999999);
function setCookie(){
	if (document.cookie.length>0) {
		tc_start=document.cookie.indexOf("totalvisits=");
		if (tc_start == -1) {
			var texdate=new Date();
			var texpiredays=10000;
			var tcurrentdate=new Date();
			texdate.setDate(texdate.getDate()+texpiredays);
			tcurrentdate.setDate(tcurrentdate.getDate());
			document.cookie="totalvisits=1;expires="+texdate.toGMTString();
		} else {
			var texdate=new Date();
			var texpiredays=10000;
			var tcurrentdate=new Date();
			texdate.setDate(texdate.getDate()+texpiredays);
			tcurrentdate.setDate(tcurrentdate.getDate());

			tc_start = tc_start + 12;
			tc_end = document.cookie.indexOf(";",tc_start);
			if (tc_end==-1) tc_end=document.cookie.length;
			
    		num_visits = Number(unescape(document.cookie.substring(tc_start,tc_end)));
    		num_visits = num_visits + 1;
    		document.cookie="totalvisits=" + num_visits + ";expires="+texdate.toGMTString();
    	}
		
		c_start=document.cookie.indexOf("firstvisit=");
		if (c_start == -1) {
			var exdate=new Date();
			var expiredays=10000;
			var currentdate=new Date();
			exdate.setDate(exdate.getDate()+expiredays);
			currentdate.setDate(currentdate.getDate());
			document.cookie="firstvisit="+currentdate.toGMTString() + ";expires="+exdate.toGMTString();
			document.cookie="totalvisits=1;expires="+exdate.toGMTString();
		}
	}
	var exdate=new Date();
	var expiredays=10000;
	var currentdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	currentdate.setDate(currentdate.getDate());
	document.cookie="lastvisit="+currentdate.toGMTString() + ";expires="+exdate.toGMTString();
}
setCookie();

/* DETECT MOBILE BROWSERS */
if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1) || (navigator.userAgent.indexOf('android') != -1)) {
	var mobileBrowser = true;
}else{
	var mobileBrowser = false;	
}
$(document).ready(function(){
	/* HIDE COMMENTS */
	var displayCommentsValue = 5;
	if($(".commentlist .comment").length > displayCommentsValue){
		$("#comment-toggle").show();
		var sliceEnd = $(".commentlist .comment").length - displayCommentsValue;
		$(".commentlist .comment").slice(0,sliceEnd).addClass('hide-comment');
		$(".commentlist .hide-comment").hide();
		$("#comment-toggle").text("Showing "+displayCommentsValue+" recent comments. View all comments.");
		$("#comment-toggle").toggle(
			function(){
				$(this).text("Hide the comments.");
				$(".commentlist .hide-comment").fadeIn("slow");						  
			},
			function(){
				$(this).text("Showing "+displayCommentsValue+" recent comments. View all comments.");
				$(".commentlist .hide-comment").fadeOut("slow");						  
			}
		);
	};
	/* DROP DOWN MENU */
	$(".drop-down").hover(
		function(){
		//console.log('active roll');
		var box1 = $(this).outerWidth();
		var box2 = $(this).children("ul,ol").width();
		//console.log(box1+" "+box2);
		if(box1 > box2){
			$(this).children("ul,ol").width(box1);
		}
		$(this).addClass('drop-down-active');
		$(this).children("ul,ol").show();
		},
		function(){
		//console.log('active roll out');
		$(this).removeClass('drop-down-active');
		$(this).children("ul,ol").hide();
		}
	);
	/* FONT SIZE */
	$('.text-size').toggle(
		function(){
			// Resize to 15px
			$('.text-resize').css('font-size','1.25em');
			$('.text-noresize').css('font-size','0.8em');
			//console.log($('.text-resize-jsa').css('font-size'))  
		},
		function(){
			// Resize to 17px
			$('.text-resize').css('font-size','1.416em');
			$('.text-noresize').css('font-size','0.705em');
			//console.log($('.text-resize-jsa').css('font-size'))  
		},
		function(){
			// Original 12px
			$('.text-resize').css('font-size','1em');
			$('.text-noresize').css('font-size','1em');
			//console.log($('.text-resize-jsa').css('font-size'))  
		}
	);
	/* ANCHOR ANIMATE */
	$("a.anchorSlide").anchorAnimate()
	
	/* IE6 FIX */
	if($.browser.msie){
		$('.span-16 > .goomba.pop .hd,.span-16 > .goomba .bd,.span-16 > .goomba .fd, .span-16 > .luigi,.span-16 > .mario').css('background-position','-320px 0');
		$('.span-10 > .goomba.pop .hd,.span-10 > .goomba .bd,.span-10 > .goomba .fd,.span-10 > .luigi,.span-10 > .mario').css('background-position','-960px 0');
		$('.span-8 > .goomba.pop .hd,.span-8 > .goomba .bd,.span-8 > .goomba .fd,.span-8 > .luigi,.span-8 > .mario').css('background-position','0 0');
	}
	
	/* ARTICLE TABS */
	$(".tab-radio .hd ul").tabs(".tab-radio .tab-content",{current:'active'});
	
	/* MEDIA SCROLLER */
	$('#mediaScroll').scrollable({circular:true,speed:1500}).autoscroll({interval:4000,autopause:false});
});
/**
Anchor Slider by Cedric Dugas 
Http://www.position-absolute.com 
**/
$.fn.anchorAnimate = function(settings) {
 	settings = jQuery.extend({
		speed : 1100
	}, settings);	
	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")
			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				window.location.hash = elementClick
			});
		  	return false;
		})
	})
}
$.fn.hoverAction = function(option) {
	if(option == null){
	return this.hover(
		function(){
			if($(this).hasClass('active')){
				return;
			}else{
				$(this).addClass('hover');
			};
		},
		function(){
			$(this).removeClass('hover');
		}
	);
	//used for objects added post DOM load
	}else{
		if(option == 'rollOver'){
			if($(this).hasClass('active')){
				return;
			}else{
				return $(this).addClass('hover');
			};
		} else if(option == 'rollOut'){
			 return $(this).removeClass('hover');	
		};
	};
}
/* used in vmix, should be replaced with TOOLS functions */
$.fn.tabAction = function(){
	if($(this).hasClass('active')){
		return;
	}else{
		var ThisTabSet = $(this).parents('.mod');
		ThisTabSet.find('.tab').removeClass('active');
		$(this).removeClass('hover');
		$(this).addClass('active');
		ThisTabSet.find('.tab-content').hide();
		ThisTabSet.find('.tab-content').eq(ThisTabSet.find('.tab').index(this)).fadeIn("slow");	
	};
}
$.fn.duplicate = function(count, cloneEvents) {
	var tmp = [];
	for ( var i = 0; i < count; i++ ) {
		$.merge( tmp, this.clone( cloneEvents ).get() );
	}
	return this.pushStack( tmp );
};
/**
hoverIntent r5 // 2007.03.27 // jQuery 1.1.2
<http://cherne.net/brian/resources/jquery.hoverIntent.html>
@author    Brian Cherne <brian@cherne.net>
**/
$.fn.hoverIntent = function(f,g) {
	// default configuration options
	var cfg = {
		sensitivity: 7,
		interval: 100,
		timeout: 0
	};
	// override configuration options with user supplied object
	cfg = $.extend(cfg, g ? { over: f, out: g } : f );

	// instantiate variables
	// cX, cY = current X and Y position of mouse, updated by mousemove event
	// pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
	var cX, cY, pX, pY;

	// A private function for getting mouse position
	var track = function(ev) {
		cX = ev.pageX;
		cY = ev.pageY;
	};

	// A private function for comparing current and previous mouse position
	var compare = function(ev,ob) {
		ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
		// compare mouse positions to see if they've crossed the threshold
		if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
			$(ob).unbind("mousemove",track);
			// set hoverIntent state to true (so mouseOut can be called)
			ob.hoverIntent_s = 1;
			return cfg.over.apply(ob,[ev]);
		} else {
		// set previous coordinates for next time
			pX = cX; pY = cY;
			// use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
			ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
		}
	};

	// A private function for delaying the mouseOut function
	var delay = function(ev,ob) {
		ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
		ob.hoverIntent_s = 0;
		return cfg.out.apply(ob,[ev]);
	};

	// A private function for handling mouse 'hovering'
	var handleHover = function(e) {
		// next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
		var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
		while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
		if ( p == this ) { return false; }

	// copy objects to be passed into t (required for event object to be passed in IE)
		var ev = jQuery.extend({},e);
		var ob = this;

	// cancel hoverIntent timer if it exists
		if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }

		// else e.type == "onmouseover"
		if (e.type == "mouseover") {
			// set "previous" X and Y position based on initial entry point
			pX = ev.pageX; pY = ev.pageY;
			// update "current" X and Y position based on mousemove
			$(ob).bind("mousemove",track);
			// start polling interval (self-calling timeout) to compare mouse coordinates over time
			if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}

		// else e.type == "onmouseout"
		} else {
			// unbind expensive mousemove event
			$(ob).unbind("mousemove",track);
			// if hoverIntent state is true, then call the mouseOut function after the specified delay
			if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
		}
	};

	// bind the function to the two event listeners
	return this.mouseover(handleHover).mouseout(handleHover);
};