/*
Copyright (c) 2007, Parosweb O.E. All rights reserved.
version: 2007-07-10
*/

////////////////////////////////////////////////////////////////////////////////
// Shortcuts ///////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
var $U = YAHOO.util;
var $D = YAHOO.util.Dom;
var $E = YAHOO.util.Event;


////////////////////////////////////////////////////////////////////////////////
// Details Object //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function SySlugDetails() {

	this.sDetailsId     = 'sy_details';

	// gallery ids
	this.sGalleryId     = 'sy_gallery';
	this.sThumbsId      = 'sy_thumbs';
	this.sThumbsSlideId = 'sy_thumbs_table';
	this.sImgId         = 'sy_img';
	this.sThumbsNavBack = 'sy_thback';
	this.sThumbsNavNext = 'sy_thnext';

	// contact ids
	this.sContactId     = 'sy_email';
	this.sContactForm   = 'sy_ocontact_form';
	this.sContactCId    = 'sy_ocontact_cancel';
	this.sContactSId    = 'sy_ocontact_submit';

	// loading
	this.sLoadId = 'sy_details_load';
	this.oLoad = false;
	this.sLoad =  '<img src="/interface/ic-indicator.gif" alt="" width="16" height="16" /><br />';
	this.sLoad += 'Loading ... Please wait.';
	this.sSucc =  '<img src="/interface/ic-indicator.gif" alt="" width="16" height="16" /><br />';
	this.sSucc += 'E-mail sent successfully.';

	// img loading
	this.sILoadId = 'sy_img_load';
	this.oILoad = false;
	this.sILoad =  '<img src="/interface/ic-indicator.gif" alt="" width="16" height="16" />';
}
SySlugDetails.version = '2007-07-10';
SySlugDetails.ident   = 'SySlugDetails';

// init ////////////////////////////////////////////////////////////////////////
SySlugDetails.prototype.init = function() {
	var o = this;
	o.galleryInit();
	$E.on( o.sContactId, 'click', o.showContact, o, true );
}

// galleryHandler //////////////////////////////////////////////////////////////
SySlugDetails.prototype.galleryInit = function() {
	var o = this;
	$E.on( o.sThumbsId, 'click', o.galleryHandler, o, true );
}
SySlugDetails.prototype.galleryHandler = function( e ) {
	var o = this;
	o.oImg = $D.get( o.sImgId );
	$E.stopEvent( e );
	var oE = $E.getTarget( e );
	while ( oE.id != o.sThumbsId ) {
		// thumbnail clicked on
		if( oE.nodeName.toUpperCase()=='IMG' ) {
			o.sSrc = oE.src;
			o.sSrc = o.sSrc.replace( /\-tn\.jpg/,'.jpg' );
			if ( o.oImg.src != o.sSrc ) {
				//var img = new Image();
				var img = document.createElement('img');
				o.showImgLoad();
				img.src = o.sSrc;
				$E.addListener( img, 'load', function() { 
					var t1 = setTimeout( function() {
						o.oImg.src = o.sSrc; 
						o.hideImgLoad();
					}, 300 );
				}, o, true );				
			}
			break;
		// thumbnail nav
		} else if( oE.nodeName.toUpperCase()=='A' && ( oE.id==o.sThumbsNavBack || oE.id==o.sThumbsNavNext ) ) {
			oTable = $D.get( o.sThumbsSlideId );
			var r = $D.getRegion( oTable );
			var w = r.right-r.left;
			var l = parseInt( $D.getStyle( oTable, 'left' ) );
			var Move = false;
			if ( oE.id==o.sThumbsNavBack && l<0 ) {
					Move = new $U.Anim( o.sThumbsSlideId, { left:{ from:l, to:(l+375) } }, 0.3, $U.Easing.easeOut );
					o.thNav( w, l+375 );	
			} else if ( oE.id==o.sThumbsNavNext && w>(Math.abs(l)+375) ) {
					Move = new $U.Anim( o.sThumbsSlideId, { left:{ from:l, to:(l-375) } }, 0.3, $U.Easing.easeOut );
					o.thNav( w, l-375 );
			}
			if ( Move ) {
				$E.removeListener( o.sThumbsId );
				Move.animate();
				Move.onComplete.subscribe( o.galleryInit, o, true );
			}
			break;
		} else {
			oE = oE.parentNode;
		}
	}
}
SySlugDetails.prototype.thNav = function( w, l ) {
	var o = this;
	if ( l==0 ) {
		$D.addClass( o.sThumbsNavBack, 'na' );
	} else {
		$D.removeClass( o.sThumbsNavBack, 'na' );
	}
	if ( ( Math.abs(l)+375 ) >= w ) {
		$D.addClass( o.sThumbsNavNext, 'na' );
	} else {
		$D.removeClass( o.sThumbsNavNext, 'na' );
	}
}
// ImgLoad /////////////////////////////////////////////////////////////////////
SySlugDetails.prototype.initImgLoad = function() {
	var o = this;
	o.oILoad = $D.get( o.sILoadId );
	if ( !o.oILoad ) {
		o.oILoad = document.createElement( 'div' );
		o.oILoad.id = o.sILoadId;
		o.oILoad.innerHTML = o.sILoad;
		$D.setStyle( o.oILoad,'display', 'none' );
		var img = $D.get( o.sImgId );
		o.appendElement( img, o.oILoad );
	}
}
// showImgLoad /////////////////////////////////////////////////////////////////
SySlugDetails.prototype.showImgLoad = function() {
	var o = this;
	o.initImgLoad();
	$D.setStyle( o.oILoad,'display', 'block' );
}
// hideImgLoad /////////////////////////////////////////////////////////////////
SySlugDetails.prototype.hideImgLoad = function() {
	var o = this;
	$D.setStyle( o.oILoad,'display', 'none' );
}

// showContact /////////////////////////////////////////////////////////////////
SySlugDetails.prototype.showContact = function( e ) {
	var o = this;
	$E.stopEvent( e );
	// prepare div
	var contact = $D.get( 'sy_ocontact' );
	if (!contact) {
		o.oContact = document.createElement( 'div' );
		o.oContact.id = 'sy_ocontact';
		o.oContact.innerHTML = '&nbsp;';
		$D.setStyle( o.oContact, 'display', 'none' );
		$D.addClass( o.oContact, 'cform' );
	}
	if (!o.oLoad) {
		o.oLoad = document.createElement( 'div' );
		o.oLoad.id = o.sLoadId;
		o.oLoad.innerHTML = o.sLoad;
		$D.setStyle( o.oLoad,'display', 'none' );
		o.appendElement( $D.get( o.sDetailsId ), o.oLoad );
	}
	var gallery = $D.get( o.sGalleryId );
	o.appendElement( gallery, o.oContact );
	// get url
	var oEmail = $D.get( o.sContactId );
	var sUrl = oEmail.href;
	sUrl = sUrl.replace( /\.html/,'-ax.html' );
	// display load
	o.oLoad.innerHTML = o.sLoad;
	var t1 = setTimeout( function() {
		$D.addClass( o.oLoad, 'dnav' );
		$D.setStyle( o.oLoad, 'display', 'block' );
	}, 100 );
	// start ajax
	var t2 = setTimeout( function() {  
		$U.Connect.asyncRequest( 'GET', sUrl, {
			success:o.onContactSuccess,
			failure:o.onContactFailure,
			scope:o
		} );
	}, 600 );
}
SySlugDetails.prototype.onContactSuccess = function( oAjax ) {
	var o = this;
	o.oContact.innerHTML = oAjax.responseText;
	
	if ( oAjax.responseText.match( /<\!--success/ ) ) { 
			o.oLoad.innerHTML = o.sSucc;
			var t1 = setTimeout( function() {
				$D.setStyle( o.oLoad, 'display', 'none' );
				$D.removeClass( o.oLoad, 'dnav' );
				$E.removeListener( o.sContactId );
				o.closeContact();
			}, 1000 );
			var t2 = setTimeout( function() {
				o.oLoad.innerHTML = o.sLoad;
			}, 1500 );
	} else {
		$D.setStyle( o.oLoad, 'display', 'none' );
		$D.removeClass( o.oLoad, 'dnav' );
		$D.setStyle( o.oContact, 'display', 'block' );
		$E.removeListener( o.sContactId );
		$E.on( o.sContactId, 'click', o.onContactClose, o, true );
		$E.on( o.sContactCId, 'click', o.onContactClose, o, true );
		$E.on( o.sContactSId, 'click', o.onContactSubmit, o, true );
	}
}
SySlugDetails.prototype.onContactFailure = function( oAjax ) {
	var o = this;
	o.closeContact();
	alert( 'failure' );
}
SySlugDetails.prototype.onContactClose = function( e ) {
	var o = this;
	$E.stopEvent(e);
	$E.removeListener( o.sContactId );
	o.closeContact();
}
SySlugDetails.prototype.closeContact = function() {
	var o = this;
	$D.setStyle( o.oContact, 'display', 'none' );
	o.oContact.innerHTML = '&nbsp;';
	$E.on( o.sContactId, 'click', o.showContact, o, true );
}

SySlugDetails.prototype.onContactSubmit = function( e ) {
	var o = this;
	$E.stopEvent(e);
	$E.removeListener( o.sContactSId );
	o.oContactForm = $D.get( o.sContactForm );
	var sUrl = o.oContactForm.action;
	sUrl = sUrl.replace( /\.html/,'-ax.html' );
	// display load
	var t1 = setTimeout( function() {
		$D.addClass( o.oLoad, 'dnav' );
		$D.setStyle( o.oLoad, 'display', 'block' );
	}, 100 );
	// start ajax
	$U.Connect.setForm( o.oContactForm );
	var t2 = setTimeout( function() {  
		$U.Connect.asyncRequest( 'POST', sUrl, {
			success:o.onContactSuccess,
			failure:o.onContactFailure,
			scope:o
		} );
	}, 600 );
}

// toggleClass /////////////////////////////////////////////////////////////////
SySlugDetails.prototype.toggleClass = function( oEl, sClass, act ) {
	if ( !act ) { 
		if ( $D.hasClass( oEl, sClass ) ) {
			$D.removeClass( oEl, sClass );
		} else {
			$D.addClass( oEl, sClass );
		}
	}
	else if ( act=='add' ) {
		$D.addClass( oEl, sClass );
	}
	else if ( act=='rem' ) {
		$D.removeClass( oEl, sClass );
	}
}
// appendElement ///////////////////////////////////////////////////////////////
SySlugDetails.prototype.appendElement = function( element, addElement ) {
    var father = element.parentNode;
    father.insertBefore( addElement, element.nextSibling );
}
// prependElement //////////////////////////////////////////////////////////////
SySlugDetails.prototype.prependElement = function( element, addElement ) {
    var father = element.parentNode;
    father.insertBefore( addElement, element );
}


////////////////////////////////////////////////////////////////////////////////
// Base Code ///////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
var oSlugDetails = new SySlugDetails();
$E.onDOMReady( oSlugDetails.init, oSlugDetails, true );

