/*
 * jQuery Cycle Lite Plugin
 * http://malsup.com/jquery/cycle/lite/
 * Copyright (c) 2008 M. Alsup
 * Version: 1.0 (06/08/2008)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Requires: jQuery v1.2.3 or later
 */
;(function($) {

var ver = 'Lite-1.0';

$.fn.cycle = function(options) {
    return this.each(function() {
        options = options || {};
        
        if (this.cycleTimeout) clearTimeout(this.cycleTimeout);
        this.cycleTimeout = 0;
        this.cyclePause = 0;
        
        var $cont = $(this);
        var $slides = options.slideExpr ? $(options.slideExpr, this) : $cont.children();
        var els = $slides.get();
        if (els.length < 2) {
            if (window.console && window.console.log)
                window.console.log('terminating; too few slides: ' + els.length);
            return; // don't bother
        }

        // support metadata plugin (v1.0 and v2.0)
        var opts = $.extend({}, $.fn.cycle.defaults, options || {}, $.metadata ? $cont.metadata() : $.meta ? $cont.data() : {});
            
        opts.before = opts.before ? [opts.before] : [];
        opts.after = opts.after ? [opts.after] : [];
        opts.after.unshift(function(){ opts.busy=0; });
            
        // allow shorthand overrides of width, height and timeout
        var cls = this.className;
        opts.width = parseInt((cls.match(/w:(\d+)/)||[])[1]) || opts.width;
        opts.height = parseInt((cls.match(/h:(\d+)/)||[])[1]) || opts.height;
        opts.timeout = parseInt((cls.match(/t:(\d+)/)||[])[1]) || opts.timeout;

        if ($cont.css('position') == 'static') 
            $cont.css('position', 'relative');
        if (opts.width) 
            $cont.width(opts.width);
        if (opts.height && opts.height != 'auto') 
            $cont.height(opts.height);

        var first = 0;
        $slides.css({position: 'absolute', top:0, left:0}).hide().each(function(i) { 
            $(this).css('z-index', els.length-i) 
        });
        
        $(els[first]).css('opacity',1).show(); // opacity bit needed to handle reinit case
        if ($.browser.msie) els[first].style.removeAttribute('filter');

        if (opts.fit && opts.width) 
            $slides.width(opts.width);
        if (opts.fit && opts.height && opts.height != 'auto') 
            $slides.height(opts.height);
        if (opts.pause) 
            $cont.hover(function(){this.cyclePause=1;}, function(){this.cyclePause=0;});

        $.fn.cycle.transitions.fade($cont, $slides, opts);
        
        $slides.each(function() {
            var $el = $(this);
            this.cycleH = (opts.fit && opts.height) ? opts.height : $el.height();
            this.cycleW = (opts.fit && opts.width) ? opts.width : $el.width();
        });

        $slides.not(':eq('+first+')').css({opacity:0});
        if (opts.cssFirst)
            $($slides[first]).css(opts.cssFirst);

        if (opts.timeout) {
            // ensure that timeout and speed settings are sane
            if (opts.speed.constructor == String)
                opts.speed = {slow: 600, fast: 200}[opts.speed] || 400;
            if (!opts.sync)
                opts.speed = opts.speed / 2;
            while((opts.timeout - opts.speed) < 250)
                opts.timeout += opts.speed;
        }
        opts.speedIn = opts.speed;
        opts.speedOut = opts.speed;

 		opts.slideCount = els.length;
        opts.currSlide = first;
        opts.nextSlide = 1;

        // fire artificial events
        var e0 = $slides[first];
        if (opts.before.length)
            opts.before[0].apply(e0, [e0, e0, opts, true]);
        if (opts.after.length > 1)
            opts.after[1].apply(e0, [e0, e0, opts, true]);
        
        if (opts.click && !opts.next)
            opts.next = opts.click;
        if (opts.next)
            $(opts.next).bind('click', function(){return advance(els,opts,opts.rev?-1:1)});
        if (opts.prev)
            $(opts.prev).bind('click', function(){return advance(els,opts,opts.rev?1:-1)});

        if (opts.timeout)
            this.cycleTimeout = setTimeout(function() {
                go(els,opts,0,!opts.rev)
            }, opts.timeout + (opts.delay||0));
    });
};

function go(els, opts, manual, fwd) {
    if (opts.busy) return;
    var p = els[0].parentNode, curr = els[opts.currSlide], next = els[opts.nextSlide];
    if (p.cycleTimeout === 0 && !manual) 
        return;

    if (manual || !p.cyclePause) {
        if (opts.before.length)
            $.each(opts.before, function(i,o) { o.apply(next, [curr, next, opts, fwd]); });
        var after = function() {
            if ($.browser.msie)
                this.style.removeAttribute('filter');
            $.each(opts.after, function(i,o) { o.apply(next, [curr, next, opts, fwd]); });
        };

        if (opts.nextSlide != opts.currSlide) {
            opts.busy = 1;
            $.fn.cycle.custom(curr, next, opts, after);
        }
        var roll = (opts.nextSlide + 1) == els.length;
        opts.nextSlide = roll ? 0 : opts.nextSlide+1;
        opts.currSlide = roll ? els.length-1 : opts.nextSlide-1;
    }
    if (opts.timeout)
        p.cycleTimeout = setTimeout(function() { go(els,opts,0,!opts.rev) }, opts.timeout);
};

// advance slide forward or back
function advance(els, opts, val) {
    var p = els[0].parentNode, timeout = p.cycleTimeout;
    if (timeout) {
        clearTimeout(timeout);
        p.cycleTimeout = 0;
    }
    opts.nextSlide = opts.currSlide + val;
    if (opts.nextSlide < 0) {
        opts.nextSlide = els.length - 1;
    }
    else if (opts.nextSlide >= els.length) {
        opts.nextSlide = 0;
    }
    go(els, opts, 1, val>=0);
    return false;
};

$.fn.cycle.custom = function(curr, next, opts, cb) {
    var $l = $(curr), $n = $(next);
    $n.css({opacity:0});
    var fn = function() {$n.animate({opacity:1}, opts.speedIn, opts.easeIn, cb)};
    $l.animate({opacity:0}, opts.speedOut, opts.easeOut, function() {
        $l.css({display:'none'});
        if (!opts.sync) fn();
    });
    if (opts.sync) fn();
};

$.fn.cycle.transitions = {
    fade: function($cont, $slides, opts) {
        $slides.not(':eq(0)').css('opacity',0);
        opts.before.push(function() { $(this).show() });
    }
};

$.fn.cycle.ver = function() { return ver; };

// @see: http://malsup.com/jquery/cycle/lite/
$.fn.cycle.defaults = {
    timeout:       7000, 
    speed:         2000, 
    next:          null, 
    prev:          null, 
    before:        null, 
    after:         null, 
    height:       'auto',
    sync:          1,    
    fit:           0,    
    pause:         1,    
    delay:         0,    
    slideExpr:     null  
};

})(jQuery);
var g = {
	links: {
	'1':'http://greenspany.com/store/page103.html',
	'2':'http://greenspany.com/store/page103.html',
	'3':'contact-giftcertificate.php',
	'4':'services-specials.php',
	'5':'services-specials.php'
	}
};
var setUpHrefs = function() {
	$('a[href]').each( function() { 
		var $t = $(this), 
		h = this.href,
		loc = location.href;		
		if ( 
			h===loc || 
			((h.indexOf('index') > -1) && document.getElementById('index')) 
			) 
		{
			$t.addClass('cur').click(function(e) {
				e.preventDefault();
			});
		}
	});
}

function getBodyId() {
		return document.body.getAttribute('id');
	};
function showFAQ(_this,p) {
// if answ exist, delete the old, exit if clicked the existing
	if ($('body').attr('id') == 'faq') scroll(0,0);
	if ($('#cur').length == 1) {
		var f = false; if (p.is('.cur')) { f = true; }
		$('#faqQ p').removeClass('cur'); $('#cur').remove();
		if (f) {  return; }
		}
	// show the answ:
	var id = _this.substring(1);
	var t = $(document.getElementById(id)); // find corresp div(answ)
	$(t).find('h2').remove(); // remove h2 from it
	divT = document.createElement( 'div' );// create container for showing answ
	$(divT).attr('id','cur').html(t.html()).insertAfter(p).show('slow');//add id and text to it, then insert it after
	p.addClass('cur');
}	
if (getBodyId() == 'index') {
	var dif = ($('#rightCol').height()+112) - ($('#contentCol').height()+138);
	if( dif > 0) {
			$('#contentTwoWrap').height($('#contentTwoWrap').height()+dif-1);
		};

	var  imgTotal = 5, imgCount = 1, ctner = $('#contentSshowCtner'), $slideShowImgCount = $('#slideShowImgCount'),
	loadedImg = ctner.find('img')[0].getAttribute('alt');
	while (imgCount < imgTotal+1) {
		if (imgCount !== parseInt(loadedImg)){
			ctner.append('<img src="images/home_ss/'+imgCount+'.jpg" alt="'+imgCount+'">');
		}
		imgCount++;
	};
	
	function onAfter() {
		$('#cur').attr('id','');
		$slideShowImgCount.find('li:eq('+ctner.find('img').index($(this))+')').attr('id','cur');
	}
	ctner.cycle({
		speed: 2000,
		timeout: 3500,
		pause: 1,
		after: onAfter
	});
	ctner.click(function() {
		window.location = g.links[ctner.find('img:visible')[0].getAttribute('alt')];
	})
}

$('.ornHeader,h1').each(function() {
	$(this).html('<img class="ornLeft" alt="ornament" src="images/ornLeft.png">'+$(this).text()+'<img class="ornRight" alt="ornament" src="images/ornRight.png">');
	})

setUpHrefs();
	
if (getBodyId() == 'services-specials') {
	
	$('.oneSpecial').each(function() {
		var $this = $(this);
		if ( $this.height() > 250 ) { 
			var h = $this.height() - 250;
			$this.find('h3').css('padding-top','0')
			.end().find('img').css('padding-bottom',h);
		}
	})
};
if (getBodyId() == 'spa-calendar') {
	$('div.oneEvent').each( function() {
		var cd = new Date();
		var ed = new Date(this.getAttribute('title'));
		if (cd.getMonth() > ed.getMonth() || (cd.getMonth() == ed.getMonth() && cd.getDate() > ed.getDate())) {
			$(this).hide();
		}
	})
};
if (getBodyId() == 'spa-faq') {

	$('#faqQ a').click(function(e) {
		e.preventDefault();
/* 
			if ($('#faqQ a').index(this) == 3) {
				$('#contentRight').css({'height':'auto','min-height':$('#rightCol').height()});
				$('#contentWrap').css('height','auto');
			}
 */
		var p = $(this).parent(); 
		showFAQ($(this).attr('href'),p); // pass href of the a and it's parent as object
			
		})
};	
if ( getBodyId() == 'shop') {
	$('#content .onePage a').append('<span></span>');
};

if (getBodyId() == 'contact' || getBodyId() == 'contact-giftcertificate' || getBodyId() == 'contact-membership' || getBodyId() == 'contact-insurance') {
	if($("#form1")[0]) {
		$("#form1").validate({debug: false, focusInvalidElement: true});
	}
};
if (getBodyId() == 'contact-appointment') {
	$('.date-pick').datePicker();
}

