$('system-requirements').setStyle('margin-left', '11px');
$('options').setStyle('width', '163px');
//
function updateProduct( productid ) {
	// blink these out before we change.
	if ( $defined( $('product_header' ) ) ) { $('product_header').setOpacity( 0 ); }
	if ( $defined( $('tagline' ) )) { $('tagline').setOpacity( 0 ); }
	if ( $defined( $('gloryshot'))) { $('gloryshot').setOpacity( 0 ); }
	if ( $defined( $('cart_image'))){ $('cart_image').setOpacity(0 ); }
	if ( $defined( $('price_image'))){ $('price_image').setOpacity(0 ); }
	if ( $defined( $('product_text'))){ $('product_text').setOpacity( 0 ); }
	if ( $defined( $('video_url'))){ $('video_url').setOpacity( 0 ); }
	if ( $defined( $('system-requirements'))){ $('system-requirements').setOpacity( 0 ); }
	// also this one
	if ( ppclass.switchImageTimer ) {
		ppclass.switchImageTimer = $clear( ppclass.switchImageTimer );
		ppclass.switchImageTimer = ppclass.switchImage.periodical( 1000*ppclass.delay );
		ppclass.currentIndex=0;
		currentIndex=0;
	}
	pd.closeOverlay(); // is the product overlay showing? remove it.
	// remove it after we hide the image
	var thisProduct = coronaryInfo[productid];
	var hdr = new Asset.image( thisProduct.headerimage );
	$('product_header').replaceWith( hdr );
	hdr.setProperty( 'id', 'product_header' );
	hdr.setProperty( 'alt', thisProduct.name );

	var tag = new Asset.image( thisProduct.taglineimage );
	$('tagline').replaceWith( tag );
	tag.setProperty('id', 'tagline' );
	tag.setProperty('alt', 'Hyperdesk: Windows Vista Themes' );

	// these are the more recent version
	var ci = new Asset.image( thisProduct.cartimage );
	ci.setProperty('alt', '' );

	var pi = new Asset.image( thisProduct.price_image);
	pi.setProperty('alt', thisProduct.price );

	$('price_image').replaceWith( pi );
	pi.setProperty('id', 'price_image');
	// update links
	if ( thisProduct.oktogo == 0 ) {
		var cu = new Element( 'a', {
			'href' :'#',
			'title':'Not Available'} );
		cu.addEvent('click', function(e){e=new Event(e).stop(); });
	}
	else {
		var cu = new Element( 'a', {
			'href':'/addtocart.php?vid='+thisProduct.vid,
			'title':''});
	}
	if ( thisProduct.price == 0 ) {
		cu.setProperty( 'title', 'Download '+thisProduct.name );
	}
	else {
		cu.setProperty( 'title', 'Purchase '+thisProduct.name );
	}
	ci.injectInside( cu );
	$('cart_url').replaceWith( cu );
	ci.setProperty( 'id', 'cart_image');
	cu.setProperty( 'id', 'cart_url' );

	if ( parseInt( thisProduct.expandedcount ) > 0 ) {
		$('product_detail_link').setProperty( 'href', '/vista_details.php?p='+thisProduct.vid );
		$E('a', 'preview-options').setOpacity(1);
		$('product_detail_link').setOpacity(1);
		$('options').setOpacity(1);
	}
	else {
		$E('a', 'preview-options').setOpacity(0);
		$('product_detail_link').setOpacity(0);
		$('options').setOpacity(0);
	}

	// if we're coming from a textonly page, remove that.
	//
	if ( $defined( $('textonly') ) ){ $('textonly').remove();}
/*
	if ( $defined( $('product_detail_link') ) ){ 
		$('product_detail_link').setProperty( 'href', '/vista_details.php?p='+thisProduct.vid );
		$E('a', 'preview-options').setOpacity(1);
		$('product_detail_link').setOpacity(1);
		$('options').setOpacity(1);
	}
*/
	// update text.
	$('product_text').setHTML(thisProduct.text );
	$('system-requirements').setOpacity(0);
	// update variables that apply to functions on this page.
	if ( $type(thisProduct.requirements ) ) {
		if ( thisProduct.requirements.substr(-3)=='jpg' || thisProduct.requirements.substr(-3)=='png' || thisProduct.requirements.substr(-3)=='gif' ) {
			$('system-requirements').setOpacity(1);
		}
	}
	pd.requirements = thisProduct.requirements;
	pd.initialize( thisProduct );
	// pd.extras = thisProduct.extras;
	// preload those requirements, extras
	new Asset.image( thisProduct.requirements );
	// new Asset.image( thisProduct.extras );
	productPreviews = thisProduct.productimages;
	ppclass.slides=thisProduct.productimages;

	var gs=new Element('img');

	if ( $type(thisProduct.gloryshot) == 'element' ) {
		gs = thisProduct.gloryshot.clone(true);
	}
	else {
		gs = new Asset.image( thisProduct.productimages[0], {onComplete:function() {
			$('product_header').setOpacity( 1 );
			$('tagline').setOpacity( 1 );
			$('gloryshot').setOpacity( 1 );
			$('cart_image').setOpacity( 1 );
			$('price_image').setOpacity( 1 );
			}}); 
	}
	$('gloryshot').replaceWith( gs );
	gs.setProperty('id', 'gloryshot' );
	gs.setProperty('alt', thisProduct.name );
	$('product_text').setOpacity( 1 );
  if ( $type( thisProduct.video_url ) == 'string' ) {
    $('video_url').setProperty('href', thisProduct.video_url );
    $('video_url').setOpacity(1);
  }
  else {
    $('video_url').setOpacity(0);
  }

	return false;
}

var pd;
/* products extras and system requirements */
(function() {
	var ProductDetails = new Class({
		requirements: null,
		extras : null,
		bound : {},
		extTrigger : null,
		reqTrigger : null,
		initialize : function(options) {
			this.requirements = options.requirements;
			this.extras = options.extras;
			
			//preloading
			
//			this.extTrigger = $('extras-included');
			this.reqTrigger = $('system-requirements');
			
			this.bound.click = this.onClick.bindWithEvent(this);
			
      /*
			if ( this.extras.length > 0 ) {
				if ( this.extTrigger !== null) {
					var extImg = new Image();
					extImg.src = this.extras;			
					this.extTrigger.addEvent('click',this.bound.click);
				}	
			}
      */
			
			if ( $type( this.requirements ) && this.requirements.length > 0 ) {
				if (this.reqTrigger !== null) {
					var reqImg = new Image();
					reqImg.src = this.requirements;						
					this.reqTrigger.addEvent('click',this.bound.click);
					$('system-requirements').removeClass('hidden');
				}
				else {
					$('system-requirements').addClass('hidden');
				}
			}
			else {
				$('system-requirements').addClass('hidden');
			}
		},
		onClick : function(event) {
			var trigger = $(event.target);
			this.closeOverlay();
			switch (trigger.getProperty('id')) {
				case 'extras-included' : 
//					this.showOverlay(this.extras);
					break;
				case 'system-requirements' :
					this.showOverlay(this.requirements);
					break;
			}
			
			trigger.addClass('overlayActive');
      event.stop();
		},
		showOverlay : function(src) {
			var overlay = new Element('div', {
				'id' : 'overlay'
			});
			
			this.bound.close = this.closeOverlay.bindWithEvent(this);
			var a = new Element('a', {
				'id' : 'overlayClose',
				'href' : '#',
				'events' : {
				'click' : this.bound.close
				}
			});
			a.setText('close');
			
			var img = new Element('img',{
				'src' : src
			});
			img.injectInside(overlay);
			overlay.injectInside('big-shot');
			a.injectInside(overlay);
		},
		closeOverlay : function(event) {
			if (event) {
				event = new Event(event).stop();
			}
			var ovr = $('overlay');
			if (ovr !== null) {
				ovr.remove();
			}

			$ES('#options a').each(function(item){
				$(item).removeClass('overlayActive');
			});
		}
	});
	
	pd = new ProductDetails(productOverlays);
})();

///// 
var ProductPreviewsClass = new Class( {
	options: {
	},
	initialize: function( options ) {
		this.parseOptions( options );
		this.holder = $('gloryshot');
		this.delay = productPreviewTiming;
		this.currentIndex=0;
		this.instance();
	},
	switchImage: function( ) { 
		this.slides=productPreviews;
		if ( this.slides.length <2 ) {
			return;
		}
		this.currentIndex=currentIndex;
		var currentImg = $('gloryshot');
		var temp = currentImg.clone();
		temp.addClass('temporary');
		temp.injectInside('big-shot');
		this.currentIndex++;
		if ( this.currentIndex >= this.slides.length ) {
			this.currentIndex=0;
		}
		var newImage = this.slides[this.currentIndex];
		// currentImg is in front of temp.
		// bookmark
		currentImg.setProperty('src', newImage );
		var mfx = new Fx.Style(	temp,'opacity',	{
			duration : 1000,
			onComplete : function() {
				temp.remove();
			}
		});
			
	mfx.start(1,0);
	currentIndex=this.currentIndex;
},
	parseOptions: function( arr ) {
		arr.each( function( item) {
			var img=new Image();
			img.src = item;
		});
		this.slides = arr.copy(); 
	},
	instance : function( ) { this.switchImageTimer = this.switchImage.periodical( 1000*this.delay ) }
});

var currentIndex=0;
var ppclass;
window.addEvent('domready', function() {
	var moof=$('product_detail_link').getProperty('href');
	if ( moof == '' || moof==false ) {
		$('product_detail_link').setOpacity(0);
	}
	//showPage( currentpage );
	ppclass=new ProductPreviewsClass(productPreviews);
});

//---
function preloadImages( arr ) {
	arr.each( function( item ) {
		thisimage=new Asset.image( item );
		coronaryInfo.each( function( coronary ) {
			if  ( $type(coronary) && item == coronary.productimages[0]) {
				var thispid=coronary.vid
				coronaryInfo[thispid].gloryshot = thisimage;
			}
		});
	});
}
