/*
 * Initialization
 */
var onloadFuncs = new Array();

function execOnloadFuncs(){
}

jQ(document).ready(function() {
	verifyVisit();
});

jQ(document).ready(function() {
	for(var i=0;i<onloadFuncs.length;i++)
		onloadFuncs[i]();
	
	jQ(".sitenav li a").hover(
		function() {
			jQ(this).parent().find("ul.subnav").slideDown('slow').show();
			jQ(this).parent().hover(
				function() {},
				function(){  
					jQ(this).parent().find("ul.subnav").slideUp('slow');
				}
			); 
		},
		function(){}
	);
});
/*
 * Common
 */

function deleteCookie( c_name ) {
	if ( getCookie( c_name ) ) {
		document.cookie = c_name + "=" + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}

function getCookie(c_name)
{
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");

		if (c_start!=-1) { 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);

			if (c_end==-1)
				c_end=document.cookie.length;
					
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
		
	return "";
}

function setCookie(c_name,value,expiredays)
{
	var pos = document.domain.indexOf('.');
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString()+";path=/;domain="+document.domain.substr(pos));
}

function hasSignedIn()
{
	var clogged_in = getCookie('logged_in');
	return (clogged_in && clogged_in > 0) ? true : false;
}

function toBeFan(ct, ce, sn)
{
	if (!hasSignedIn()) {
		if (!postLoginCommands) {
			postLoginCommands = new Array();
			postLoginCommands.push("toBeFan(" + ct + "," + ce + ",'" + sn + "')");
		}
		
		showSignInForm();
		return;
	}
	
	jQ.post(BU + 'main/abefan', { ct:ct, ce:ce }, function(json) {
		alert('Thank you for becoming a fan of ' + sn);
		jQ('#befan-'+ct+'-'+ce).css('display', 'none');
		
		if (json.eCode == 0) {
			var str = '';
			
			if (json.total_fans > 1)
				str = json.total_fans + ' fans';
			else if (json.total_fans > 0)
				str = json.total_fans + ' fan';
			
			jQ('#tfans-'+ct+'-'+ce).html(str);
		}
	}, "json");
}


var verifyVisitCounter = 0;

function verifyVisit()
{

	if (verifyVisitCounter > 0) return;
	
	verifyVisitCounter += 1;

	if (!hasSignedIn()) {
		var confirm_reg = getCookie('regconf');
		var today = new Date();
		var last_first_visit = getCookie('lfvisit');

		if (confirm_reg == '0') {
			var day_in_milli = 60000; // 2592000000 = 30 days // 86400 * 1000  (3600 = 1 hour  * 24hrs = 1day  * 1000 = milliseconds in a day)
			var since_last_first = today.getTime() - last_first_visit;
			//alert(since_last_first);
			/*if (since_last_first > day_in_milli) {
				pageTracker._trackEvent('newsletter', 'signup', 'hitlist');
				var elem = getModalDialog();
				document.getElementById('modal_content').appendChild(genEmailRegForm());
				if (jQ('#modal_dialog').css('display') == 'none')
					jQ('#modal_dialog').modal({onOpen: simpleModalOpen, onClose: simpleModalClose, position: ["10%",]});
			}*/
		} else if (confirm_reg == 1 || confirm_reg == 2) {
		} else {
			if (last_first_visit == '')
				setCookie('lfvisit', today.getTime(), 30);

			setCookie('regconf', '0', 30);
		}
	}
}

/*
 * Slide box
 */
function Slider(delay, root_id)
{
	this.delay = delay;
	this.root_id = root_id;
	this.root_elem = null;
	this.current_index = 0;
	this.next_index = 1;
	this.timer = 0;
	this.slides = new Array();
	this.dots = new Array();
	this.init = slideInit;
	this.forward = slideForward;
	this.backward = slideBackward;
	this.slide = slideNext;
	this.start = slideStart;
	this.slideto = slideTo;
	this.stop = slideStop;
	this.pause = slidePause;
}

function slideInit()
{
	var slides = jQ('#' + this.root_id + ' .slide');
	var dots = jQ('#' + this.root_id + ' .slide_dot');

	for (var i=0; i<slides.length; i++) {
		this.slides.push(jQ(slides[i]).css('z-index', String(slides.length-i)).css('position', 'absolute').hide());
		this.dots.push(jQ(dots[i]));
	}
}

function slideForward()
{
	var next_index = (this.current_index + this.slides.length + 1) % this.slides.length;
	this.slideto(this.next_index);
}

function slideBackward()
{
	var next_index = (this.current_index + this.slides.length - 1) % this.slides.length;
	this.slideto(next_index);
}

function slideNext()
{
	this.slides[this.current_index].fadeOut('slow');
	this.dots[this.current_index].attr('src', BIU + 'but-dot-white.png');
	this.slides[this.next_index].find('.slide_drop').hide();
	this.slides[this.next_index].find('.slide_info').hide();
	var nidx = this.next_index;
	this.dots[nidx].attr('src', BIU + 'but-dot-orange.png');
	this.current_index = this.next_index;
	this.next_index = (this.current_index + 1) % this.slides.length;
	this.slides[nidx].fadeIn('slow', function(){
		jQ(this).find('.slide_drop').slideDown('slow');
		jQ(this).find('.slide_info').hide().slideDown('show');
	});
}

function slideTo(n)
{
	if (n < 0 || n >= this.slides.length) {
		return false;
	}
	
    if (n != this.indexCurrent){
        this.pause(5000);
        this.next_index = n;
        this.slide();
    }
    
    return false;
}

function slideStart(n)
{
    //in ms
    this.timer = setInterval('slidebox.slide()',this.delay);
    
    //lets you start at some other slide
    if (typeof n == "number"){
        this.slideto(n);
    }
    
    return true;	
}

function slideStop()
{
    if ((typeof this.timer) == "number") {
    	clearInterval(this.timer);
    }
    
    return true;
}

function slidePause(msecs)
{
    this.stop();
    this.timer = setTimeout('slidebox.start()', msecs);
}

var slidebox = null;

function init_slider()
{
	slidebox = new Slider(8000, 'slide_bx');
	slidebox.init();
	slidebox.start(0);
}
/*
 * End slide box
 */
 
