$(document).ready(function(){				   
	$("input.input_text").HideTitle();
});

function hover_menu(){
	var orig_image_source;
	
	$("ul#menu_main li a img").mouseover(function(){
		
		orig_image_source = $(this).attr("src");
		console.log(orig_image_source);
		var new_image_source = orig_image_source.replace('_hover','');
		
		console.log(new_image_source);
		$(this).attr("src",new_image_source);
		
	}).mouseout(function(){
		console.log(orig_image_source);
		$(this).attr("src",orig_image_source);
	});	
}
	
jQuery.fn.extend({
		 
	HideTitle: function() {
		var obj = this;
		var title;
		var obj_new;
		
		for(x=0; x<obj.length; x++){
			
			obj_new = obj[x];
			title = $(obj_new).attr("title");
			
			$(obj_new).val(title);
			
			$(obj_new).blur(function(event){
				
				var title = $(this).attr("title");
				
				if($(this).val() == ''){
					$(this).val(title);
				}
				
			});
			
			$(obj_new).focus(function(event){
				
				var title = $(this).attr("title");
				
				if($(this).val() == title){
					$(this).val('');
				}
				
			});
		}
	
	}

});



(function () {
    $.fn.infiniteCarousel = function () {
        function repeat(str, n) {
            return new Array( n + 1 ).join(str);
        }
        
        return this.each(function () {
            // magic!
            var $wrapper = $('> div', this).css('overflow', 'hidden'),
                $slider = $wrapper.find('> ul').width(9999),
                $items = $slider.find('> li'),
                $single = $items.filter(':first')
                
                singleWidth = $single.outerWidth(),
                visible = Math.ceil($wrapper.innerWidth() / singleWidth),
                currentPage = 1,
                pages = Math.ceil($items.length / visible);
                
            /* TASKS */
            
            // 1. pad the pages with empty element if required
            if ($items.length % visible != 0) {
                // pad
                $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
                $items = $slider.find('> li');
            }
            
            // 2. create the carousel padding on left and right (cloned)
            $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
            $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
            $items = $slider.find('> li');
            
            // 3. reset scroll
            $wrapper.scrollLeft(singleWidth * visible);
            
            // 4. paging function
            function gotoPage(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir * visible * n;
                
                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft : '+=' + left
                }, 500, function () {
                    // if page == last page - then reset position
                    if (page > pages) {
                        $wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                        page = pages;
                        $wrapper.scrollLeft(singleWidth * visible * pages);
                    }
                    
                    currentPage = page;
                });
            }
            
            // 5. insert the back and forward link
            $wrapper.after('<a href="#" class="arrow back"></a><a href="#" class="arrow forward"></a>');
            
            // 6. bind the back and forward links
            $('a.back', this).click(function () {
                gotoPage(currentPage - 1);
                return false;
            });
            
            $('a.forward', this).click(function () {
                gotoPage(currentPage + 1);
                return false;
            });
            
            $(this).bind('goto', function (event, page) {
                gotoPage(page);
            });
            
            // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
            $(this).bind('next', function () {
                gotoPage(currentPage + 1);
            });
        });
    };
})(jQuery);

$(document).ready(function () {
    // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
    var autoscrolling = true;
    
    $('.infiniteCarousel').infiniteCarousel().mouseover(function () {
        autoscrolling = false;
    }).mouseout(function () {
        autoscrolling = true;
    });
    
    setInterval(function () {
        if (autoscrolling) {
            $('.infiniteCarousel').trigger('next');
        }
    }, 2000);
});


function goto( url ){
	window.location.href=url;
}

/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
function select_membership_type(obj){
	$("div.membership_type").hide();
	var form_name = $(obj).val();
	if( form_name != '' ){
		$("div#"+form_name).show();
	}
}
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/


function start_slide_show(id_name, int_min){
	var obj = $("#"+id_name);
	var li = $("li",obj);
	var li_length = li.length;			
	
	image_carousel();
	
	for(x=0; x<li_length; x++){
		if(x > 0){
			$(li[x]).hide();
		} else {
			$(li[x]).show();
		}
	}
	/*$("a.left").hover(
		function () {
			console.log("false");
			slide = false;
		}, 
		function () {
			console.log("true");
			slide = true;
		}
	);
	$("a.right").hover(
		function () {
			console.log("false");
			slide = false;
		}, 
		function () {
			console.log("true");
			slide = true;
		}
	);*/
	
	setInterval(function(){
		slide_show(li);
	}, (int_min*1000));
}

function slide_show(obj){
	var slide = true;

	$("a.left").hover(function(){
			console.log("false");
		slide = false;
	}, function () {
		slide = true;
	});
	$("a.right").hover(function(){
			console.log("true");
		slide = false;
	}, function () {
		slide = true;
	});
	
	var li = $(obj);
	var li_length = li.length;
	var li_obj_cur;
	var li_obj_next;
	
	if( slide == true ){
		for(x=0; x<li_length; x++){
			
			style_display = $(li[x]).css("display");
			
			if(style_display != 'none'){
				
				li_obj_cur = li[x];
				
				if((x+1) == li_length){
					li_obj_next = li[0];
				} else {
					li_obj_next = li[x+1];
				}
			}
		}
		
		$(li_obj_cur).fadeOut("normal");
		$(li_obj_next).fadeIn("normal");
	}
}

function image_carousel(){
	var slide_images = $("div#header_slide_images");
	var image_array = $("ul li",slide_images);
	var count_images = image_array.length;
	
	$(image_array).hide();
	$(image_array[0]).show();

	var x;
	var key;
	$("a.left",slide_images).click(function(){
		key = get_selected_image(count_images, image_array);
		
		$(image_array[key]).fadeOut("normal");
		
		if( key > 0 ){
			$(image_array[key-1]).fadeIn("normal");
		} else {
			$(image_array[count_images-1]).fadeIn("normal");
		}
	});
	$("a.right",slide_images).click(function(){
		key = get_selected_image(count_images, image_array);
		
		$(image_array[key]).fadeOut("normal");
		
		if( key < (count_images-1) ){
			$(image_array[key+1]).fadeIn("normal");
		} else {
			$(image_array[0]).fadeIn("normal");
		}
	});
}

function get_selected_image(count_images, image_array){
	var selected_key;
	var display;
	
	for(x=0; x<count_images; x++){
		display = $(image_array[x]).css("display");
		
		if( display == 'block' || display == 'list-item' ){
			selected_key = x;		
		}
	}
	
	return selected_key;
}
