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 browser_ie6 = false;
var browser_ie7 = false;
var browser_ie8 = false;


var slideshow = {
    container: null,
    last_index: null,
    items: [],
    
    setup: function() {
	
	this.container = jQuery('#photo-slideshow');
	if(!this.container) return;
	
	this.items = jQuery('li', this.container);
	
	this.items.map(function(){
	    jQuery(this).css('display', 'none');
	})


	var items_no = this.items.length;
	var show_item = Math.floor(Math.random() * (items_no));
	
	jQuery(this.items[show_item]).css('display', 'block');
	jQuery(this.items[show_item]).addClass('active');
	
	this.last_index = show_item;
	
	var o = this;
	if(this.items.length > 1) {
	    setInterval(function(){o.changePhoto()}, 6000);
	}
    },
    
    changePhoto: function() {
	jQuery(this.items[this.last_index]).fadeOut(1000);
	
	var next_index = this.last_index + 1;
	if(next_index == this.items.length) {
	    next_index = 0;
	}
	jQuery(this.items[next_index]).fadeIn(1000);
	
	this.last_index = next_index;
    }
}

var navigation = {
    container: null,
    items: [],
    original_bkg_position: null,
    is_expanding: false,
    is_collapsing: false,
    nav_bkg: null,
    time_expanded: null,
    original_bkg: null,
    
    setup: function() {
	
	this.container = jQuery('#navigation');
	
	this.items = jQuery('li ul', this.container);
	
	//red background button
	//var has_active = jQuery('.active', this.container);
	var has_active = jQuery('ul', this.container).first().children('li.active');
	if(has_active.length > 0) {
	    var item_position = has_active.first().position();
	    jQuery(this.container).addClass('has-active');
	    var padding_fix = has_active.first().hasClass('first-child') ? 0 : 6;
	    jQuery('ul', this.container).first().css('background-position', item_position.left + padding_fix + 'px 0px');
	    this.original_bkg_position = item_position.left + padding_fix + 'px 0px';
	} else {
	    jQuery('ul', this.container).first().css('background', 'url(images/css/navigation-button.gif) no-repeat -207px 0');
	    this.original_bkg_position = '-207px 0px';
	}
	this.original_bkg = jQuery('ul', this.container).first().css('background');
	
	//min height
	var max_height = 0;
	this.items.map(function(){
	    var item_height = parseInt(jQuery(this).height());//parseInt(jQuery(this).css('height'));
	    if(item_height > max_height) {
		max_height = item_height;
	    }
	})
	
	this.items.map(function(){
	    jQuery(this).css('height', max_height + 'px')
	})

	this.nav_bkg = document.createElement('div');
	this.nav_bkg.setAttribute('id', 'nav-bkg');
	jQuery(this.container).append(this.nav_bkg);
	jQuery(this.nav_bkg).css('height', max_height + 'px')
	
	//left offset
	var level_1_items = jQuery('ul', this.container).first().children();
	level_1_items.map(function() {
	    //mouseover effect
	    jQuery(this).bind('mouseover', function(event) {
		
		if(navigation.is_collapsing) return;
		var item_position = jQuery(event.currentTarget).position();
		var padding_fix = jQuery(event.currentTarget).hasClass('first-child') ? 0 : 6;
		//jQuery('ul', navigation.container).first().css('background-position', item_position.left + padding_fix + 'px 0');
		jQuery('ul', navigation.container).first().clearQueue();

		var first_ul = jQuery('ul', navigation.container);
		if(jQuery(first_ul[0]).css('background-image') == 'none') {
		    jQuery(first_ul[0]).css('background', navigation.original_bkg);
		    jQuery(first_ul[0]).css('background-position', navigation.original_bkg_position);
		}
		
		if(browser_ie6 || browser_ie7 || browser_ie8) {
		    var first_ul_background = jQuery(first_ul[0]).css('backgroundPosition');
		} else {
		    var first_ul_background = jQuery(first_ul[0]).css('background-position');
		}
		
		//jQuery(first_ul).css('background-image', 'url(images/css/navigation-button.gif)');
		
		//if(first_ul_background.indexOf('-207px 0px') != -1) {
		//if(navigation.original_bkg_position == '-207px 0px') {

		if(navigation.original_bkg_position == '-207px 0px' && first_ul_background.indexOf('-207px') == 0) {
		    jQuery('ul', navigation.container).first().css('background-position', item_position.left + padding_fix + 'px 0');
		    
		} else {
		    jQuery('ul', navigation.container).first().animate({
			backgroundPosition: item_position.left + padding_fix + 'px 0px'
		      }, 200, function() {
			// Animation complete.
		    });
		}
		
	    })	    
	    
	    //subitems alignement
	    var current_a = jQuery('a', this);
	    current_a.first().css('display', 'inline');
	    var current_width = current_a.first().width();
	    var padding_left =	Math.ceil((207-current_width)/2);
	    var padding_right = Math.floor((207-current_width)/2);
	    
	    var is_last = jQuery(current_a).parents('.last-child').length > 0;
	    var width_fix = is_last ? -6 : 0;
	    
	    var current_ul = jQuery('ul', this)[0];
	    jQuery(current_ul).css('width', 207 - padding_left + width_fix + 'px');
	    jQuery(current_ul).css('padding-left', padding_left + 'px');
	    current_a.first().css('display', 'block');
	});

	jQuery(this.items).slideUp(0);
	jQuery(this.nav_bkg).slideUp(0);
	
	jQuery(this.items).map(function() {
	    jQuery(this).css('left', 0);
	    
	})
	
	jQuery('a', this.container).bind('mouseover', function(event) {

	    if(navigation.is_expanding || navigation.is_collapsing || jQuery(navigation.container).hasClass('expanded')) return;
	    navigation.time_expanded = new Date().getTime();	    
	    navigation.is_expanding = true;
	    setTimeout(function(){navigation.is_expanding = false; }, 400);
	    jQuery(navigation.container).addClass('expanded');
	    jQuery(navigation.items).slideDown(400);
	    jQuery(navigation.nav_bkg).slideDown(400);
	})
	
	jQuery('body').bind('mouseover', function(event) {
	    
	    if(jQuery(event.target).parents('#navigation').length == 0) {
		if(navigation.time_expanded != null && (new Date().getTime() - navigation.time_expanded < 250)) {
		    return;
		}
		if(navigation.is_collapsing || navigation.is_expanding || !jQuery(navigation.container).hasClass('expanded')) return;

		navigation.is_collapsing = true;
		
		jQuery('ul', navigation.container).first().clearQueue();
		if(navigation.original_bkg_position != null) {

		    if(navigation.original_bkg_position == '-207px 0px') {
			//jQuery('ul', navigation.container).first().css('background-position',  navigation.original_bkg_position);
			jQuery('ul', navigation.container).first().css('background',  'none');
		    } else {
			jQuery('ul', navigation.container).first().animate({
			   backgroundPosition: navigation.original_bkg_position
			 }, 200, function() {
			   
		       });
		    }
		}
		
		setTimeout(function(){ /*navigation.is_animating = false;*/ jQuery(navigation.container).removeClass('expanded'); navigation.is_collapsing = false; }, 200);
		jQuery(navigation.items).slideUp(200);
		jQuery(navigation.nav_bkg).slideUp(200);
	    }
	})	
    }
}


var tools = {
    setup: function() {
	
	var font_size_links = jQuery('#tools ul a');
	
	jQuery(font_size_links).bind('click', function(event) {
	     event.preventDefault();
	     var parent_li = jQuery(event.target).parent('li');
	     setActiveStyleSheet(jQuery(parent_li).attr('class') + '-fonts');
	});
	
	var print_link = jQuery('#print-link a')[0];
	jQuery(print_link).bind('click', function(event) {
	     event.preventDefault();
	     window.print();
	});	
    }    
}

function resizeContent() {
    var html_el = jQuery('html').first();
    var body_el = jQuery('body').first();

    jQuery(html_el).css('height', '100%');
    jQuery('#content').css('height', 'auto');
	    
    var layout_height = jQuery('#layout').height();
    var window_height = jQuery(window).height();
    
    if(window_height > layout_height) {
	jQuery('#content').css('height', window_height - 182 + 'px');
	jQuery(html_el).css('height', '100%');
    } else {
	jQuery('#content').css('height', 'auto');
	jQuery(html_el).css('height', layout_height + 'px');
    }	
    
}
jQuery.event.add(document, "ready", resizeContent);
jQuery.event.add(window, "resize", resizeContent);

function init() {
    if(typeof slideshow == 'object') {
        slideshow.setup();
    }
    
    if(typeof navigation == 'object') {
        navigation.setup();
    }
    
    if(browser_ie6) {
	jQuery('#navigation').wrap('<div id="ie-navigation-wrap">');
    }
    
    if(typeof tools == 'object') {
        tools.setup();
    }
    
    jQuery(document).ready(function(){
        jQuery("a[rel='gallery']").colorbox({slideshow:true, slideshowAuto:false, slideshowSpeed: 3500, 'next': 'Slijedeća', 'previous':'Prethodna', 'close':'Zatvori', current: 'Slika {current} od {total}', slideshowStart: "pokreni slideshow", slideshowStop: "zaustavi slideshow"});
    });
}

function setMenuDots() {

    jQuery('.menu-dot').remove();
    jQuery('.menu-break').remove();

    try {
        // početna X pozicija novog reda
        var startOffsetX = jQuery('#subMenu').children().first().position().left;

        // visina jednog reda
        var singleRowHeight = jQuery('#subMenu').children().first().height();

        // širina cijelog menu-a
        var menuWidth = jQuery('#subMenu').width();

        // postavljam točke
        jQuery('#subMenu').children().each(function() {

            if (jQuery(this).position().left != startOffsetX || jQuery(this).height() > singleRowHeight) {

                var originalOffsetX = jQuery(this).position().left;
                var originalHtml = jQuery(this).html();
                var originalHeight = jQuery(this).height();

                //alert(jQuery(this).children().first().html() + " *** " + jQuery(this).position().left + " *** " + jQuery(this).height());

                jQuery(this).html('<span class="menu-dot">&nbsp;</span>' + originalHtml);

                //alert(jQuery(this).position().left + " *** " + jQuery(this).height());

                // apsolutna širina item-a, da bi ju dobio moram spriječiti prelamanje po redovima
                jQuery(this).css('position', 'absolute');
                var itemAbsoluteWidth = jQuery(this).width();
                jQuery(this).css('position', 'relative');

                // provjera da li je točka uzrokovala prijelom
                if ((jQuery(this).position().left < originalOffsetX && !(jQuery(this).height() > originalHeight)) ||
                    (jQuery(this).height() <= singleRowHeight && originalHeight > singleRowHeight) ||
                    (itemAbsoluteWidth > menuWidth)) {

                    jQuery(this).html(originalHtml);
                    jQuery('<br class="menu-break" />').insertAfter(jQuery(this).prev());
                }
            }
        });
    }
    catch (Error) { }
}
