var myGallery

window.size = function() {
	var w = 0;
	var h = 0;

	if(!window.innerWidth) {
		if(!(document.documentElement.clientWidth == 0)) {
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		} else {
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	} else {
		w = window.innerWidth;
		h = window.innerHeight;
	}
	
	return {width:w,height:h};
}

function full(str, elem, name, nullvalue) {
	if(!name) name = '';
	if(!nullvalue) nullvalue = 0
	if (elem.value == nullvalue) {
		alert(str + (name != '' ? " " + name + "." : ''))
	    elem.focus();
	    return false;
	}
	return true;
}

function checkEmail(str, elem) {
	if (elem.value == 0) {
		alert(str);
	    elem.focus();
	    return false;
	} else {
		var re=/^[\w\.\_\+-]*@[\w\_\+-]+(\.[\w\_\+-]+)*\.[\w\+-]+$/;
		if (elem.value.search(re)==-1) {
			alert(str);
			elem.focus();
			elem.select();
			return false;
		}
		return true;
	}
}

function getNewXMLHttp() {
	xmlhttp = null;

    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			xmlhttp = new XMLHttpRequest();
        } catch(e) {
			xmlhttp = null;
        }
   
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		xmlhttp = null;
        	}
		}
    }
    
    return xmlhttp;
}

function startGallery() {
//	var w = window.size().width;
//	var h = window.size().height;
//	$('gallery').style.width = w + 'px';
//	$('gallery').style.height = h + 'px';
	
	myGallery = new gallery( $('gallery'), {
		timed: true,
		delay: 3500,
		fadeDuration: 750,
		showArrows: false,
		showCarousel: false,
		embedLinks: false,
		useHistoryManager: false,
		useThumbGenerator: false,
		showInfopane: false,
		slideInfoZoneSlide: false,
		titleSelector: "span",
		slideInfoZoneOpacity: 0,
		defaultTransition: "crossfade",
		textShowCarousel: 'toate imaginile',
		carouselMinimizedOpacity: 0.5,
		carouselMinimizedHeight: 20,
		carouselMaximizedOpacity: 0.9,
		thumbHeight: 75,
		thumbWidth: 100,
		thumbSpacing: 10,
		thumbIdleOpacity: 0.65,
		showCarouselLabel: false,
		thumbCloseCarousel: true,
		carouselHorizontal: true,
		activateCarouselScroller: true,
		carouselPreloader: true,
		textPreloadingCarousel: 'se incarca ...'
	} );
	$('gallery').style.visibility = 'visible';
	
}

var galleryAjax;
function loadGallery() {
	galleryAjax = getNewXMLHttp();
	galleryAjax.onreadystatechange = loadGalleryComplete;
	galleryAjax.open('GET', '/ro/gallery-images/' + screen.width + '/' + screen.height + '/', true);
	galleryAjax.send(null);
}

function loadGalleryComplete() {
	if(galleryAjax.readyState != 4) return;
	$('gallery').innerHTML = galleryAjax.responseText;
	startGallery();
}

var trackElem = null;
var trackTimeout = null;
function trackBtn(elemId) {
	if(trackElem == elemId) return;
	trackElem = elemId;
	$('tracker').style.visibility = 'visible';
	var left =  String($(elemId).getPosition($('mainmenu')).x) + "px";
	var width = String($(elemId).getWidth()) + "px";
	var trackerBgInMorph = new Fx.Morph ( 'tracker', { duration: 300, transition: Fx.Transitions.linear } );
	if(trackTimeout) clearTimeout(trackTimeout);
	trackTimeout = setTimeout( function() { trackerBgInMorph.start( { 'marginLeft': left, 'width': width } ); }, 50 );
}

function hideTracker() {
	$('tracker').style.visibility = 'hidden';
}

function showMenu(logo) {
	var trackerBgInMorph = new Fx.Morph ( 'mainmenu', { onComplete: function() { if(logo) showLogo() }, duration: 600, transition: Fx.Transitions.linear } );
	trackerBgInMorph.start( { 'bottom': -31 } );
}

function hideMenu() {
	var trackerBgOutMorph = new Fx.Morph ( 'mainmenu', { duration: 600, transition: Fx.Transitions.linear } );
	trackerBgOutMorph.start( { 'bottom': -120 } );
}

function showLogo() {
	var trackerBgInMorph = new Fx.Morph ( 'logo', { duration: 300, transition: Fx.Transitions.linear } );
	trackerBgInMorph.start( { 'top': 30 } );
}

function loadPage(handle, language, sessionId, isContentPage) {
	$('thepage-content-bg').style.backgroundPosition = isContentPage ? '0 0' : 'center';
	//alert($('thepage-content-bg').style.backgroundPosition);
	$('thepage-content-bg').style.backgroundImage = isContentPage ? 'url(/Assets/PageImages/' + handle + '.jpg)' : 'url(/Assets/JScripts/jdgallery/css/img/loading-bar-black.gif)';
	$('thepage-content-bg').innerHTML = '';
	$('text').innerHTML = '';
	$('text').style.backgroundImage = '';
	
	setTimeout( function () { showContent(handle, language, sessionId, isContentPage); }, 100);	
} 

function hideGallery() {
	myGallery.clearTimer();
	$('gallery').style.display = 'none';
}	

function showGallery() {
	myGallery.startSlideShow();
	$('gallery').style.display = 'block';
}

function showContent(handle, language, sessionId, isContentPage) {

	$('thepage-bg').style.display = 'block';
	$('thepage-top').style.display = 'block';
	$('thepage-bottom').style.display = 'block';
	$('thepage-content').style.display = 'block';
	
	hideGallery();
	hideMenu();
	
	var w = window.size().width;
	var m = -Math.round(w/2);
	
	var topLiftMorph = new Fx.Morph ( 'thepage-top', { duration: 700, transition: Fx.Transitions.linear } );
	var bottomLiftMorph = new Fx.Morph ( 'thepage-bottom', { duration: 700, transition: Fx.Transitions.linear } );
	var contentLiftMorph = new Fx.Morph ( 'thepage-content', { duration: 700, transition: Fx.Transitions.linear, onComplete: function() { loadContent(handle, language, sessionId, isContentPage);  $('thepage-close').style.visibility = 'visible'; $('disclaimer').style.visibility = 'visible';} } );
	
	var topExtendMorph = new Fx.Morph ( 'thepage-top', { 
		duration: 300,
		transition: Fx.Transitions.linear, 
		onComplete: function() { 
			topLiftMorph.start( { marginTop: -300 } ); 
		} 
	} );
	
	var bottomExtendMorph = new Fx.Morph ( 'thepage-bottom', { 
		duration: 300, 
		transition: Fx.Transitions.linear,
		onComplete: function() { 
			bottomLiftMorph.start( { marginTop: 270 } ); 
		} 
	} );
	
	var contentExtendMorph = new Fx.Morph ( 'thepage-content', { 
		duration: 300,
		transition: Fx.Transitions.linear, 
		onComplete: function() { 
			contentLiftMorph.start( { height: 540, marginTop: -270 } );
		} 
 		
	} );
	
	var pageBgInMorph = new Fx.Morph ( 'thepage-bg', { duration: 500 } );
	var topInMorph = new Fx.Morph ( 'thepage-top', { 
		duration: 300, 
		transition: Fx.Transitions.linear,
		onComplete: function() { 
			topExtendMorph.start( { width: w, marginLeft: m } ); 
		}
	} );
	var bottomInMorph = new Fx.Morph ( 'thepage-bottom', { 
		duration: 300, 
		transition: Fx.Transitions.linear,
		onComplete: function() { 
			bottomExtendMorph.start( { width: w, marginLeft: m } ); 
		} 
	} );
	var contentInMorph = new Fx.Morph ( 'thepage-content', { 
		duration: 300, 
		transition: Fx.Transitions.linear,
		onComplete: function() { 
			contentExtendMorph.start( { width: w, marginLeft: m } ) 
		} 
	} );

	pageBgInMorph.start( { opacity: [0, 0.9] } );
	
	//topInMorph.start( { opacity: 1 } );
	//bottomInMorph.start( { opacity: 1 } );
	//contentInMorph.start( { opacity: [1, 1] } );
	
	topExtendMorph.start( { width: w, marginLeft: m } );
	bottomExtendMorph.start( { width: w, marginLeft: m } ); 
	contentExtendMorph.start( { width: w, marginLeft: m } )
}

function hideContent() {
	if ( $('thepage-bg').style.display == 'none' ) return;
	
	var w = Math.round(window.size().width / 20);
	var m = -Math.round(w/2);
	
	$('thepage-close').style.visibility = 'hidden';
	$('disclaimer').style.visibility = 'hidden';
	if(bgRightMorph) bgRightMorph.cancel();
	if(bgLeftMorph) bgLeftMorph.cancel();
	
	var topOutMorph = new Fx.Morph ( 'thepage-top', {
		duration: 300,
		onComplete: function() { 
			$('thepage-top').style.display = 'none'; 
			bgOutMorph.start( {opacity: [0.9, 0] } );
			topLiftMorph.start( { marginTop: [0, 540] });
			submenuOut.start( { marginTop: [0, -540] } );
			showMenu(false);
			$('thepage-content-bg').innerHTML = '';
			//$('thepage-content-bg').style.backgroundImage = '';
			$('text').innerHTML = '';
			$('text').style.backgroundImage = '';
		 }
	} );
	
	var topCollapseMorph = new Fx.Morph ( 'thepage-top', {
		duration: 300,
		//onComplete: function() { topOutMorph.start( { opacity: [1, 0] } ); }
		onComplete: function() { 
			$('thepage-top').style.display = 'none'; 
			bgOutMorph.start( {opacity: [0.9, 0] } );
			topLiftMorph.start( { marginTop: [0, 540] });
			submenuOut.start( { marginTop: [0, -540] } );
			showMenu(false);
			showGallery();
			$('thepage-content-bg').innerHTML = '';
			//$('thepage-content-bg').style.backgroundImage = '';
			$('text').innerHTML = '';
			$('text').style.backgroundImage = '';
		}
	} );
	
	var topDescendMorph = new Fx.Morph ( 'thepage-top', {
		duration: 300,
		onComplete: function() { topCollapseMorph.start( { width: w, marginLeft: m } ); }
	} ); 
	
	var bottomOutMorph = new Fx.Morph ( 'thepage-bottom', {
		duration: 300,
		onComplete: function() { $('thepage-bottom').style.display = 'none'; }
	} );
	
	var bottomCollapseMorph = new Fx.Morph ( 'thepage-bottom', {
		duration: 300,
		//onComplete: function() { bottomOutMorph.start( { opacity: [1, 0] } ); }
		onComplete: function() { $('thepage-bottom').style.display = 'none'; }
	} );
	
	var bottomDescendMorph = new Fx.Morph ( 'thepage-bottom', {
		duration: 300,
		onComplete: function() { bottomCollapseMorph.start( { width: w, marginLeft: m } ); }
	} ); 
	
	var contentOutMorph = new Fx.Morph ( 'thepage-content', {
		duration: 300,
		onComplete: function() { $('thepage-content').style.display = 'none'; }
	} );
	
	var contentCollapseMorph = new Fx.Morph ( 'thepage-content', {
		duration: 300,
		//onComplete: function() { contentOutMorph.start( { opacity: [1, 0] } ); }
		onComplete: function() { $('thepage-content').style.display = 'none'; }
	} );
	
	var contentDescendMorph = new Fx.Morph ( 'thepage-content', {
		duration: 300,
		onComplete: function() { contentCollapseMorph.start( { width: w, marginLeft: m } ); }
	} ); 

	var bgOutMorph = new Fx.Morph ( 'thepage-bg', {
		duration: 300,
		onComplete: function() { $('thepage-bg').style.display = 'none'; }
	} );
	
	var topLiftMorph = new Fx.Morph ( 'text', { duration: 700, transition: Fx.Transitions.linear } );
	var submenuOut = new Fx.Morph( 'submenu', { duration: 700, transition: Fx.Transitions.linear } );
	topDescendMorph.start( { marginTop: -35 } );
	bottomDescendMorph.start( { marginTop: 5 } );
	contentDescendMorph.start( { marginTop: -5, height: 10 } );
}

var bgRightMorph;
var bgLeftMorph;

var bgMoveInterval;
var moveOffset = -1;
function movePageBg() { 
 	var pos = screen.width - 1600;
 	var offset = moveOffset > 0 ? Math.min( parseInt ( $('thepage-content-bg').style.backgroundPosition) + moveOffset, 0) : Math.max( parseInt ( $('thepage-content-bg').style.backgroundPosition) + moveOffset, pos);
 	$('thepage-content-bg').style.backgroundPosition = String ( parseInt ( $('thepage-content-bg').style.backgroundPosition) + moveOffset) + 'px';
 	
 	if(parseInt ( $('thepage-content-bg').style.backgroundPosition) >= 0 || parseInt ( $('thepage-content-bg').style.backgroundPosition) <= pos) {
 		moveOffset = -moveOffset;
 	}
 	setTimeout('movePageBg()', 75);
}

function moveBg() {
	var topLiftMorph = new Fx.Morph ( 'text', { duration: 500, transition: Fx.Transitions.linear, 
		onComplete: function(){ 
			var pos = screen.width - 1600;
			var duration = 9000;
			if(bgLeftMorph) bgLeftMorph.cancel();
			bgLeftMorph = new Fx.Morph ( 'thepage-content-bg', { 
				duration: duration,
				onComplete: function() {
					bgRightMorph.cancel();
					bgRightMorph.start( { 'background-position': 0 } );
				},
				transition: Fx.Transitions.linear 
			} );
			
			if(bgRightMorph) bgRightMorph.cancel();
			bgRightMorph = new Fx.Morph ( 'thepage-content-bg', { 
				duration: duration,
				onComplete: function() {
					bgLeftMorph.cancel();
					bgLeftMorph.start( { 'background-position': pos } );
				},
				transition: Fx.Transitions.linear 
			} );
			
			bgLeftMorph.start( { 'background-position': pos } );
		}
	} );
	topLiftMorph.start( { marginTop: [540, 0] });
	
	//movePageBg();
}

function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){

	var ie = navigator.appVersion.indexOf('MSIE') > 0;
	if(ie) {
		scrollbar.style.display = 'none';
		content.style.overflow = 'auto';
		content.style.overflowX = 'hidden';
		return;
	}
	
	// hide scrollbar if not needed
	if( content.getScrollSize().y <= content.getSize().y ) {
		scrollbar.style.display = 'none';
		return;
	}
	
	// resize the handle to give a more accurate impression
	handle.style.height = Math.round( content.getSize().y / content.getScrollSize().y * scrollbar.getSize().y ) + 'px';

	var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y))
	var slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	});
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);					
		});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}

var isContentPageEx;
var ajaxContent;
function loadContent(handle, language, sessionId, isContentPage) {
	isContentPageEx = isContentPage;

	if(isContentPage) { 
		moveBg();
		$('text').style.backgroundImage = 'url(/Assets/JScripts/jdgallery/css/img/loading-bar-black.gif)';
		$('text').innerHTML = '';
		loadSubmenu(handle, language, sessionId); 
	} else {
		$('thepage-content-bg').innerHTML = '';
		$('thepage-content-bg').style.backgroundPosition = 'center';
		$('thepage-content-bg').style.backgroundImage = 'url(/Assets/JScripts/jdgallery/css/img/loading-bar-black.gif)';
		$('thepage-content-bg').style.backgroundRepeat = 'no-repeat';
	}

	ajaxContent = getNewXMLHttp();
	str = "content=" + sessionId;;
	url = "/" + language + "/" + handle + "/";
	ajaxContent.open("POST", url, true);
	ajaxContent.onreadystatechange = loadContentComplete;
	ajaxContent.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajaxContent.send(str);
}

function loadContentComplete() {
	if(ajaxContent.readyState != 4) return;
	var content = ajaxContent.responseText;
	if(isContentPageEx) { 
		$('text').style.backgroundImage = '';
		$('text').innerHTML = content;
	} else {
		$('thepage-content-bg').style.backgroundImage = '';
		$('thepage-content-bg').innerHTML = content;
		$$('.gallery-thumb img').reflect();
		$$('.apt-thumb img').reflect();
		Shadowbox.setup('.gallery-thumb a');
		//swfobject.registerObject("flashContent", "9.0.0", "/Assets/JScripts/coverflow/expressInstall.swf");
	}
	makeScrollbar( $('div-page-content'), $('scrollbar'), $('scroll-handle'), false, false );
}

var ajaxSubmenu;
function loadSubmenu(handle, language, sessionId) {
	$('submenu').innerHTML = ''; 
	ajaxSubmenu = getNewXMLHttp();
	str = "submenu=" + sessionId;;
	url = "/" + language + "/" + handle + "/";
	ajaxSubmenu.open("POST", url, true);
	ajaxSubmenu.onreadystatechange = loadSubmenuComplete;
	ajaxSubmenu.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajaxSubmenu.send(str);
}

function loadSubmenuComplete() {
	if(ajaxSubmenu.readyState != 4) return;
	$('submenu').innerHTML = ajaxSubmenu.responseText;
	showSubmenu();	
}

function showSubmenu() {
	var submenuIn = new Fx.Morph( 'submenu', { duration: 500, transition: Fx.Transitions.linear } );
	submenuIn.start( { marginTop: [-540, 0] } );
}

function showSubmenuContent(id, pgTitle) {
	Shadowbox.init({});
	Shadowbox.open({
        content:    $(id).innerHTML,
        player: 	"html",
        title:      pgTitle,
        height:     600,
        width:      800,
        modal: 		true
    });
}

var currentAptOpen = null;
var currentAptExtended = null;

function aptHover ( elem, open ) {
	if ( currentAptExtended == elem ) return;
	if ( currentAptOpen != null && currentAptOpen != elem ) aptHover ( currentAptOpen, false );
	var aptCls = '.apt' + (open ? 'Hover' : 'Normal');
	//var trans = open ? Fx.Transitions.linear : Fx.Transitions.linear;
	var trans = Fx.Transitions.linear;
	var aptEffect = new Fx.Morph($('apt' + elem), { duration: 250, transition: trans, onComplete: function() { if(open) currentAptOpen = elem; makeScrollbar( $('div-page-content'), $('scrollbar'), $('scroll-handle'), false, false ); } });
	aptEffect.start(aptCls);
}

function aptExtend(elem, open) {
	if ( currentAptExtended != null && currentAptExtended != elem ) aptExtend( currentAptExtended, false );
	var aptCls = '.apt' + (open ? 'Extended' : 'Closed');
	var trans = open ? Fx.Transitions.linear : Fx.Transitions.linear;
	var aptEffect = new Fx.Morph($('apt' + elem), { duration: 300, transition: trans, onComplete: function() { if(open) currentAptExtended = elem; $('apt' + elem).style.cursor = open ? 'default' : 'pointer';  makeScrollbar( $('div-page-content'), $('scrollbar'), $('scroll-handle'), false, false ); } });
	aptEffect.start(aptCls);
}

function showAptPicture( file, pgTitle, w, h) {
	Shadowbox.init({});
	Shadowbox.open({
        content:    file,
        player: 	"img",
        title:      pgTitle,
        height:     h,
        width:      w,
        modal: 		true
    });
  }
  
function selected(param){
	showAptPicture( param, '', 800, 600);
}
