/**********************************************************************************
*                    atmananda.js (A compilation of great works)                  *
*                             A bkuberek Web Solution                             *
*                                                                                 *
*  @scope        Global                                                           *
*  @version      0.1                                                              *
*  @date         2008-04-12                                                       *
*  @url          http://www.atmananda.com                                         *
*  @author       Bastian Kuberek - webmaster@bkuberek.com                         *
*  @copyright    Copyright (C) 2008 Atmananda Yoga Sequence All rights reserved.  *
*                                                                                 *
**********************************************************************************/


///////////////////////////////////////////////////////////////////////////////////
//
//			GENERAL
//
///////////////////////////////////////////////////////////////////////////////////


function popup(t) {	
	if(t == "schedule" || t == "schedules") {
		target = "http://clients.mindbodyonline.com/ws.asp?studioid=1019&stype=-7";
		window_name = "schedule";
	}
	else {
		target = t;
		window_name = "popup";
	}
	
	popup = window.open(target,window_name,'width=900,height=600,toolbar=no,location=no,directories=no,resizable=yes,status=no,menubar=no,scrollbars=yes,left=150,top=10,screenX=200,screenY=30');
	if (window.focus) {
		popup.focus();
	}
}



///////////////////////////////////////////////////////////////////////////////////
//
//			CAROUSEL
//
///////////////////////////////////////////////////////////////////////////////////


Event.observe(window, 'load', function(){
	if($('photo-feature') != null) {
		new UI.Carousel(
			"photo-feature", {
				container		: ".pf_cont",
				scrollInc		: 2
			}
		);
	}
	
	if($('photo-slider') != null) {
		new UI.Carousel(
			"photo-slider", {
				container		: ".ps_cont",
				scrollInc		: 1
			}
		);
	}
	
});

/////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	                  MODAL FUNCTIONS
//////////	
/////////////////////////////////////////////////////////////////////////////////////////////////

Event.observe(window, 'load', function(){
	$$('.modal_img').each(function(link){
	    new Control.Modal(link,{
		        opacity: 0.9,
				loading: '/img/loading.gif'
		});
	});
});

///////////////////////////////////////////////////////////////////////////////////
//
//			GOOGLE MAP
//
///////////////////////////////////////////////////////////////////////////////////
var googlemap = Class.create();
googlemap.prototype = {
	initialize:function(el,trigger,address,geo,info) {
		this.address= address;
		this.geo	= geo;
		this.info	= info;
		this.el 	= $(el);
		this.trigger= $(trigger);
		this.setEvent();
	},
	setEvent:function() {
		Event.observe(
			this.trigger,
			'click',
			function(){
				Effect.toggle(
					this.el,
					'blind',{
						duration : 1,
						beforeStart: function() {
							if(!this.el.visible()) {
								Effect.toggle(
									$('contact-form'),
									'Blind',{
										duration: .5
									}
								);
								this.trigger.innerHTML = "Show Contact Form";
								$('heading-content').innerHTML = "Directions";
							}
						}.bind(this),
						afterFinish: function() {
							if(!this.el.visible()) {
								Effect.toggle(
									$('contact-form'),
									'Blind',{
										duration: .5
									}
								);
								this.trigger.innerHTML = "Show Map";
								$('heading-content').innerHTML = "Contact";
							}
							this.draw();
						}.bind(this)
					}
				);
			}.bindAsEventListener(this)
		);
		Event.observe(window,'unload',GUnload);
	},
	draw:function() {
		if(GBrowserIsCompatible()) {
			this.addAddressToMap();
		}
	},
	addAddressToMap:function() {
		this.map = new GMap2(this.el);
		this.geocoder = new GClientGeocoder();
		this.mapControl = new GMapTypeControl();
		this.geocoder.getLatLng(
			this.address,
			function(latlng) {
				if (!latlng) {
					if(!this.geo) {
						alert("you have passed invalid coordinates to the map");
						return;
					}
					this.point = new GLatLng(this.geo);
				}
				else {
					this.point = latlng;
				}
				this.map.setCenter(this.point, 16);
				this.marker = new GMarker(this.point);
				GEvent.addListener(
					this.marker,
					"click",
					function() {
					this.marker.openInfoWindowHtml(this.info);
					}.bind(this)
				);
				this.map.addOverlay(this.marker);
				this.map.addControl(this.mapControl);
				this.map.addControl(new GLargeMapControl());
			}.bind(this)
		);
	}
	
}


/////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	                  MODAL FUNCTIONS
//////////	
/////////////////////////////////////////////////////////////////////////////////////////////////

Event.observe(window, 'load', function(){
	$$('.modal_img').each(function(link){
	    new Control.Modal(link,{
		        opacity: 0.9,
				loading: '/img/loading.gif'
		    });
	});
	
	if($('loader')) {
		$('loader').hide();
	}
});

function showRegistration(){
	$('overlay').addClassName('screen');
    $('overlay').show();
    center('registration');
    return false;
}

function hideRegistration(){
    $('registration').hide();
    $('overlay').hide();
	if($('overlay').hasClassName('screen') == true) $('overlay').removeClassName('screen');
	valid.reset();
    return false;
}

function showLogin(){
	$('overlay').addClassName('screen');
    $('overlay').show();
    center('login');
    return false;
}

function hideLogin(){
    $('login').hide();
    $('overlay').hide();
	if($('overlay').hasClassName('screen') == true) $('overlay').removeClassName('screen');
    return false;
}

function showForgot(){
	$('overlay').addClassName('screen');
    $('overlay').show();
    center('forgot');
    return false;
}

function hideForgot(){
    $('forgot').hide();
    $('overlay').hide();
	if($('overlay').hasClassName('screen') == true) $('overlay').removeClassName('screen');
    return false;
}


function hideOverlay(){
	$('overlay').hide();
	if($('login')){
		if($('login').getStyle('display') != 'none') $('login').hide();
	}
	if($('forgot')){
		if($('forgot').getStyle('display') != 'none') $('forgot').hide();
	}
    if($('registration')){
		if($('registration').getStyle('display') != 'none') $('registration').hide();
		valid.reset();
	}
	if($('overlay').hasClassName('screen') == true) $('overlay').removeClassName('screen');
	return false;
}

function center(element){
    try{
        element = $(element);
    }catch(e){return;}

    var my_width  = 0;
    var my_height = 0;

    if ( typeof( window.innerWidth ) == 'number' ){
        my_width  = window.innerWidth;
        my_height = window.innerHeight;;
    }else if ( document.documentElement && 
             ( document.documentElement.clientWidth ||
               document.documentElement.clientHeight ) ){
        my_width  = document.documentElement.clientWidth;
        my_height = document.documentElement.clientHeight;
    }
    else if ( document.body && 
            ( document.body.clientWidth || document.body.clientHeight ) ){
        my_width  = document.body.clientWidth;
        my_height = document.body.clientHeight;
    }

    element.style.position = 'absolute';
    element.style.zIndex   = 1000;

    var scrollY = 0;

    if ( document.documentElement && document.documentElement.scrollTop ){
        scrollY = document.documentElement.scrollTop;
    }else if ( document.body && document.body.scrollTop ){
        scrollY = document.body.scrollTop;
    }else if ( window.pageYOffset ){
        scrollY = window.pageYOffset;
    }else if ( window.scrollY ){
        scrollY = window.scrollY;
    }

    var elementDimensions = Element.getDimensions(element);

    var setX = ( my_width  - elementDimensions.width  ) / 2;
    var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;

    setX = ( setX < 0 ) ? 0 : setX;
    setY = ( setY < 0 ) ? 0 : setY;

    element.style.left = setX + "px";
    element.style.top  = setY + "px";

    element.style.display  = 'block';
}

/////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////							OVERLAY SCROLL FIX
//////////	             CALCULATE CONTENT AREA FOR OVERLAY SCROLL
//////////	
/////////////////////////////////////////////////////////////////////////////////////////////////


function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {oldonload();}
            func();
        }
    }
}

addLoadEvent(
function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
                elem = document.getElementById("overlay");
                elem.style.height = yWithScroll + 'px';
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
                elem = document.getElementById("overlay");
                elem.style.height = yWithScroll + 'px';
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
                elem = document.getElementById("overlay");
                elem.style.height = yWithScroll + 'px';
  	}
}
);
