var slideshowTimer = null;
$(document).ready(function(){

    $('.header UL LI .search-form INPUT.form-text').focus(function(){
        if ($(this).val() == $(this).attr('title'))
            $(this).val('');
    });
    
    $('.header UL LI .search-form INPUT.form-text').blur(function(){
        if ($(this).val() == '' )
            $(this).val($(this).attr('title'));
    });
    
    
    
    $(window).resize(function(){
        if (isVisible($('.overlay'))) {
            $('.overlay').width($(document).width());
            $('.overlay').height($(document).height());
        }
    });
    
    $('a.popup-js').click(function(){
        $('.popup .center').load($(this).attr('href'),function(){
            $('.overlay').width($(document).width());
            $('.overlay').height($(document).height());
            $('.overlay').show();
            $('.popup').show();
            Cufon.set('fontFamily', 'Univers LT 47 CondensedLt').replace('.popup .more span.center')
            Cufon.set('fontFamily', 'Gotham Rounded Medium').replace('h4.fs20');
            $(".popup select").selectbox();
            initContactForm();
            $('.popup A.close, .overlay').click(function(){
                $('.overlay').hide();
                $('.popup').hide();
            });
        });
    
        return false;
    });

    $('.header ul li').hover(function(){
        $(this).find('a.list').css('color','#76ab10').addClass('active');
        $(this).find('a.service').css('color','#434343').removeClass('active').addClass('select');
        $(this).find('.submenu').show();
        $(this).find('.submenu-service').show();
    }, function(){        
        if ($(this).hasClass('active')){
            $(this).find('a.list').css('color','#76ab10'); 
        } else {
            $(this).find('a.list').css('color','#25aae1'); 
        }
        $(this).find('a.list').removeClass('active').removeClass('select'); 
        $(this).find('.submenu').hide();
        $(this).find('.submenu-service').hide();
        Cufon.set('fontFamily', 'Univers LT 47 CondensedLt').replace('.header ul li a.list', {hover: true});        
    });

    if($('#rotator').size() > 0){
		doSlideshow('rotator', true, 9000, 'nav');
    
        $('#nav li a').click(function(){
            var currObj = $('#rotator li.current');
            var nextObj = $('#rotator li#' + $(this).attr('rel') + '');  
            $('#nav li').removeClass('active');
            $(this).parent().addClass('active');
            
            nextObj.addClass('current');
            nextObj.css('opacity', 0).css('z-index','2');
            currObj.removeClass('current').css('z-index','1');
            currObj.animate({opacity:0}, 2000);
            nextObj.animate({opacity:1}, 2000);
            $('.button a').attr('href', nextObj.find('.hide').attr('href'));
            
            return false;
        });
    
	}    

});

function isVisible(obj) { return (obj.css('display') == 'block'); } 

function doSlideshow(objid, start, time, navId){
	if(!$('#' + objid) || !isVisible($('#' + objid))){
		if(slideshowTimer != null){
			clearTimeout(slideshowTimer);
			slideshowTimer = null;
		}
		return;
	}
	
	if(start){
		clearTimeout(slideshowTimer);
		slideshowTimer = null;
	}
	
	var currObj = $('#' + objid + ' li.current');
	var nextObj = currObj.next();
	if($('#' + objid + ' li:last').hasClass('current')){ 
		nextObj = $('#' + objid + ' li:first');
	}
    
    nextObj.addClass('current');
	nextObj.css('opacity', 0).css('z-index','2');
	currObj.removeClass('current').css('z-index','1');
	currObj.animate({opacity:0}, 2000);
	nextObj.animate({opacity:1}, 2000);
    $('.button a').attr('href', nextObj.find('.hide').attr('href'));
    
    if(navId != ''){        
        $('#' + navId + ' .'+currObj.attr('id')).removeClass('active');
        $('#' + navId + ' .'+nextObj.attr('id')).addClass('active');
    }
	
	slideshowTimer = setTimeout('doSlideshow("' + objid + '", false, ' + time + ',"' + navId + '");', time);
}

function initContactForm(){
	$('#contact').submit(function(){
		var form = $(this);
		var formData = $(this).serialize();
		var error = '';
        
        $('#error').slideUp('normal');
            $.ajax({
                type: 'POST', url: form.attr('action'), data: formData, 
                success: function ( responseData ){
                    if((error = Process(responseData)) != ''){
                        $('#error').html(error);
                        $('#error').css('color','#FF0000');
                        $('#error').slideDown('normal');
                    } else{
                        document.getElementById('contact').reset();
                        $('#error').html('Thank You!');
                        $('#error').css('color','green');
                        $('#error').slideDown('normal');
                    }
                }
        });		
        
		return false;
	});
}

function Process(str){
	list = str.split(':');
	if(list[0].toLowerCase() != 'done'){
		return list[1];
	} else {
		return '';
	}
}
