var is_opera =  navigator.userAgent.indexOf('Opera') > -1;
var is_safari = navigator.userAgent.indexOf('AppleWebKit/') > -1 && navigator.userAgent.indexOf('Chrome') == -1;
var is_chrome = navigator.userAgent.indexOf('Chrome') > -1;
var is_firefox = navigator.userAgent.indexOf('Firefox') > -1;
var is_mac = navigator.platform.toLowerCase().indexOf('mac') > -1;
var is_linux = navigator.platform.toLowerCase().indexOf('linux') > -1;
var ie6 = false;
var ie7 = false;
var ie8 = false;

$.fn.getTitle = function() {
	var arr = jQuery("a.fancybox");
	jQuery.each(arr, function() {
		var title = jQuery(this).children("img").attr("title");
		jQuery(this).attr('title',title);
	})
}

$(document).ready(function() {

	navigation_handler.setup();
	
	$('#menu-header li').first().addClass('first-child');
	$('#menu-footer li').first().addClass('first-child');

	if(is_safari) {
		$('body').addClass('browser-safari');
	}

	if(is_chrome) {
		$('body').addClass('browser-chrome');
	}
		
	if(is_opera) {
		$('body').addClass('browser-opera');
	}

	if(is_firefox) {
		$('body').addClass('browser-firefox');
	}	

	var all_links = $('a[href]');
	var hostname = window.location.hostname;
	hostname = hostname.replace("www.","").toLowerCase();

	all_links.map(function(i, el){
		if(($(el).attr('href').indexOf('http://') != -1 || $(el).attr('href').indexOf('https://') != -1) && $(el).attr('href').indexOf(hostname) == -1) {
			$(el).attr('target', '_blank');
		}
		
		if($(el).attr('href').indexOf('.pdf') != -1) {
			$(el).attr('target', '_blank');
		}
	});
	
	if(typeof slides_handler  == 'object') {
		slides_handler.setup();
	}
	
	var thumbnails = '.default-txt a:has(img)[href$=".bmp"],.default-txt a:has(img)[href$=".gif"],.default-txt a:has(img)[href$=".jpg"],.default-txt a:has(img)[href$=".jpeg"],.default-txt a:has(img)[href$=".png"],.default-txt a:has(img)[href$=".BMP"],.default-txt a:has(img)[href$=".GIF"],.default-txt a:has(img)[href$=".JPG"],.default-txt a:has(img)[href$=".JPEG"],.default-txt a:has(img)[href$=".PNG"]';

	$(thumbnails).addClass("fancybox").attr("rel","fancybox").getTitle();

	$("a.fancybox").fancybox({
		'overlayOpacity' : 0.6,
		'overlayColor': '#000',
		'padding': 1,
		'cyclic': 1,
		'titlePosition': 'over',
		'centerOnScroll': true				
	});		
	
	$('.default-txt tr').map(function(i, el){
		if(i%2==0) {
			$(el).addClass('odd');
		} else {
			$(el).addClass('even');
		}
    })
	
	if(typeof content_handler == 'object') {
		content_handler.setup();
	}
	
	var all_failures = $('#contact-form .formBuilderFailure');
	if(all_failures[1]) {
		$(all_failures[1]).remove();	
	}
	
});


var navigation_handler = {
    items: [],
    
    setup: function()  {
        this.items = $('#menu-top-menu').children();

		$(this.items).bind('mouseover', function(event) {
            if($(event.currentTarget).hasClass('over')) return;
            
            navigation_handler.items.map(function(i, el){
                if($(event.currentTarget) != el) {
                    $(el).removeClass('over');
                }
            })

            var submenu = $('ul', event.currentTarget)[0];
            if(submenu) {
                $(event.currentTarget).addClass('over');

                    if(!ie8 && !ie7 && !ie6) {
						$(submenu).css({'opacity':'0'});
						$(submenu).animate({
						opacity: 1.0
						  }, 300, function() {
						});
					}
            }
        });        

        $('body').bind('mouseover', function(event) {
            if($(event.target).parents('#menu-header').length > 0) return;
            navigation_handler.items.map(function(i, el){
                
				if($(el).hasClass('over')) {
					$(el).removeClass('over');
				}
				
            })
			
        });

    }
}

var slides_handler  = {
	container: null,
	nav_items: null,
	items: [],
	last_active: 0,
    animation_allowed: false,
	timeout: null,
	delay: 6500, //ms
	stopped: false,
	numbered_nav_items: [],
	
	setup: function() {
		
		this.container = $('#slideshow');
		if(this.container.length == 0) return;
		
		this.items = $('li', this.container);
		if(this.items.length < 2) return;

		this.animationStart();
		 
		var numbered_nav = '<ul class="numbered-nav">';
		this.items.map(function(i, el){
			numbered_nav += '<li><a href="#">' + (i+1) + '</a></li>';
		})
		numbered_nav += '</ul>';
		
		$(this.container).append(numbered_nav);

		
		$('.numbered-nav li:first-child').addClass('first-child first-active');
		$('.numbered-nav li:last-child').addClass('last-child');
		
		this.numbered_nav_items = $('.numbered-nav li a');
		
		$(this.items).bind('mouseover', function(event) {
			slides_handler.animationStop();
		});

		$('body').bind('mouseover', function(event) {
			if($(event.target).parents('#slideshow').length > 0 || slides_handler.animation_allowed == true) return;
			slides_handler.stopped = false;
			slides_handler.animationStart(true);
		});

		$(this.numbered_nav_items).bind('click', function(event) {
			event.preventDefault();
			
			var parents_active = $(event.currentTarget).parents('.active, .first-active, .last-active');
			if(parents_active.length > 0) return;
			
			var item_index = $(slides_handler.numbered_nav_items).index($(event.currentTarget));
			slides_handler.stopped = true;
			slides_handler.showSlide(item_index);
			slides_handler.last_active = item_index;
		});
		
	},
	
	setActiveNumber: function(slide_no) {
		var related_a = this.numbered_nav_items[slide_no];
		var related_li = $(related_a).parents('li')[0];
		
		this.numbered_nav_items.map(function(i, el){
			var parent_li = $(el).parents('li')[0];
			$(parent_li).removeClass('first-active');
			$(parent_li).removeClass('last-active');
			$(parent_li).removeClass('active');
		})
		
		if($(related_li).hasClass('first-child')) {
			$(related_li).addClass('first-active');	
		} else if($(related_li).hasClass('last-child')) {
			$(related_li).addClass('last-active');	
		} else {
			$(related_li).addClass('active');	
		}

		
	},
	
	showSlide: function(item_index) {
		
		var last_active = $('.items .active', slides_handler.container);
		var new_slide = slides_handler.items[item_index];

		slides_handler.setActiveNumber(item_index);

		if(false && (ie6 || ie7 || ie8)) {
			$(last_active).removeClass('active');
			$(new_slide).addClass('active');
		} else {
            $(new_slide).removeClass('ie-slide-over');
			$(last_active).animate({
			opacity: 0.0
			  }, 500, function() {
                $(last_active).removeClass('active');
			});			

			$(new_slide).addClass('active');
			$(new_slide).css({'opacity':'0'});
			$(new_slide).animate({
			opacity: 1.0
			  }, 500, function() {
                $(new_slide).addClass('ie-slide-over');
				if(!slides_handler.stopped) {
					slides_handler.animationStart();	
				}
			// Animation complete.
			});
		}
	},
	
	animationStart: function(no_delay) {
		this.animationStop();
		this.animation_allowed = true;
		this.timeout = setTimeout(function(){ slides_handler.queItem(); }, no_delay ? 2000 : this.delay);
	},
	
	animationStop: function() {
		this.animation_allowed = false;
		clearTimeout(this.timeout);
	},
	
	queItem: function() {

		if(this.animation_allowed == false || this.stopped == true) {
			return;
		}
		this.showNexItem();
		//this.timeout = setTimeout(function(){ slides_handler.queItem(); }, this.delay);
    },
	
	showNexItem: function() {
		var next_item = this.last_active + 1;
		if(next_item == this.items.length) {
			next_item = 0;
		}

		this.last_active = next_item;
		this.showSlide(next_item);
	}		
}

var content_handler = {
	
    setup: function() {
        $(window).resize(function() {
            content_handler.content_resize();
        });
        this.content_resize();
    },
    
    content_resize: function() {

		$('#content-inner').css({'height': 'auto'});

		var dh = $(document).height();
		var ch = $('#content-inner').height();
		var lh = $('#layout').height();
		var wh = $(window).height();
	
		var content_inner_height = wh - parseInt($('#header').css('height')) - parseInt($('#footer').css('height')) - parseInt($('#content').css('paddingBottom')) - parseInt($('#content').css('paddingTop')) - 2;
		
		if($('#featured-content-container').length > 0) {
			content_inner_height -= parseInt($('#featured-content-container').innerHeight());
		}
		
		if(lh < wh) {
			$('#content-inner').css({'height' : content_inner_height});
		}
    }
}
