//Carousel
(function($){
    $.fn.liteSlider= function(options) {

        var defaults = {
            content : '.content',
            width : 500,
            height : 250,
            autoplay : false,
			delay : 7,
            buttonsClass : '',
            activeClass : '',
			controlBt : '',
			playText : ' Play',
			pauseText : 'Pause'
        };
        
        var options = $.extend(defaults, options);

        var slideNo = 1;
        var timer = 0;
        var playStatus = options.autoplay;
        var thisClass = ($(this).attr('class'));
        var theClass = '.' + thisClass;
        var count = 0;
        var slides;
		var currentSlide = 1;
		var delay = parseInt(options.delay)*1000;
		
        $(this).children(options.content).each(function(){
            slides = ++count;
        });
		
		function wrapContent(ele){
			ele.wrap('<div class="sliderContentsWrap" />');
		}
		
		function applyCss(ele){
			$('.sliderContentsWrap').css({
                padding : 0,
                /*margin : 0,*/
                width : options.width,
                height : options.height,
                overflow : 'hidden',
                position : 'relative'
            });
            
            ele.css({
                padding : 0,
                margin : 0,
                width : options.width * slides,
                height : options.height,
                position : 'relative'
            });
            
            ele.children(options.content).css({
                float : 'left',
                width : options.width,
                height : options.height
            });
		}
		
        function resetButtons(){
            i = 0;
            $('.' + options.buttonsClass).each(function(){
                i++;
                $(this).addClass('bt' + i);
                $(this).attr('rel', i);
            });
        }
		        
        function goToSlide(theSlide){
		
            var animateLeft = -(options.width) * (parseInt(theSlide)-1); 
            $('.sliderContentsWrap' + ' ' + theClass)
                .animate({
                    left: animateLeft
                });
            
            $('.' + options.buttonsClass).each(function(){
                $(this).removeClass(options.activeClass);
                    if($(this).hasClass('bt' + theSlide)){
                        $(this).addClass(options.activeClass)}
            });
			
			currentSlide = theSlide;
        }
		
		function autoplay(){
			if(currentSlide < slides){
				goToSlide(parseInt(currentSlide) + 1);
			}else{
				goToSlide(1);
			}
		}
		
		function playSlide(){
			clearInterval(timer);
			timer = setInterval(function(){
				autoplay();
			}, delay);
			
			$(options.controlBt).text(options.pauseText);
			playStatus = true;
		}
		
		function pauseSlide(){
			clearInterval(timer);
			$(options.controlBt).text(options.playText);
			playStatus = false;
		}

		function init(ele){
			wrapContent(ele);
			applyCss(ele);
			resetButtons();
			
			if(options.autoplay == true){
				playSlide()
			}else{
				pauseSlide();
			}
		}
		
        return this.each(function(){
			init($(this));
			
			$('.'+options.buttonsClass).click(function(e){
				e.preventDefault();
				playSlide();
                goToSlide($(this).attr('rel'));
            });
			
			$(options.controlBt).click(function(e){
				e.preventDefault();
				if(playStatus == true){
					pauseSlide()
				}else{
					playSlide()
				} ;
            });
			
        });
        
    };
})(jQuery);

$('.fadein img:gt(0)').hide();
setInterval(function(){$('.fadein :first-child').fadeOut(2000).next('img').fadeIn(2000).end().appendTo('.fadein');}, 8000);

$(function(){
	$('.sliderWrap').liteSlider({
		content : '.sliderContent',
		width : 947,
		height : 390,
		autoplay : true,
		buttonsClass : 'buttons',
		activeClass : 'active',
		controlBt : 'input'
	});
});


//Tipsy
(function($) {
    $.fn.tipsy = function(opts) {

        opts = $.extend({fade: false, gravity: 'n'}, opts || {});
        var tip = null, cancelHide = false;

        this.hover(function() {
            
            $.data(this, 'cancel.tipsy', true);

            var tip = $.data(this, 'active.tipsy');
            if (!tip) {
                tip = $('<div class="tipsy"><div class="tipsy-inner">' + $(this).attr('title') + '</div></div>');
                tip.css({position: 'absolute', zIndex: 100000});
                $(this).attr('title', '');
                $.data(this, 'active.tipsy', tip);
            }
            
            var pos = $.extend({}, $(this).offset(), {width: this.offsetWidth, height: this.offsetHeight});
            tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).appendTo(document.body);
            var actualWidth = tip[0].offsetWidth, actualHeight = tip[0].offsetHeight;
            
            switch (opts.gravity.charAt(0)) {
                case 'n':
                    tip.css({top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tipsy-north');
                    break;
                case 's':
                    tip.css({top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tipsy-south');
                    break;
            }

            if (opts.fade) {
                tip.css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: 1});
            } else {
                tip.css({visibility: 'visible'});
            }

        }, function() {
            $.data(this, 'cancel.tipsy', false);
            var self = this;
            setTimeout(function() {
                if ($.data(this, 'cancel.tipsy')) return;
                var tip = $.data(self, 'active.tipsy');
                if (opts.fade) {
                    tip.stop().fadeOut(function() { $(this).remove(); });
                } else {
                    tip.remove();
                }
            }, 100);
            
        });

    };
})(jQuery);

$(function() { 
    $("#botFrame a").tipsy({gravity: 's'});
     
  });


//Fonts
Cufon.replace('h1, .title', {
		color: '-linear-gradient(#5da6f9, #2e6db8)',
		textShadow: '0px 1px #999',
		fontWeight: '700'
	});
	
Cufon.replace('h2', {
		color: '#3372bd',
		textShadow: '0px 1px #FFF',
		fontWeight: '400'
	});

Cufon.replace('.showcase h1', {
		color: '-linear-gradient(#5da6f9, #2e6db8)',
		textShadow: '0px 1px #333',
		fontWeight: '700'
	});

Cufon.replace('.showcase h2', {
		color: '-linear-gradient(#5da6f9, #2e6db8)',
		textShadow: '0px 1px #333',
		fontWeight: '400'
	});
Cufon.replace('#title h1', {
		color: '-linear-gradient(#fdfdfd, #afafaf)',
		textShadow: '0px 1px #333',
		fontWeight: '700'
	});	
	

	
//Lightbox
$(document).ready(function() {
	$('body').append('<div id="lbBackground" class="hide"></div><div id="lightbox" class="hide"><iframe src="/welcome/blank" id="galleryFrame" name="galleryFrame" scrolling="auto" frameborder="0" class="hide"></iframe><img src="#" alt="" class="hide" id="lbImage" /><a id="close" href="/close"><img src="/images/icons/close.png" alt="close" width="29" height="29" /></a></div>');
});

$('a.lightbox').click(function(){
		imgTypes = ['png', 'jpg', 'jpeg', 'gif'];
		$src = $(this).attr("href");
		//pageTracker._trackPageview($src);
		$('#lbBackground').fadeIn();
		$top = $(this).offset().top;
		$left = $(this).offset().left;
		
		if($(this).attr('rel').length > 0) {
			$d = $(this).attr('rel').split(",");
			$width = $d[0]; 
			$height = $d[1];
		
		}else {
			$height=550;
			$width=850;
		}
		imgRegExp = imgTypes.join('|');
    	imgRegExp = new RegExp('\.' + imgRegExp + '$', 'i');
		
		$fTop = $(document).scrollTop() + (($(window).height() - $height - 60) / 2);
		if ($src.match(imgRegExp)) {
			$('#lbImage')
			.attr('src', $src)
			.load(function(){
				var lbImage = new Image();
				lbImage.src = $src;
				$height = lbImage.height;
				$width = lbImage.width;
				
				if($(window).width() > $width) {
					$fLeft = ($(window).width() - $width - 60) / 2;
				}else{
					$fLeft = 10;	
				}
				$('#lbImage, #lightbox, #close').show(); 
				$('#lightbox').css({ 
					top:$top, 
					left:$left 
				}).animate({
					top:$fTop,
					left:$fLeft,
					width:$width,
					height:$height
				});
				
			});
			
		}else{	
			if($(window).width() > $width) {
				$fLeft = ($(window).width() - $width - 60) / 2;
			}else{
				$fLeft = 10;	
			}
			$('#galleryFrame').attr({src:$src+'?lb'});
			$('#galleryFrame, #lightbox, #close').show();
			$('#galleryFrame').attr({style:'display:inline; width: '+$width+'px; height: ' +$height+'px'});
			$('#lightbox').css({ 
				top:$top, 
				left:$left 
			}).animate({
				top:$fTop,
				left:$fLeft,
				width:$width,
				height:$height
			});

		}
		lb = true;
		return false;
	});

$('#lbBackground, #close').live('click', function(){
	$('#lightbox').animate({
		top:$top,
		left:$left,
		width:10,
		height:10
	}, function(){ $('#lightbox, #close, #galleryFrame, #lbImage').hide(); })
	$('#lbBackground').fadeOut();
	$('#galleryFrame').attr({src:'/blank.htm'});
	lb = false;
	return false;
});
