var sfTimeout = null;
$(document).ready(function(){ 
    $('img[src$=".png"]').ifixpng();  
    $('#contentContainer').ifixpng();

    $('textarea').each(function() {
        if (jQuery.trim($(this).text()) == "") {
            $(this).html("");
        }
    });

    $('#services a', '#homepage').each( function() {
        $(this).attr("href", 'javascript:void(0);');
    });

    $('#topNav > li').hover(
        function() {
            window.clearTimeout(sfTimeout);
            $('#topNav > li').removeClass('sfHover');
            $(this).addClass('sfHover');
        },
        function() {
            sfTimeout = window.setTimeout(hideSubNav, 500, this);
        }
    );
});

function hideSubNav(el) {
    $('#topNav > li').removeClass('sfHover');
}

function showService(el, id) {
    if ($('#largeImage .text').length == 1) {
        $('#largeImage .text').remove();
        $('#largeImage').attr("style", "");
    }
    // remove other services which are currently selected
    $('#services .selected').removeClass("selected");
    $('#largeImage .service:visible').fadeOut();
    // select this one
    $(el).addClass("selected");
    $('#service'+id).fadeIn();
}

function latestNewsLeft() {
    // move the #itemGroupRight to the left
    $('#itemGroupRight').css("margin-left", "-782px");
    // move the #itemGroupMiddle one out and the #itemGroupRight one in 
    $('#itemGroupRight').animate({"marginLeft": "0px"});
    $('#itemGroupMiddle').animate({"marginLeft": "782px"}, function() {
        // now to swap around some ids
        $('#itemGroupMiddle').attr("id", "itemGroupRight2");
        $('#itemGroupRight').attr("id", "itemGroupMiddle2");

        $('#itemGroupMiddle2').attr("id", "itemGroupMiddle");
        $('#itemGroupRight2').attr("id", "itemGroupRight");
    });
}

function latestNewsRight() {
    // move #itemGroupRight to the right
    $('#itemGroupRight').css("margin-left", "782px");
    // move the middle one out and the right one in 
    $('#itemGroupRight').animate({"marginLeft": "0px"});
    $('#itemGroupMiddle').animate({"marginLeft": "-782px"}, function() {
        // now to swap around some ids
        $('#itemGroupMiddle').attr("id", "itemGroupRight2");
        $('#itemGroupRight').attr("id", "itemGroupMiddle2");

        $('#itemGroupMiddle2').attr("id", "itemGroupMiddle");
        $('#itemGroupRight2').attr("id", "itemGroupRight");
    });
}

function imagePreview(el, pos, newsId, imageSrc) {
    $('.selected', '#newsItem'+newsId+' .gallery').removeClass("selected");
    $(el).addClass("selected");
    console.log($(el));
    $('.largePreview', '#newsItem'+newsId).attr("src", imageSrc);
    $('.currentImage', '#newsItem'+newsId+' .gallery').html(pos);
}

function galleryRight(el, newsId) {
    if (!$('.rightArrow', '#newsItem'+newsId+' .gallery').parent('a').hasClass("disabled")) {
        $('.leftArrow', '#newsItem'+newsId+' .gallery').parent('a').removeClass("disabled");
        $('.imageContainer', '#newsItem'+newsId+' .gallery').animate({"marginLeft": "-=141px"}, function() {
            // do we need to disable the right arrow?
            var marginLeft = $('.imageContainer', '#newsItem'+newsId+' .gallery').css("margin-left").replace(/px/, "") + 141;
            var width = $('.imageContainer', '#newsItem'+newsId+' .gallery').css("width").replace(/px/, "");
            if (Math.abs(marginLeft) >= width) {
                //$('.imageContainer', '#newsItem'+newsId+' .gallery').css("margin-left", width+"px");
                $('.rightArrow', '#newsItem'+newsId+' .gallery').parent('a').addClass("disabled");
            }
        });
    }
}

function galleryLeft(el, newsId) {
    if (!$('.leftArrow', '#newsItem'+newsId+' .gallery').parent('a').hasClass("disabled")) {
        $('.rightArrow', '#newsItem'+newsId+' .gallery').parent('a').removeClass("disabled");
        $('.imageContainer', '#newsItem'+newsId+' .gallery').animate({"marginLeft": "+=141px"}, function() {
            // do we need to disable the right arrow?
            var marginLeft = $('.imageContainer', '#newsItem'+newsId+' .gallery').css("margin-left").replace(/px/, "");
            if (marginLeft >= 0) {
                $('.imageContainer', '#newsItem'+newsId+' .gallery').css("margin-left", "0px");
                $('.leftArrow', '#newsItem'+newsId+' .gallery').parent('a').addClass("disabled");
            }
        });
    }
}