var slideshowTimer = null;
var mass = new Array();
$(document).ready(function(){
    
    $('.anythingSlider').anythingSlider({
        autoPlay: false,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
        delay: 3000,                    // How long between slide transitions in AutoPlay mode
        startStopped: false,            // If autoPlay is on, this can force it to start stopped
        animationTime: 600,             // How long the slide transition takes
        hashTags: true,                 // Should links change the hashtag in the URL?
        buildNavigation: true,          // If true, builds and list of anchor links to link to each slide
        pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
        startText: "Go",             // Start text
        stopText: "Stop",               // Stop text
        navigationFormatter: formatText       // Details at the top of the file on this use (advanced use)
    });

    if ( !($.browser.msie && $.browser.version == 6) ){
        $('.anythingSlider LI').each(function(i){
            $(this).addClass('load');
            mass[i] = $(this).children('img').attr('src');
            $(this).children('img').remove();
        });
        
        var count = mass.length;  
        
        for (var i = 0; i < count; i++) {
            image = new Image();
            image.src = mass[i];
            image.onLoad=imagesLoaded(i);        
        }
    }

    $('#accordion LI').click(function(){
        if ($(this).children('DIV.txt-area').css('display') != 'block') {
            $('DIV.txt-area').slideUp();
            $(this).children('DIV.txt-area').slideDown(200);        
        } else {
            $(this).children('DIV.txt-area').slideToggle(200);
        }
      
        return false;
    });	
    
    Cufon.replace('h2')('.sidebar h3')('.sidebar h4');    
    Cufon.set('fontFamily', 'Helvetica Neue Bold').replace('h2.nuvet-bold');    

	var inputsList = getElementsByClassName('auto-hint');
	for (i=0; i < inputsList.length; i++){
		inputsList[i].onfocus = function() {if(this.value == this.defaultValue) this.value = '';}
		inputsList[i].onblur = function() {if(this.value == '') this.value = this.defaultValue };
	};	 
    
    initForms();
    
    if($('#rotator').size() > 0 ){
		doSlideshow('rotator', true, 5000);
	}
    
	$("SELECT").selectbox();
    
    $("SELECT").change(function(){
        if ( parseInt($(this).val()) > 0 ){
            window.location.href = 'location.html?id=' + $(this).val();
        }
    });
    
    var option = {
        imageLoading:			'public/images/lightbox/lightbox-ico-loading.gif',
        imageBtnPrev:			'public/images/lightbox/lightbox-btn-prev.gif',
        imageBtnNext:			'public/images/lightbox/lightbox-btn-next.gif',
        imageBtnClose:			'public/images/lightbox/lightbox-btn-close.gif',
        imageBlank:				'public/images/lightbox/lightbox-blank.gif'
    }
    
    $('.pictures UL A').lightBox(option);
    $('.content .info .lightbox A').lightBox(option);
    
});

function imagesLoaded(i){
    $('.anythingSlider LI:eq(' + i + ')').html('<img src="' + mass[i] + '" />').removeClass('load');
}

window.onload = function() {
	
}

function formatText(index, panel) {
  return index + "";
}

function isVisible(obj) { return (obj.css('display') == 'block'); } 

function doSlideshow(objid, start, time){
	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);
	currObj.removeClass('current');
	currObj.animate({opacity:0}, 2000);
	nextObj.animate({opacity:1}, 2000);
	
	slideshowTimer = setTimeout('doSlideshow("' + objid + '", false, ' + time + ');', time);
}

function getElementsByClassName(in_class,in_start) {
    var start_el = in_start || document;
    var all_els = start_el.getElementsByTagName("*")
    var rez_arr = Array();
    for(var i=0; i < all_els.length; i++){
        var curr_className = all_els[i].className;
        if(curr_className){
            var atom_class_arr = curr_className.split(/\s+/);
            for(var j=0; j < atom_class_arr.length; j++){
                if(in_class == atom_class_arr[j]){
                    rez_arr.push(all_els[i]);
                }
            }
        }
    };
    return(rez_arr);
};

function initForms(){
	$('form.forms').submit(function(){
		var form = $(this);
		var formData = $(this).serialize();
        var error = '';
		var err = form.find('.error');        
        err.slideUp('normal');
            $.ajax({
                type: 'POST', url: form.attr('action'), data: formData, 
                success: function ( responseData ){
                    if((error = Process(responseData)) != ''){
                        err.html(error);
                        err.css('color','#FF0000');                        
                        err.slideDown('normal');
                    } else{
                        document.getElementById(form.attr('id')).reset();
                        err.html('Thank You');
                        err.css('color','green');                        
                        err.slideDown('normal');
                        setTimeout("$('form.forms .error').slideUp('normal');", 2000);
                    }
                }
        });		
        
		return false;
	});
}

function Process(str){
	list = str.split(':');
	if(list[0].toLowerCase() != 'done'){
		return list[1];
	} else {
		return '';
	}
}
