var is_opera =  navigator.userAgent.indexOf('Opera') > -1;
var is_safari = navigator.userAgent.indexOf('AppleWebKit/') > -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;

var gallery_data = [];

$.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() {

	var main_menu_items = $('#menu-header ul')[0];
	$('li:first-child', main_menu_items).addClass('first-child');
	$('li:last-child', main_menu_items).addClass('last-child');

    if(typeof navigation_handler == 'object') {
        navigation_handler.setup();
    }

	var all_links = $('a');
	var hostname = window.location.hostname;
	hostname = hostname.replace("www.","").toLowerCase();

	all_links.map(function(i, el){
		if($(el).attr('href')) {
			if($(el).attr('href').indexOf('http://') != -1 && $(el).attr('href').indexOf(hostname) == -1) {
				$(el).attr('target', '_blank');
			}
			
			if($(el).attr('href').indexOf('.pdf') != -1 || $(el).attr('href').indexOf('.doc') != -1 || $(el).attr('href').indexOf('.docx') != -1) {
				$(el).attr('target', '_blank');
			}
		}
	});
	
	quick_news.setup();

	if(typeof header_slideshow == 'object') {
		header_slideshow.setup();
	}

	function formatTitle(title, currentArray, currentIndex, currentOpts) {
		return '<div id="fancybox-title-over">' + (title && title.length ? '<b>' + title + '</b>' : '' ) + '<span>Image ' + (currentIndex + 1) + ' of ' + currentArray.length + '</span></div>';
	}
		
	$('#gallery-slideshow').bind('click', function(event) {
		event.preventDefault();
		$.fancybox(
				   gallery_data,
				   {'overlayOpacity' : 0.6,
					'overlayColor': '#000',
					'padding': 1,
					'cyclic': 1,
					'titlePosition': 'over',
					'titleFormat'		: formatTitle,
					'centerOnScroll': true}
				   );
	});
	
	$('#gallery-listing-container a').bind('click', function(event) {
		event.preventDefault();
		
		var related_a = event.currentTarget;
		
		var related_gallery = gallery_data[$(event.currentTarget).attr('rel')];
		$.fancybox(
				   related_gallery,
				   {'href': $(related_a).attr('href'),
					'overlayOpacity' : 0.6,
					'overlayColor': '#000',
					'padding': 1,
					'cyclic': 1,
					'titlePosition': 'over',
					'titleFormat'		: formatTitle,
					'centerOnScroll': true}
				   );
	});	
	
	
	$('.default-txt tr').map(function(i, el){
		if(i%2==0) {
			$(el).addClass('odd');
		} else {
			$(el).addClass('even');
		}
    })
	

	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				
	});	
	
	if(is_safari) {
		$('body').addClass('browser-safari');
	}

	if(is_opera) {
		$('body').addClass('browser-opera');
	}
	
	
	$('#gallery-slideshow, #page-tabs li, #gallery-listing-container .gallery-slideshow').bind('mouseover', function(event) {
		$(event.currentTarget).addClass('over');
	});

	$('#gallery-slideshow, #page-tabs li, #gallery-listing-container .gallery-slideshow').bind('mouseout', function(event) {
		$(event.currentTarget).removeClass('over');
	});
	

});

var quick_news = {
	container: null,
	last_item: 0,
	total_items: [],
	delay: 3000,
	
	setup: function() {
		this.container = $('#quick-news');
		if(!this.container) return;
		
		var all_news = $('li', this.container);
		this.total_items = all_news.length;
		
		if(this.total_items == 1) return;
		
		setTimeout(function(){quick_news.toggleItem()}, quick_news.delay)
	},
	
	toggleItem: function() {
		var last_item = $('#quick-news .active');
		var next_index = this.last_item + 1;
		if(next_index == this.total_items) {
			next_index = 0;
		}
		
		$(last_item).animate({
			width: 0
		  }, 1000, function() {
			$(last_item).removeClass('active')

			var next_item = $('#quick-news li')[next_index];
			$(next_item).css({'width':0});
			$(next_item).addClass('active');

			$(next_item).animate({
				width: 480
			  }, 1000, function() {
				quick_news.last_item = next_index;
				
				setTimeout(function(){quick_news.toggleItem()}, quick_news.delay + 1000)
			});

		});
	}
}

    
    
var navigation_handler = {
    items: [],
    
    setup: function()  {
		
        this.items = $('#menu-header ul:first-child').children();
		
		$('#menu-header li ul').wrap('<div class="dropdown-menu"></div>');

		$('#menu-header a').bind('click', function(event) {
			if($(event.currentTarget).attr('href') == '#') {
				event.preventDefault();
			}
		});

		$(this.items).bind('mouseover', function(event) {
            if($(event.currentTarget).hasClass('over') || $(event.currentTarget).hasClass('no-submenu-over')) return;
            
            navigation_handler.items.map(function(i, el){
                if($(event.currentTarget) != el) {
                    $(el).removeClass('over');
					$(el).removeClass('no-submenu-over');
                }
            })

			
			
            var submenu = $('ul', event.currentTarget)[0];
            if(submenu) {
					$(event.currentTarget).addClass('over');
                    if(false && !ie8 && !ie7 && !ie6) {
						$(submenu).css({'opacity':'0'});
						$(submenu).animate({
						opacity: 1.0
						  }, 300, function() {
						});
					}
            } else {
				$(event.currentTarget).addClass('no-submenu-over');
			}
        });        

        $('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');
				}

				if($(el).hasClass('no-submenu-over')) {
					$(el).removeClass('no-submenu-over');
				}
				
            })
			
        });

    }
}

/*
var header_slideshow = {
    container: null,
    total_items: 4,
    last_item: null,
    delay: 6500,
    
    setup: function()  {

		setTimeout(function(){ header_slideshow.changePic(); }, header_slideshow.delay);
		last_item = 0;
		this.total_items = $('#header-photos li').length;
    },
    
    changePic: function() {
		$('#header-photos').removeClass('slideshow-over');
		var last_pic = $('#header-photos .active');
		var next_index = this.last_item + 1;
		if(next_index == this.total_items) {
			next_index = 0;
		}
		
		var last_pic = $('#header-photos .active');
		$(last_pic).animate({
			opacity: 0
		  }, 1000, function() {
			// Animation complete.
		});
		setTimeout(function(){$(last_pic).removeClass('active')}, header_slideshow.delay);
	
		var next_pic = $('#header-photos li')[next_index];
		$(next_pic).css({'opacity':'0'});
		$(next_pic).addClass('active');
		$(next_pic).animate({
			opacity: 1
		  }, 1000, function() {
			$('#header-photos').addClass('slideshow-over');
		});
		
		this.last_item = next_index;
		setTimeout(function(){ header_slideshow.changePic(); }, header_slideshow.delay);
    }
	
}*/


var header_slideshow  = {
	container: null,
	nav_items: null,
	items: [],
	last_active: 0,
    animation_allowed: false,
	timeout: null,
	delay: 6500, //ms
	stopped: false,
	
	setup: function() {
		
		this.container = $('#header-photos');
		if(this.container.length == 0) return;
		
		this.items = $('li', this.container);
		if(this.items.length < 2) return;

		this.animationStart();
		
	},
	
	showSlide: function(item_index) {
		
		var last_active = $('.active', header_slideshow.container);
		var new_slide = header_slideshow.items[item_index];

		if(false && (ie6 || ie7 || ie8)) {
			$(last_active).removeClass('active');
			$(new_slide).addClass('active');
		} else {
            $(header_slideshow.container).removeClass('slideshow-over');
			$(last_active).animate({
			opacity: 0.0
			  }, 1000, function() {
                $(last_active).removeClass('active');
			});			

			$(new_slide).addClass('active');
			$(new_slide).css({'opacity':'0'});
			$(new_slide).animate({
			opacity: 1.0
			  }, 1000, function() {
                $(header_slideshow.container).addClass('slideshow-over');
				if(!header_slideshow.stopped) {
					header_slideshow.animationStart();	
				}
			// Animation complete.
			});
		}
	},
	
	animationStart: function(no_delay) {
		this.animationStop();
		this.animation_allowed = true;
		this.timeout = setTimeout(function(){ header_slideshow.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(){ header_slideshow.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() {
		$('body').removeClass('no-scroll');
		var dw = $(document).width();
		var ww = $(window).width();
		if(ww < 1020) {
			$('body').addClass('no-scroll');
		}
    }
}



function initArticleGallery() {
	var jcarousel_wrap = 'circular'; //(ie6 || ie7 || ie8) ? 'both' : 'circular';
	
	//gallery slider
	var items_no = $('#article-gallery-item li').length;
	
	if(items_no <=1) {
		$('.jcarousel-article-gallery').jcarousel({itemFallbackDimension: 620});
	} else {
		$('.jcarousel-article-gallery').jcarousel({
			scroll: 1,
			//auto: 6,
			animation: 500,
			wrap: jcarousel_wrap,
			itemVisibleInCallback: {
				onAfterAnimation:  function() {
				}
			},

			itemVisibleOutCallback: {
				onBeforeAnimation: function(){
				},
				onAfterAnimation:  function(){
				}
			},
		
			itemFallbackDimension: 620,
			
			initCallback: function(carousel) {

				carousel.buttonNext.bind('click', function() {
					carousel.startAuto(0);
				});
			
				carousel.buttonPrev.bind('click', function() {
					carousel.startAuto(0);
				});
			},
			
			itemFirstInCallback:  function(carousel, item, idx, state) {
			}
			
		});
	}	
}
