var canclick="yes";
var x = 0;
var loginToggle = 1;
var inc = 400;
var currentChapter = '2D'
var onmovie = 'no';

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

var browserVer = BrowserDetect.browser + " " + BrowserDetect.version;

window.addEvent('domready', function(){
	
	
	
	
	if (typeof(quickChangePage) != 'undefined'){ 
		if(quickChangePage != 'nochange'){ quickChange(currentPage, quickChangePage); rotate = 0; }
	}


	var tabs = $$('.tab');		
	var tabs2 = $$('.tab2');
		
	var chapterThumbs = $$('.ChapterThumb');

	if (typeof(quickChangePage2) != 'undefined'){ 
	 	if(quickChangePage2 != 'nochange'){  
	 		quickChangeTwo(currentPage2, quickChangePage2); 
	 	}
	 }
	
	
	if (typeof(ipadpage) == 'undefined' || typeof(ipadpage) == 'null'){ 
			tabs.addEvents({
			   'click': function(){
			   		rotate = 0;
						if(currentPage != this.id && canclick =="yes"){ changePage(currentPage,this.id); }
			    }
			});
	}

	
	if (typeof(rotate) != 'undefined'){
		var rotation = function(){
			var x2 = x+1;
			if(rotate == 1){
				if(x2 == tabs.length){ x2=0; }
				changePage(tabs[x].id, tabs[x2].id);
				x = x2;
			}
		};
		var myInterval = rotation.periodical(6500);
	}

	tabs2.addEvents({
	   'click': function(){
			if(currentPage2 != this.id && canclick =="yes"){  changePage2(currentPage2,this.id); }
	    }
	});
	
	if (typeof(changeChapter) != 'undefined'){
		onmovie = 'yes';
	 	if(changeChapter != 'nochange'){  
	 		changeChapters(); 
	 	}
	 }
	
	chapterThumbs.addEvents({
	   'mouseenter': function(){ growPic(this.id); },
	   'mouseleave': function(){ shrinkPic(this.id); }
	});
	
	var loginbuttons = $$('.loginButton');
	
	loginbuttons.addEvents({
	   'click': function(e){
	   		e.stop();
			toggleLogin();
	    }
	});
	
	
});

function changeChapters(){
	var divtype = 'inline-table';
	if (browserVer == 'Explorer 7' || browserVer == 'Explorer 6'){ divtype = 'block'; }
	
	//alert(currentChapter + ' ' + isset('Chapters3D') + " " + isset('Chapters2D'))
	
	if(currentChapter == '2D' && isset('Chapters3D') == true && isset('Chapters2D') == true){
		$('Chapters3D').setStyles({ 'display': divtype });
		$('Chapters2D').setStyles({ 'display': 'none' });
		currentChapter = '3D';
	}
	else{
		if(currentChapter == '3D' && isset('Chapters3D') == true && isset('Chapters2D') == true){
			$('Chapters2D').setStyles({ 'display': divtype });
			$('Chapters3D').setStyles({ 'display': 'none' });
			currentChapter = '2D';
		}
	}
}


function growPic(ele){
	rel = $(ele).getParent();
	var less = inc - 1;
	$(ele).setStyles({ 'z-index': inc });
	rel.setStyles({ 'z-index': less });
	$(ele).set('morph', {duration: 300, transition: 'back:out'});
	$(ele).morph({
            	'left': '-30',
            	'top': '-14',
           	    'width': '177',
           	    'height': '94',
           	    'border-left-width': '4px',
           	    'border-right-width': '4px'
            });
    inc = inc+1;
}

function shrinkPic(ele){
	$(ele).set('morph', {duration: 300, transition: 'back:out'});
	$(ele).morph({
            	'left': '0',
            	'top': '0',
           	    'width': '115',
           	    'height': '62',
           	    'border-left-width': '2px',
           	    'border-right-width': '2px'
            });
}


function changePage(before, after){
	nameBefore = before + 'Box';
	nameAfter = after + 'Box';
	$(nameBefore).setStyles({ 'left': '0','margin-left': '0', 'top': '0', 'z-index':'11', 'opacity': '1' });
	$(nameAfter).setStyles({ 'left': '0','margin-left': '0', 'top': '0',  'z-index':'13', 'opacity': '.01' });
	

	if(onmovie == 'yes' && currentChapter == '2D' && after == 'movie3D'){ changeChapters(); }
	if(onmovie == 'yes' && currentChapter == '2D' && after == 'photos3D'){ changeChapters(); }
	if(onmovie == 'yes' && currentChapter == '3D' && after == 'movie2D'){ changeChapters(); }
	if(onmovie == 'yes' && currentChapter == '3D' && after == 'photos2D'){ changeChapters(); }
	
	if (typeof(hideOld) != 'undefined')
		 	{ if(hideOld == 'hide')
		 		{
					
					(function(){     $('box').scrollTop=0;    }).delay(250);

				}
		 	}
		 	
	$(nameBefore).set('tween', { duration: 700, transition: Fx.Transitions.linear }).tween('opacity', '.01');
	$(nameAfter).set('tween', { duration: 700, transition: Fx.Transitions.linear }).tween('opacity', '1');
/*
	(function(){
		$(nameBefore).setStyles({ 'left': '3000'});
	}).delay(700);
*/
	canclick="yes";
	currentPage = after;
	
	changeTab(before, after);
	changeTitle(before, after);	
}

function changePage2(before, after){
	nameBefore = before + 'Box';
	nameAfter = after + 'Box';
	$(nameBefore).setStyles({ 'left': '0', 'top': '0', 'z-index':'11', 'opacity': '1' });
	$(nameAfter).setStyles({ 'left': '0', 'top': '0', 'display': 'block', 'z-index':'13', 'opacity': '.01' });

	$(nameBefore).set('tween', { duration: 700, transition: Fx.Transitions.linear }).tween('opacity', '.01');
	$(nameAfter).set('tween', { duration: 700, transition: Fx.Transitions.linear }).tween('opacity', '1');

	currentPage2 = after;
	
	changeTab2(before, after);
}

function quickChange(before, after){
	nameBefore = before + 'Box';
	nameAfter = after + 'Box';
	$(nameBefore).setStyles({  'margin-left': '3000', 'visibility': 'visible', 'left': '0', 'top': '0', 'z-index':'11', 'opacity': '.01' });
	$(nameAfter).setStyles({  'margin-left': '0', 'visibility': 'visible', 'left': '0', 'top': '0', 'display': 'block', 'z-index':'13', 'opacity': '1' });
	currentPage = after;
	changeTab(before, after);
	$('title').innerHTML = tabTitles[after];
}
function quickChangeTwo(before, after){
	nameBefore = before + 'Box';
	nameAfter = after + 'Box';
	$(nameBefore).setStyles({ 'left': '0', 'top': '0', 'z-index':'11', 'opacity': '1' });
	$(nameAfter).setStyles({ 'left': '0', 'top': '0', 'display': 'block', 'z-index':'13', 'opacity': '.01' });

	$(nameBefore).set('tween', { duration: 700, transition: Fx.Transitions.linear }).tween('opacity', '.01');
	$(nameAfter).set('tween', { duration: 700, transition: Fx.Transitions.linear }).tween('opacity', '1');

	currentPage2 = after;
	
	changeTab2(before, after);
}

function changeTab(before, after){
	$(after).getElement('.activeColor').set('tween', { duration: 100, transition: Fx.Transitions.linear.easeInOut }).tween('opacity', '1');
	$(before).getElement('.activeColor').set('tween', { duration: 100, transition: Fx.Transitions.linear.easeInOut }).tween('opacity', '0');
	$(after).getElement('.tabProng img').set('tween', { duration: 100, transition: Fx.Transitions.linear.easeInOut }).tween('opacity', '1');
	$(before).getElement('.tabProng img').set('tween', { duration: 100, transition: Fx.Transitions.linear.easeInOut }).tween('opacity', '0');
	$(after).getElement('.tabProng').set('tween', { duration: 100, transition: Fx.Transitions.linear.easeInOut }).tween('top', '32');
	$(before).getElement('.tabProng').set('tween', { duration: 100, transition: Fx.Transitions.linear.easeInOut }).tween('top', '30');
	$(after).getElement('.tabText').set('tween', { duration: 100, transition: Fx.Transitions.linear.easeInOut }).tween('color', '#000');
	$(before).getElement('.tabText').set('tween', { duration: 100, transition: Fx.Transitions.linear.easeInOut }).tween('color', '#FFF');
	$(after).setStyles({ 'margin-top': '0px', 'height': '32px'});
	$(before).setStyles({ 'margin-top': '2px', 'height': '30px'});
	
}
function changeTab2(before, after){
	$(after).getElement('.activeColor2').set('tween', { duration: 100, transition: Fx.Transitions.linear.easeInOut }).tween('opacity', '1');
	$(before).getElement('.activeColor2').set('tween', { duration: 100, transition: Fx.Transitions.linear.easeInOut }).tween('opacity', '0');
	$(after).getElement('.tabProng2 img').set('tween', { duration: 100, transition: Fx.Transitions.linear.easeInOut }).tween('opacity', '1');
	$(before).getElement('.tabProng2 img').set('tween', { duration: 100, transition: Fx.Transitions.linear.easeInOut }).tween('opacity', '0');
	$(after).getElement('.tabProng2').set('tween', { duration: 100, transition: Fx.Transitions.linear.easeInOut }).tween('top', '32');
	$(before).getElement('.tabProng2').set('tween', { duration: 100, transition: Fx.Transitions.linear.easeInOut }).tween('top', '30');
	$(after).getElement('.tabText2').set('tween', { duration: 100, transition: Fx.Transitions.linear.easeInOut }).tween('color', '#000');
	$(before).getElement('.tabText2').set('tween', { duration: 100, transition: Fx.Transitions.linear.easeInOut }).tween('color', '#FFF');
	$(after).setStyles({ 'margin-top': '0px', 'height': '32px'});
	$(before).setStyles({ 'margin-top': '2px', 'height': '30px'});
}
	
function isset(varname)  {
	if(typeof( window[ varname ] ) != "undefined") return true;
	else return false;
}

function changeTitle(before, after){
	var y = new Chain();
	var one = function(){
		$('title').set('tween', { duration: 600, transition: Fx.Transitions.linear }).tween('opacity', '0');
	}
	var two = function(){
		$('title').innerHTML = tabTitles[after];
	};
	var three = function(){
		$('title').set('tween', { duration: 600, transition: Fx.Transitions.linear }).tween('opacity', '1');
	};
	y.chain(one);
	y.chain(two);
	y.chain(three);
	y.callChain();
	y.callChain.delay(600, y);
	y.callChain.delay(650, y);
}

function toggleLogin(){

	if (loginToggle == 0){
		$('loginBox').set('tween', { duration: 300, transition: Fx.Transitions.Quad.easeOut }).tween('top', '-105');
		loginToggle=1;
	}
	else{
		$('loginBox').set('tween', { duration: 250, transition: Fx.Transitions.Quad.easeOut }).tween('top', '-30');
		document.getElementById('unFocus').focus();
		loginToggle=0;
	}
}

function nextJoin(){
	var current = 'joinBox' + currentJoin;
	currentJoin = currentJoin + 1;
	var next = 'joinBox' + currentJoin;
	if(current=="joinBox1"){
		$("SelectMembershipImg").set('tween', { duration: 700, transition: Fx.Transitions.Cubic.easeOut }).tween('left', '-650');
		(function(){     $("selectimg").setStyles({ 'display': 'none' });    }).delay(700);
	
	}

	
	$(current).set('tween', { duration: 700, transition: Fx.Transitions.Cubic.easeOut }).tween('left', '-650');
	$(next).set('tween', { duration: 700, transition: Fx.Transitions.Cubic.easeOut }).tween('left', '0');

	(function(){    
		if(currentJoin == 2){
		document.getElementById('joinUsername').focus();
	}
	if(currentJoin == 4){
		document.getElementById('customer_fname').focus();
	
	}

	
	   }).delay(800);


	

}
	 
	
function prevJoin(){

	var current = 'joinBox' + currentJoin;
	currentJoin = currentJoin - 1;
	var next = 'joinBox' + currentJoin;
	//alert(next);

	if(current=="joinBox2"){
		 $("selectimg").setStyles({ 'display': 'block' });
		 $("SelectMembershipImg").set('tween', { duration: 700, transition: Fx.Transitions.Cubic.easeOut }).tween('left', '0');
	
	}

	$(current).set('tween', { duration: 700, transition: Fx.Transitions.Cubic.easeOut }).tween('left', '650');
	$(next).set('tween', { duration: 700, transition: Fx.Transitions.Cubic.easeOut }).tween('left', '0');
	}
	
	
	
function gotojoinpage(page){
	var current = 'joinBox1';
	var next = 'joinBox' + page;
	if(current=="joinBox1"){
		$("SelectMembershipImg").set('tween', { duration: 700, transition: Fx.Transitions.Cubic.easeOut }).tween('left', '-650');
		(function(){     $("selectimg").setStyles({ 'display': 'none' });    }).delay(700);
	
	}

	$(current).set('tween', { duration: 700, transition: Fx.Transitions.Cubic.easeOut }).tween('left', '-650');
	$(next).set('tween', { duration: 700, transition: Fx.Transitions.Cubic.easeOut }).tween('left', '0');
	}

