//adding background position X function...
$.fn.backgroundPosX = function() {
    var pos = $(this).css("background-position");
    if (pos == 'undefined' || pos == null) {
        pos = $(this).css("background-position-x");
    } else {
        pos = pos.split(" ")[0];
    }
    return pos || 0;
}

$(document).ready(function(){
    $('#flags li').mouseenter(function(){
        $(this).css({'opacity': 0.7});
    }).mouseleave(function(){
        $(this).css({'opacity': 1});
    });
    
    $('.boxLink').mouseenter(function(){
        $('.overlay', this).css({'opacity': 0.15, 'background' : '#ffffff'});
    }).mouseleave(function(){
        $('.overlay', this).css({'opacity': 1, 'background' : 'transparent'});
    });
    
    /*main Menu*/
    $('#menu > li').mouseenter(function(){
        $(this).addClass('hovered').find('> a').addClass('hovered').parent().find('> ul').show();
    }).mouseleave(function(){
        $(this).removeClass('hovered').find('> a').removeClass('hovered').parent().find('> ul').fadeOut('fast');
    }).each(function(){
        //moving submenu to its designed position...
        $('> ul', this).each(function(){
            var left = parseInt($(this).parent().find('> a').backgroundPosX()) * -1;
            var width = parseInt($(this).parent().find('> a').width());
            $(this).css('left', left - (200 - width) / 2);
        });
    });
    
    /*main Menu level 2*/
    $('#menu > li > ul > li').each(function() {
        $(this).mouseenter(function(){
            $('> ul' ,this).fadeIn('fast');
            $('> a' ,this).addClass('hovered');
        }).mouseleave(function(){
            $('> ul' ,this).hide();
            $('> a' ,this).removeClass('hovered');
        });
        $(this).find('> ul').each(function(){
            var top = $(this).parent().position().top;
            $(this).css('margin-top', (top - 5) + 'px');
            $('> li',this).mouseenter(function(){
                $('> a' ,this).addClass('hovered');
            }).mouseleave(function(){
                $('> a' ,this).removeClass('hovered');
            });
        });
    });
    
    $('#menu ul').css({
        'display': 'none',
        'visibility' : 'visible'
    });
    
    /*intro container*/
    $('.introContainer img.miniature, .introContainer h3, .introContainer a.more').mouseenter(function(){
        $(this).parent().find('a.more').addClass('hoveredMore');
        //$(this).parent().find('.moreHelper').stop(true, true).animate({width: 'show'});
        $(this).parent().find('.moreHelper').stop(true, true).fadeIn('slow');
    }).mouseleave(function(){
        $(this).parent().find('a.more').removeClass('hoveredMore');
        $(this).parent().find('.moreHelper').stop(true, true).fadeOut('fast');
    }).click(function(){
        var href = $(this).parent().find('a.more').attr('href');
        location = href;
    });
    $('.introContainer a.archive').mouseenter(function(){
        $(this).addClass('hoveredMore');
    }).mouseleave(function(){
        $(this).removeClass('hoveredMore');
    });
    $('.netIntroContainer').mouseenter(function(){
        $(this).addClass('hoveredNetIntroContainer');
    }).mouseleave(function(){
        $(this).removeClass('hoveredNetIntroContainer');
    }).click(function(){
        var href = $(this).find('a.more').attr('href');
        location = href;
    });
    
    $('.downloadCatalogue, .downloadPDF').mouseenter(function(){
        $(this).css({backgroundPosition: '0 -54px'});
    }).mouseleave(function(){
        $(this).css({backgroundPosition: '0 0'});
    }).mousedown(function(){
        $(this).css({backgroundPosition: '0 -53px'});
    }).mouseup(function(){
        $(this).css({backgroundPosition: '0 -54px'});
    });
    
    $('.downloadPDF').click(function(){
        $('#pdfHelper').fadeIn('slow');
        setTimeout(function(){$('#pdfHelper').fadeOut('slow');}, 10000);
    });
    
    //fancy slects...
    $('#contactForm select').mouseenter(function(){
        $(this).parent().css({backgroundPosition: 'left bottom'});
    }).mouseleave(function(){
        $(this).parent().css({backgroundPosition: 'left top'});
    }).each(function(){
        var self = this;
        $(this).css({opacity: 0}).parent().find('input').val($(':selected', self).text());
    }).change(function(){
        var self = this;
        $(this).parent().find('input').val($(':selected', self).text());
    });
    
    $("a.popup").fancybox();
    
    $(".googleMapLink").fancybox({
        'type' : 'iframe',
		'width' : '75%',
		'height' : '75%'
	});
});
