var imagePos;

$(document).ready(function(){

    // Hover on side images
    $("ul.topImages li a, a.sideImg").hover(
      function () {
        $(this).children("img").animate({
        opacity: "0.5"
      }, 300 );
    },
      function () {
        $(this).children("img").animate({
        opacity: "1.0"
      }, 300 );
    });

    // Mask over featured box
    $(".homeLeftBox").append("<span class='featuredMask'></span>")

    /* Hover on home news boxes
    $("ul.homeNewsBox li span").css("opacity","0.0");
    $("ul.homeNewsBox li").hover(
      function () {
        $(this).children("span").animate({
        opacity: "1.0"
      }, 250 );
    },
      function () {
        $(this).children("span").animate({
        opacity: "0.0"
      }, 250 );
    });*/

    /* Remove title attribute on Featured divs
    $(".panel").hover(function () {$(this).removeAttr("title");},function () {$(this).addAttr("title");});*/

    // Add file upload boxes in Brief
    $(".attachFileButton").click(function () {$('.fileField').append('<input type="file" name="attachment[]"/><br />');$('fieldset.addFilesBox span').fadeIn(); return false;});

    var example = 3;

    $("#addurl").click(function () {
        example++;
        alert(example);
        $('#similarprojects').append('<li><label>Example '+example+':</label><input type="text"/></li>');
        return false;
        });

    // Focus/blur on form elements
    $("#innerPageContent input[type=text], #formsContent input[type=text]").focus(function () {$(this).addClass("fieldFocus");});
    $("#innerPageContent input[type=text], #formsContent input[type=text]").blur(function () {$(this).removeClass("fieldFocus");});

    $("fieldset.newsletterBox span input").focus(function () {
        $(this).parent().css({ "backgroundPosition":"0 -25px"});if(this.value==this.defaultValue)this.value='';});
    $("fieldset.newsletterBox span input").blur(function () {
        $(this).parent().css({ "backgroundPosition":"0 0"});if(this.value=='')this.value=this.defaultValue;});

    $("fieldset.contactNewsletterBox span input").focus(function () {
        $(this).parent().css({ "backgroundPosition":"0 -27px"});if(this.value==this.defaultValue)this.value='';});
    $("fieldset.contactNewsletterBox span input").blur(function () {
        $(this).parent().css({ "backgroundPosition":"0 0"});if(this.value=='')this.value=this.defaultValue;});

    $("fieldset.rightFieldset span textarea").focus(function () {
        $(this).parent().css({ "backgroundPosition":"0 -224px"});if(this.value==this.defaultValue)this.value='';});
    $("fieldset.rightFieldset span textarea").blur(function () {
        $(this).parent().css({ "backgroundPosition":"0 0"});if(this.value=='')this.value=this.defaultValue;});

    $("#formsContent span.textareaBox textarea").focus(function () {
        $(this).parent().css({ "backgroundPosition":"0 -113px"});if(this.value==this.defaultValue)this.value='';});
    $("#formsContent span.textareaBox textarea").blur(function () {
        $(this).parent().css({ "backgroundPosition":"0 0"});if(this.value=='')this.value=this.defaultValue;});

    $('#footrList a, a.viewOnlineButton').click(function(){
        window.open(this.href);
        return false;
    });

    // $('.customCombo').combobox();

    /* Get absolute coordinates of top left corner of image. */
    image = $("#slider1");
    image = image[0];
    imagePos = findPos(image);

    $("#slider1").mousemove(function(e) {
        /* Get mouse coordinates relative to top left corner of image. */
        var x = e.pageX - imagePos[0];
        var y = e.pageY - imagePos[1] - 30;

        /* Get image width and height. */
        var height = this.offsetHeight;
        var width = this.offsetWidth;

        /* Make the arrow stop at top and bottom margins. */
        var margin_top = 36;
        var margin_bottom = this.offsetHeight - margin_top - 60;

        if (y <= margin_top) {
            y = margin_top;
        } else if (y >= margin_bottom) {
            y = margin_bottom;
        }

        /* Set new arrow coordinates. */
        $("#stripNavL0").css("top", y);
        $("#stripNavR0").css("top", y);

        var left = x < this.offsetWidth / 2;

        /* Check which side of image has been entered. */
        if (left) {
            $("#stripNavL0").show();
            $("#stripNavR0").fadeOut("fast");
        } else {
            $("#stripNavR0").show();
            $("#stripNavL0").fadeOut("fast");
        }
    });

    $("#workMainBox").mouseleave(function() {
        $("#stripNavL0").fadeOut("fast");
        $("#stripNavR0").fadeOut("fast");
    });

 });

// Go through all parents and add their offsets to get position relative to document.
function findPos(obj){
    var posX = obj.offsetLeft;
    var posY = obj.offsetTop;

    while(obj.offsetParent){
        if (obj==document.getElementsByTagName('body')[0]) {
            break;
        } else {
            posX=posX+obj.offsetParent.offsetLeft;
            posY=posY+obj.offsetParent.offsetTop;
            obj=obj.offsetParent;
        }
    }
    var posArray=[posX,posY];
    return posArray;
}


