function showDepartment(whatDepartment) {
    if(currentDepartment != whatDepartment) {
        $('#'+whatDepartment+'_over').fadeIn(500);
    }
}

function hideDepartment(whatDepartment) {
    if(currentDepartment != whatDepartment) {
        $('#'+whatDepartment+'_over').fadeOut(200);
    }
}

var spotlightfiles = new Array();
var spotlightlast = -1;
 
function getSpotlights() {
    $.getJSON('/ajax/getspotlight.php', function(data) {

        for(var pos = 0; pos < data.spotlight.length; pos++) { 
            spotlightfiles[pos] = data.spotlight[pos].file;
        }
        showSpotlight();
        preloadSpotlight();
    });
    
}

function preloadSpotlight() {
    var preloads = new Array();
    for(var i = 0; i < spotlightfiles.length; i++){
        preloads[i] = "/images/spotlight/" + spotlightfiles[i];
    }
    $.preloadImages(preloads);
}

function showSpotlight() {
      
    var randPos = null;
    do {
        randPos = Math.floor(Math.random() * spotlightfiles.length);
    } while (randPos == spotlightlast);
    spotlightlast = randPos;
    var showfile = spotlightfiles[randPos];
    
    if($('#spotlightimg').attr('src') == '/images/empty.gif') {
        // first load
        $('#spotlightimg').attr('src','/images/spotlight/'+showfile);
        
    } else {
        // change image
        $('#spotlightimg').animate({opacity: 0, left: 0 }, 1000,'swing', function(){
            $('#spotlightimg').attr('src','/images/spotlight/'+showfile);                
            $('#spotlightimg').animate({opacity: 1,left: 0 }, 1000,'swing');
        });
    }
    
    setTimeout(showSpotlight, 15000);
}

// sudda value från formulärfält vid fokus.
(function($) {
    $.fn.blurRr = function(options) {
        
        var defaults = {
            
        };
        
        var options = $.extend(defaults, options);
        
        return this.each(function() {
            var obj = $(this);
            var prevValue = '';
            
            obj.attr('title', obj.val());
            
            var titleValue = obj.attr('title');

            obj.focus(function() {
                if(obj.val() == titleValue) {
                    prevValue = obj.val();
                    obj.val('');
                }
            });
            
            obj.blur(function() {
                if(obj.val() == '') {
                    obj.val(prevValue);
                }
            });
        });
        
    };
})(jQuery);

$(document).ready(function() {
    
    getSpotlights();
    
    // check browser if we should use images
    var agent=navigator.userAgent.toLowerCase();    
    if(agent.indexOf('iphone')!=-1 || agent.indexOf('ipad')!=-1) {
        // on iphone
    } else {
        // replace all h2 title to img tag
        $('h2.title').each(function(index) {
                var text = $(this).text();
                $(this).replaceWith('<h2 class="title"><span>'+text+'</span><img src="/header/'+escape(escape(text))+'.png" alt="'+text+'" title="'+text+'" /></h2>')
        });
        $('h2.titlepink').each(function(index) {
                var text = $(this).text();
                $(this).replaceWith('<h2 class="titlepink"><span>'+text+'</span><img src="/headerpink/'+escape(escape(text))+'.png" alt="'+text+'" title="'+text+'" /></h2>')
        });
        $('h2.titlered').each(function(index) {
                var text = $(this).text();
                $(this).replaceWith('<h2 class="titlered"><span>'+text+'</span><img src="/headerred/'+escape(escape(text))+'.png" alt="'+text+'" title="'+text+'" /></h2>')
        });
        $('h2.titleblue').each(function(index) {
                var text = $(this).text();
                $(this).replaceWith('<h2 class="titleblue"><span>'+text+'</span><img src="/headerblue/'+escape(escape(text))+'.png" alt="'+text+'" title="'+text+'" /></h2>')
        });
        
        // replace h3 to img
        $('h3.title').each(function(index) {
                var text = $(this).text();
                $(this).replaceWith('<h3 class="title"><span>'+text+'</span><img src="/header/'+escape(escape(text))+'.png" alt="'+text+'" title="'+text+'" /></h3>')
        });
        
        // replace all h2 left titles to img tag
        $('h2.lefttitle').each(function(index) {
                var text = $(this).text();
                $(this).replaceWith('<h2 class="lefttitle"><span>'+text+'</span><img src="/leftheader/'+escape(escape(text))+'.png" alt="'+text+'" title="'+text+'" /></h2>')
        });
        $('h2.leftred').each(function(index) {
                var text = $(this).text();
                $(this).replaceWith('<h2 class="leftred"><span>'+text+'</span><img src="/leftred/'+escape(escape(text))+'.png" alt="'+text+'" title="'+text+'" /></h2>')
        });
        $('h2.leftblue').each(function(index) {
                var text = $(this).text();
                $(this).replaceWith('<h2 class="leftblue"><span>'+text+'</span><img src="/leftblue/'+escape(escape(text))+'.png" alt="'+text+'" title="'+text+'" /></h2>')
        });
        $('h2.leftblack').each(function(index) {
                var text = $(this).text();
                $(this).replaceWith('<h2 class="leftblack"><span>'+text+'</span><img src="/leftblack/'+escape(escape(text))+'.png" alt="'+text+'" title="'+text+'" /></h2>')
        });
        
        // replace all b title to img tag
        $('b.title').each(function(index) {
                var text = $(this).text();
                $(this).replaceWith('<img src="/subheader/'+escape(escape(text))+'.png" alt="'+text+'" title="'+text+'" />')
        });
    }
    // style & mixtra with form elements.
    $("input:text, textarea, select").uniform();
    $("input, textarea").blurRr();
    $("#openmap").fancybox({
        'type'			: 'iframe'
    });
});

function contactUs(){
    
    var name = $("#name").val();
    var surname = $("#surname").val();
    var email = $("#email").val();
    var subject = $("#subject").val();
    var message = $("#message").val();
    
    var pass = validateContactUs(name,surname,email,subject,message);
    
    if(pass){
        $.post('/ajax/contact.php', {"name":name,"surname":surname,"email":email,"subject":subject,"message":message}, function(data) {
            inform("Thank you! Your Email has been sent to us.");
        });
    } else {
        inform("Please correct the errors above.")
    }
    
    return false;  
}

function validateContactUs(name,surname,email,subject,message){
    var ok = true;
    
    /*unMarkField("name");
    if(name.length == 0 || name == "Name"){
        ok = false;
        markField("name");
    }
    
    unMarkField("surname");
    if(surname.length == 0 || surname == "Surname"){
        ok = false;
        markField("surname");
    }
    
    unMarkField("email");
    if(email.length == 0 || email == "Email"){
        ok = false;
        markField("email");
    }
    
    unMarkField("subject");
    if(subject == "-1"){
        ok = false;
        markField("subject");
    }
    
    unMarkField("message");
    if(message.length == 0 || message == "Tell us something..."){
        ok = false;
        markField("message");
    }*/
    
    return ok;  
}

function markField(id){
    $("#"+id).css("border","1px solid #e8088b");
}

function unMarkField(id){
    $("#"+id).css("border","0px solid");
}

function inform(text){
    $("#inform").html(text);
}

