// JavaScript Document

var initVideo = function(video){
	if(correctFlashInstalled){
		var obj = new Swiff('/desireme/assets/flash/videoplayer.swf', {
			id: 'flashVideoObject',
			width: 552,
			height: 351,
			container: 'column01',
			params: {
				wmode: 'opaque',
				bgcolor: '#000000'
			},
			vars: {
				videoPath: video
			},
			callBacks: {
				//load: myOnloadFunc
			}
		});	
	}else if(Browser.Plugins.Quicktime){
		var qt = QT_GenerateOBJECTText_XHTML(video,
                        552, 351,             		// width & height
                        '',                         // required version of the ActiveX control, we're OK with the default value
                        'autoplay', 'true',         // autoplay the video?
						'loop', 'false',     		// Loop the video or not
                        'controller', 'true',      // show controller?
                        'enablejavascript', 'false',
                        'kioskmode', 'true',
                        'scale', 'tofit',           // scale to fit element size exactly so resizing works
                        'emb#id', 'qt_embed',    	// ID for embed tag only
                        'obj#id', 'qt_obj');     	// ID if container
		$('column01').set('html',qt)
	}
}

var setVideoLink = function(href,origin){
	
	initGalleryLinksReset();
	initVideoLinksReset();
	
	//alert(origin)
	
	if(origin){
		origin.set('class','videolinkactive')
		origin.set('rel','active')
	}
	
	if(correctFlashInstalled || Browser.Plugins.Quicktime){
		
		$('column01').fade('hide');
		
		initVideo(href);
		
		window.location.href = getSection(window.location.href)+'#'+relativeLink(href)
			
		$('column01').fade('in');
		
		return false
		
	}else{
		return true	
	}
	
}
var initVideoLinksReset = function(){
	
	var objs = $$('div#videolinks div');
	
	objs.each(function(item, index){
					   
		item.set('rel','inactive')
		item.set('class','videolink')
	});
	
}
var initVideoLinks = function(){
	
	var objs = $$('div#videolinks div.videolink');
	
	objs.each(function(item, index){
		var linkObj = item.getChildren('a')[0];
			if(correctFlashInstalled || Browser.Plugins.Quicktime){
				linkObj.addEvents({
					'click': function(){
						
						//setVideoLink(this.href);
						this.getParent().set('rel','active')
						
						return setVideoLink(this.href,this.getParent());
					},
					
					'mouseover': function(){
						
						this.getParent().set('class','videolinkactive')
						
						return false;
					},
					
					'mouseout': function(){
						//alert("rel:"+this.get('rel'))
						if(this.getParent().get('rel') != "active"){
							this.getParent().set('class','videolink')
						}
						
						return false;
					}
				});
			}else{
				item.set('target','_blank');
			}
	});
	
}

var setGalleryLink = function(href, title, origin){
	
	initGalleryLinksReset();
	initVideoLinksReset();
	
	if(origin){
		origin.getParent().set('class','galleryitemactive')
	}
	
	$('column01').fade('hide');
	$('column01').set('html','<img src="'+href+'" alt="'+title+'"  />');
	//$('column01').set('html','');
	
	window.location.href = getSection(window.location.href)+'#'+relativeLink(href)
		
	$('column01').fade('in');
	
}
var initGalleryLinksReset = function(){
	var objs = $$('div#gallery div');
	
	objs.each(function(item, index){
					   
		item.set('class','galleryitem')
		item.set('rel','inactive')
	});
	
}
var initGalleryLinks = function(){
	
	var objs = $$('div#gallery div.galleryitem');
	
	objs.each(function(item, index){
					   
		var linkObj = item.getChildren('a')[0];
		
			linkObj.addEvents({
				'click': function(){
					
					this.set('rel','active')
					setGalleryLink(this.href,this.title,this);
					
					return false;
				},
				
				'mouseover': function(){
					
					this.getParent().set('class','galleryitemactive')
					
					return false;
				},
				
				'mouseout': function(){
					
					if(this.get('rel') != "active"){
						this.getParent().set('class','galleryitem')
					}
					
					return false;
				}
			});
	});
	
}
var checkLinks = function(){
	var mediaItem = window.location.href.split('#')[1]
	if(mediaItem){
		if(mediaItem.indexOf('/desireme/assets/images/') != -1){
			setGalleryLink(mediaItem,'',$$('div#gallery div')[0]);
		}else if(mediaItem.indexOf('/desireme/assets/video') != -1){
			setVideoLink(mediaItem,$$('div#videolinks div')[0]);
		}
	}
}
window.addEvent('domready', function() {
				
	initGalleryLinks();
	initVideoLinks();
	//checkLinks();
	//setVideoLink('http://media.escada-fragrances.com/desireme/assets/video/desireme50s.m4v',$$('div#videolinks div')[0]);
	
});
