animRunning = false;

addJavascript('joshBox', 'head');
addJavascript('viewers/genealogy', 'head');
addJavascript('viewers/downline', 'head');
addJavascript('viewers/matrix', 'head');
addJavascript('viewers/matrix2', 'head');
addJavascript('Forms/forms','head');

function addJavascript(jsname, pos) {
	document.write('<scr' + 'ipt type="text/javascript" src="/scripts/' + jsname + '.js" ></scr' + 'ipt>');
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function changeImageRotator() {
	imageRotatorRunning = true;
	imageRotatorTimer = window.setTimeout("changeImageRotator()", $speed + 1000);
	var $active = $('#imageRotator img.active');
	var $activeLi = $('#imageRotator ul li.active');
	if ($active.length == 0) {
		$active = $('#imageRotator div:last');
		$activeLi = $('#imageRotator ul li:last');
	}
	var $next = $active.next().length ? $active.next() : $('#imageRotator img:first');
	var $nextLi = $activeLi.next().length ? $activeLi.next() : $('#imageRotator ul li:first');
	$active.addClass('last-active').removeClass('active');
	$next.css({
		opacity: 0.0
	}).addClass('active').animate({
		opacity: 1.0
	},
	1000, function() {
		$active.removeClass('active last-active');
		imageRotatorRunning = false;
	});
/*
	$activeLi.animate({
		backgroundColor: '#111111'
	},
	1000, function() {
		$activeLi.removeClass('active');
	});
	$nextLi.animate({
		backgroundColor: '#eec900'
	},
	1000, function() {
		$nextLi.addClass('active');
	});
*/
}

function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i < vars.length; i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
	return false;
}

function popup(theURL,width,height) {
	var left = Math.round((screen.width - width)/2);
	var top = Math.round((screen.height - height)/2);
	window.open(theURL,'','width='+width+',height='+height+',location=no,menubar=no,toolbar=no,scrollbars=yes,resizeable=yes,left='+left+',top='+top);
	return false;
}

posFix = {
	selector: null,
	speed: 500,
	x: null,
	y: null,
	offset: null,
	minGap: 50,
	findPos: function(axis) {
		var obj = $(this.selector).get(0);
		var curtop = 0;
		var curLeft = 0;
		if (obj.offsetParent) {
			do {
				curtop += obj.offsetTop;
				curLeft += obj.offsetLeft;
			} while (obj = obj.offsetParent);
			if ('y' == axis) {
				return curtop;
			} else {
				return curLeft;
			}
		}
	},
	init: function(selector) {
		if (selector != '') {
			this.selector = selector;
		} else if (this.selector == null) {
			return false;
		}
		if ($(this.selector).get(0) === undefined) {
			return false;
		}
		this.y = this.findPos('y');
		this.x = this.findPos('x');
		$(this.selector).css({ position: "absolute", top: this.y + "px", left: this.x + "px" });
		$(window).scroll(function() {
			if (($(document).scrollTop() + posFix.minGap) > posFix.y) {
				posFix.offset = $(document).scrollTop() + posFix.minGap;
			} else {
				posFix.offset = posFix.y;
			}
			$(posFix.selector).animate({ top: posFix.offset + "px" }, { duration: posFix.speed, queue: false });
		});
		return true;
	}
};

(function ($) {
	var bcPosFix = function (element, settings) {
		var pos = { x: 0, y: 0 };
		var offset = 0;
		var scroll = 0;
		var scrollbase = 0;
		var $d = $(document);
		var windowHeight = $(window).height();
		var findPos = function () {
			var obj = element.get(0);
			if (obj.offsetParent) {
				var pos = { x: 0, y: 0 };
				do {
					pos.x += obj.offsetLeft;
					pos.y += obj.offsetTop;
				} while (obj = obj.offsetParent);
				return pos;
			}
		};
		var handleScroll = function () {
			scroll = $d.scrollTop();
			if (scroll < pos.y) { // not scrolled down far enough
				offset = pos.y;
			} else {
				var elHeight = element.outerHeight();
				if (scroll > scrollbase) { // going down
					if (windowHeight > (elHeight + (settings.minGap * 2))) { //window is big enough
						if ((scroll + settings.minGap) > 0) {
							offset = scroll + settings.minGap;
						} else {
							offset = 0;
						}
					} else {
						var bottom = offset + elHeight;
						if ((scroll + (settings.minGap * 2) + windowHeight) > bottom) {
							offset = (windowHeight + scroll) - elHeight;
						}
					}
				} else { // going up
					if (scroll < offset) {
						offset = scroll + settings.minGap;
					}
				}
			}
			if (settings.speed > 0) {
				element.animate({ top: offset }, { duration: settings.speed, queue: false });
			} else {
				element.css({ top: offset });
			}
			scrollbase = scroll;
		};
		pos = findPos();
		element.css({ position: 'absolute', top: pos.y, left: pos.x });
		$(window).scroll(function () {
			handleScroll();
		});
	};
	$.fn.bcPosFix = function (options) {
		var settings = {
			minGap: 0,
			speed: 500,
			calculatePosition: false,
			fixedHeight: true
		};
		if (options) {
			$.extend(settings, options);
		}
		return this.each(function () {
			var $el = $(this);
			dt = $el.text();
			if ($el.data('bcPosFix')) { return; } // Return early if this element already has a plugin instance
			var _bcPosFix = new bcPosFix($el, settings); // pass options to plugin constructor
			$el.data('bcPosFix', _bcPosFix); // Store plugin object in this element's data
		});
	};
})(jQuery);

function removeLoading(id) {
	if (id) {
		$('#' + id + ' .loading').remove();
	} else {
		$('.loading').remove();
	}
}

function makeCollagePopups() {
	$('div.collageCompetition div.entry a').click(function() {
		var entry = $(this).parents('div.entry');
		var content = '<h2>'+$(entry).find("h3").text()+'<span style="float: right; font-size: 10px;"><a href="javascript:hideJoshBox();">[X]</a></span></h2><img src="'+$(this).attr("href")+'" />';
		content += $(entry).find("div.desc").html();
		content += '<div class="field" style="clear: none; float: none; width: auto;">' + $(entry).find("div.field").html() + '</div>';
		var options = {
			hideOnBlur: 1,
			width: 760
		}
		showJoshBox(content,options);
//		alert();
		return false;
	});
}

function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href")) {
			var href = anchor.getAttribute("href");
			if ((href.indexOf('http') != -1) && (href.indexOf('itopproject.com') == -1)) {
				anchor.target = "_blank";
			}
		}
	}
}

function langSelector() {
	$('.languageSelector li a').each(function() {
		var href = $(this).attr('href');
		var lang = href.substring(href.indexOf('lang=')+5);
		$(this).click(function() {
			createCookie('lang',lang,'365');
//			return false;
		});
	});
}

$(document).ready(function() {
	if (null != document.getElementById("mainmenu")) {
		$('ul#mainmenu>li').bind('mouseover', function(e) {
			$(this).addClass('over');
		}).bind('mouseout', function(e) {
			$(this).removeClass('over');
		});
	}
	if (null != document.getElementById("memberDetails")) {
		$("#memberDetails").bcPosFix({speed: 200});
	}
	if (null != document.getElementById("packBuilderSummary")) {
		$("#packBuilderSummary").bcPosFix({speed: 200});
	}
	externalLinks();
	setupSettings();
	setupShipping();
	setupOrderPayment();
	setUpInputs();
	$('div.photoGallery ul').each(function() {
		$(this).find("a").lightBox();
	});
	$('.lightbox').each(function() {
		$(this).lightBox();
	});
	makeCollagePopups();
	if (null != document.getElementById('imageRotator')) {
		$speed = 4000;
		imageRotatorTimer = window.setTimeout("changeImageRotator()", $speed);
		imageRotatorRunning = false;
	}
	langSelector();
	$('.loginForm input').eq(0).focus();
});
