window.addEvent('domready', function(){ 
	if($chk($('slider-stage'))){
		var test = new ClickRotate();
		var test2 = new AjaxInject();
	}
	if($chk($('logo'))){
		$('logo').setStyle('cursor','pointer');
		$('logo').addEvent('click',function(){window.location='/';});
	}
	if($chk($('capquest'))){
		$('capquest').set('text',unescape('%74%79%70%65%20%74%68%65%20%77%6F%72%64%20%22%6E%69%73%73%61%6E%22%20%77%69%74%68%6F%75%74%20%71%75%6F%74%65%73%20%69%6E%20%74%68%65%20%62%6F%78%20%62%65%6C%6F%77'));
	}
	if($chk($('cc-stage'))){
		var startratator = new ClickRotate({stage: 'cc-stage',sliders: 'cc-list',transition:'scroll',autorotate:false,returnrotate:false});
	}
});

window.addEvent('load',function(){
	if($chk($('mainimagestage'))){
		var im = new CustomerCarImages();
	}
});
function closevideo(){
	$('videoblackbackgrd').dispose();
	$('videoiconsection').dispose();
	$('videobackgrd').dispose();
}
function showvideo(id){
	var blackbackground = new Element('div',{id:'videoblackbackgrd'}).inject($(document.body));
	blackbackground.setStyles({
		'height':$(document.body).getScrollSize().y+'px',
		'width':$(document.body).getScrollSize().x+'px',
		'background-color':'#000000',
		'opacity':0.7,
		'position':'absolute',
		'left':0,
		'top':0,
		'z-index':2001
	});
	var imgdiv = new Element('div',{styles:{
		'height':71,
		'width':400,
		'opacity':0.2,
		'position':'absolute',
		'bottom':20,
		'left':20,
		'z-index':2002},
		id:'videoiconsection'
	}).inject($(document.body));
	var img = new Element('img',{id:'videoiconimg',src:'gfx/IAlogolarge.gif'}).inject(imgdiv);
	var vidbackground = new Element('div',{styles:{
		'height':364,
		'width':445,
		'background-image':'url(gfx/videobackground.gif)',
		'background-repeat':'no-repeat',
		'position':'absolute',
		'padding':10,
		'left':(($(document.body).getSize().x/2)-(445/2)).toInt(),
		'top':(($(document.body).getSize().y/2)-(364/2)).toInt(),
		'z-index':2001},
		id:'videobackgrd'
	}).inject($(document.body));
	vidbackground.set('html','<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/'+id+'&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+id+'&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object><p style="text-align:right"><a onclick="closevideo();" href="#" style="color:#fff;font-weight:bold;">Close Video</a></p>');
	blackbackground.addEvent('click',function(){
		vidbackground.dispose();
		imgdiv.dispose();
		this.dispose();
	});
	
}

var CustomerCarImages = new Class({
	Implements: [Options],
	options: {
		linkclass:'.opener',
		stage:'mainimagestage',
		largeimage:'mainimage',
		duration:1000,
		startevent:'click'
	},
	initialize: function(options) {
		this.setOptions(options);
		var op = this.options;
		op.doevent=true;
		var holdingdiv = new Element('div',{'styles':{'height':0,'overflow':'hidden'}}).inject($(op.stage),'before');
		
		$(op.stage).setStyle('overflow','hidden');
		op.stagesize = $(op.stage).getSize().y;
		$(op.stage).setStyle('height',op.stagesize);
		$$(op.linkclass).each(function(elm){
			elm.addEvent(op.startevent,function(e){
				e.stop();
				op.stagesize = $(op.stage).getSize().y;
				
				if(op.doevent){
					var previousimage = $(op.largeimage).getSize().y;
					holdingdiv.empty();
					var myFx3 = new Fx.Tween($(op.largeimage),{duration:op.duration,link:'chain','onComplete':function(){
							op.doevent=true;
							previousimage = $(op.largeimage).getSize().y;
						}
					});
					
					var myFx = new Fx.Tween($(op.largeimage),{duration:op.duration,link:'chain','onComplete':function(){
							var myEl = new Element('img',{
								'src':elm.getProperty('href'),
								'events': {
									'load': function(){
										$(op.largeimage).setProperty('src',this.getProperty('src'));
										if(previousimage != this.getSize().y){
											var myFx2 = new Fx.Tween($(op.stage),{duration:(op.duration/2),link:'chain','onComplete':function(){
													myFx3.start('margin-top',0);
												}
											}).start('height',this.getSize().y);
										}else{
											myFx3.start('margin-top',0);
										}
									}
								}
							}).inject(holdingdiv);																  
						}
					});
					
					op.doevent=false;
					myFx.start('margin-top', op.stagesize+($(op.stage).getStyle('padding-top').toInt()+$(op.stage).getStyle('padding-bottom').toInt()));
				}
			});
		});
	}
});


var AjaxInject = new Class({
	Implements: [Options],
	options: {
		linkclass:'.menuajax',
		injectinto:'menuload',
		duration:1000,
		startevent:'click'
	},
	initialize: function(options) {
		this.setOptions(options);
		var opai = this.options;
		var f = this;
		var inject=true;
		var sizediv = new Element('div',{styles:{'height':0,'width':'100%','overflow':'hidden'}}).inject(document.body);
		var myFx = new Fx.Tween($(opai.injectinto),{
			duration:opai.duration,
			transition:Fx.Transitions.Bounce.easeOut,
			link:'chain',
			onStart:function(){
				if(inject){
					var content = sizediv.get('html');
					sizediv.empty();
					$(opai.injectinto).set('html',content);
					//I HATE YOU IE !!!!!!!!
					if($chk($('startscript'))) eval($('startscript').get('html'));
				}
			}
		});
		

		if($chk($(opai.injectinto))){
			opai.startheight = $(opai.injectinto).getSize().y;
			$$(opai.linkclass).each(function(elm){
					elm.addEvent(opai.startevent,function(e){
						e.stop();
						if ($(opai.injectinto).getSize().y>0){
							inject=false;
							myFx.start('height', 0);
						}
						var myHTMLRequest = new Request.HTML({noCache:true,onSuccess:function(responseTree, responseElements, responseHTML, responseJavaScript){
							sizediv.set('html',responseHTML);
							inject=true;
							myFx.start('height', sizediv.getScrollSize().y);
						}
						}).get(elm.getProperty('href'));

					});
			});
		}
	}
});


var ClickRotate = new Class({
	Implements: [Options],
	options: {
		stage: 'slider-stage',
		sliders: 'slider-list',
		gotoclass: 'gotolink',
		gotoevent:'click',
		next:'next',
		nextevent:'click',
		transition:'fade',
		transitionduration:2000,
		clickduration:250,
		previous:'previous',
		previousevent:'click',
		autorotate:true,
		waittime:8000,
		returnrotate:true,
		returnafter:5000,
		returnid:null,
		intervalID:null,
		useactivebuttons:false,
		onbutton:'/gfx/splash_on.gif',
		offbutton:'/gfx/splash_off.gif',
		buttonsinject:'splashbuttons'
	},
	initialize: function(options) {
		this.setOptions(options);
		var op = this.options;
		var f = this;
		op.children = $(op.sliders).getChildren('li');
		
		if (op.transition == "fade"){
			var startloop = 1000+op.children.length;
			for(i=0;i<op.children.length;i++){
				op.children[i].setStyle("z-index",startloop);
				startloop--;
			}
		}
		if(op.useactivebuttons){
			op.clickbuttons = Array();
			op.currentbuttonid=0;
			for(i=0;i<op.children.length;i++){
				if(i==0){
					op.clickbuttons[i] = new Element('img', {'src': op.onbutton});
				}else{
					op.clickbuttons[i] = new Element('img', {'src': op.offbutton});
				}
				op.clickbuttons[i].inject($(op.buttonsinject));
			}
		}
		
		if(op.autorotate) op.intervalID = this.autorotate.periodical(op.waittime,this);
		if($type($(op.next))=='element'){
			$(op.next).addEvent(op.nextevent,function(e){
				e.stop();
				if($type(op.returnid)=='number') $clear(op.returnid);
				if($type(op.intervalID)=='number'){
					$clear(op.intervalID);
					if(op.returnrotate) op.returnid = f.autorotate.delay(op.returnafter,f);
				}
				var children = $(op.sliders).getChildren('li');
				if (op.transition == "fade"){
					children[0].set('tween',{duration:op.clickduration,onComplete:function(){
						children[0].setStyle("z-index",0).inject($(op.sliders)).setStyle('opacity',1);
						var children2 = $(op.sliders).getChildren('li');
						var startloop = 1000+op.children.length;
						for(i=0;i<children2.length;i++){
							children2[i].setStyle("z-index",startloop);
							startloop--;
						}
					}});
					children[0].tween('opacity', 0);
				}else{
					children[0].set('tween',{duration:op.clickduration,onComplete:function(){children[0].inject($(op.sliders)).setStyle('margin-left',0);}});
					children[0].tween('margin-left', -children[0].getSize().x);
				}
			});
		}
		if($type($(op.previous))=='element'){
			$(op.previous).addEvent(op.previousevent,function(e){
				e.stop();
				if($type(op.returnid)=='number') $clear(op.returnid);
				if($type(op.intervalID)=='number'){
					$clear(op.intervalID);
					if(op.returnrotate) op.returnid = f.autorotate.delay(op.returnafter,f);
				}
				var children = $(op.sliders).getChildren('li');
				var last = children.length-1;
				if (op.transition == "fade"){
					children[last].set('tween',{duration:op.clickduration,onComplete:function(){children[last].setStyle('opacity',1);op.infade=false;}});
					children[last].setStyle('opacity',0).inject($(op.sliders),'top');
					var children2 = $(op.sliders).getChildren('li');
					var startloop = 1000+op.children.length;
					for(i=0;i<children2.length;i++){
						children2[i].setStyle("z-index",startloop);
						startloop--;
					}
					children[last].tween('opacity', 1);
				}else{
					children[last].set('tween',{duration:op.clickduration,onComplete:function(){children[last].set('style','');}});
					children[last].setStyle('margin-left',-children[last].getSize().x).inject($(op.sliders),'top');
					children[last].tween('margin-left', 0);
				}
			});
		}
		$$("a."+op.gotoclass).each(function(elm){
			var slideid = elm.getProperty('rel');
			if ($type(slideid)=="string"){
				slideid=slideid.toInt();
				elm.addEvent(op.gotoevent,function(e){
					e.stop();
					if($type(op.returnid)=='number') $clear(op.returnid);
					if($type(op.intervalID)=='number'){
						$clear(op.intervalID);
						if(op.returnrotate) op.returnid = f.autorotate.delay(op.returnafter,f);
					}
					var children = $(op.sliders).getChildren('li');
					if (op.transition == "fade"){
						op.children[slideid].set('tween',{duration:op.clickduration,onComplete:function(){
								op.children[slideid].setStyle('opacity',1);
								for(i=slideid+1;i<op.children.length;i++) op.children[i].inject($(op.sliders),'bottom');
								for(i=0;i<slideid;i++) op.children[i].inject($(op.sliders),'bottom');
								var children2 = $(op.sliders).getChildren('li');
								var startloop = 1000+op.children.length;
								for(i=0;i<children2.length;i++){
									children2[i].setStyle("z-index",startloop);
									startloop--;
								}
								op.infade=false;
						},onStart:function(){op.infade=true;}});
						op.children[slideid].setStyle('opacity',0).inject($(op.sliders),'top');
						var children2 = $(op.sliders).getChildren('li');
						var startloop = 1000+op.children.length;
						for(i=0;i<children2.length;i++){
							children2[i].setStyle("z-index",startloop);
							startloop--;
						}
						op.children[slideid].tween('opacity', 1);
					}else{
						op.children[slideid].set('tween',{duration:op.clickduration,onComplete:function(){
							op.children[slideid].set('style','');
							for(i=slideid+1;i<op.children.length;i++) op.children[i].inject($(op.sliders),'bottom');
							for(i=0;i<slideid;i++) op.children[i].inject($(op.sliders),'bottom');
						}});
						op.children[slideid].setStyle('margin-left',-op.children[slideid].getSize().x).inject($(op.sliders),'top');
						op.children[slideid].tween('margin-left', 0);
					}
				});
			}

		
		});
		
	},
			
	autorotate:function(){
		var op = this.options;
		if($type(op.returnid)=='number'){
			$clear(op.returnid);
			op.returnid=null;
			op.intervalID = this.autorotate.periodical(op.waittime,this);
		}
		var children = $(op.sliders).getChildren('li');
		
		var toupdate = op.currentbuttonid+1;
			var lasupdate = op.currentbuttonid;
			if(toupdate>children.length-1){
					lasupdate = children.length-1;
					toupdate=0;
					op.currentbuttonid=0;
			}else{
				op.currentbuttonid++;
			}
			if(op.useactivebuttons){
				op.clickbuttons[toupdate].src = op.onbutton;
				op.clickbuttons[lasupdate].src = op.offbutton;
			}
		
		if (op.transition == "fade"){
			children[0].set('tween',{duration:op.transitionduration,onComplete:function(){
				children[0].setStyle("z-index",0).inject($(op.sliders)).setStyle('opacity',1);
				var children2 = $(op.sliders).getChildren('li');
				var startloop = 1000+op.children.length;
				for(i=0;i<children2.length;i++){
					children2[i].setStyle("z-index",startloop);
					startloop--;
				}
			}});
			children[0].tween('opacity', 0);
		}else{
			children[0].set('tween',{duration:op.transitionduration,onComplete:function(){
				children[0].inject($(op.sliders)).setStyle('margin-left',0);
			}});
			children[0].tween('margin-left', -children[0].getSize().x);
		}
	}
		
	
});