
// replace font with cufon
Cufon.replace('.cufon');


blockspam = 0;
Event.addBehavior({
    'input:focus': function() {
        $(this).select();
    },
    'textarea:focus': function() {
        $(this).select();
    },
    'form#contactform:submit': function() {
        if(blockspam == 1) return false;
        blockspam = 1;
        theform = $(this);
        $(theform).down('.send').innerHTML = 'Sending';
        $(theform).down('.loader').show();
        if($(this).down('.formresp')) $($(this).down('.formresp')).remove();
        new Ajax.Request('/sendmail.php', {
            method: 'post',
            asynchronous: true,
            evalScripts: true,
            parameters: $(this).serialize(),
            onComplete: function(t) {
                responsediv = '<div class="formresp">' + t.responseText + '</div>';
                new Insertion.After($(theform).down('.button'), responsediv);
                $('contactresult').show();
                $(theform).down('.send').innerHTML = 'Send';
                $(theform).down('.loader').hide();
                blockspam = 0;
            }
        });

        return false;
    },
    'div.button .send:click': function() {
        $($(this).up('FORM')).down('.formsubmit').click();
        return false;
    }
})
document.observe("dom:loaded", function() {
    if($('reel')) {
        Event.observe($('scrollnext'), 'click', function() {
            scroller(this, 'n');
            return false;
        });
        Event.observe($('scrollprev'), 'click', function() {
            scroller(this, 'p');
            return false;
        });
        $A($('reel').getElementsByTagName('LI')).each(function(e) {
            Event.observe($(e), 'mouseover', function() {
                isMouseOverSlide = 1;
            });
            Event.observe($(e), 'mouseout', function() {
                isMouseOverSlide = 0;
            });
        });
        $A($('reel').getElementsByTagName('A')).each(function(e) {
            Event.observe($(e), 'focus', function() {
                this.blur();
            });
        });
        $$('#reel_pages a[rel=pager]').each(function(e,index) {
            Event.observe($(e), 'click', function() {
                scroller($(e), index);
                return false;
            });
        });
        autoRunReel();
    }
});


var blockclick = 0;
var isMouseOverSlide = 0;
function scroller(el, dir) {
    if(blockclick == 1) return false;
    if(isMouseOverSlide == 1) return false;

    slide = 'reel';

    blockclick = 1;
    slide_item_width = 950;
    slide_width = 950;

    // get reel width
    no_of_slides = $(slide).immediateDescendants().length;
    reel_width = no_of_slides*slide_item_width;

    // current position
    leftpos = $(slide).getStyle('left');
    if(leftpos != null) leftpos = parseInt(leftpos.sub('px', ''));

    if(is_int(dir)) {
        move = -(dir*slide_item_width);
    }
    else if(dir == 'n') {
        move = slide_width;

        if(reel_width+leftpos-move > slide_width) {
            move = leftpos-slide_width;
        }
        else if(reel_width+leftpos-move > 0) {
            move = (-reel_width)+slide_width;
        }
        else {
            move = 0;
        }
    }
    else if(dir == 'p') {
        move = slide_width;

        if(leftpos+move < 0) {
            move = leftpos+slide_width;
        }
        else if(leftpos+move < slide_width) {
            move = 0;
        }
        else if(leftpos+move == 0) {
            move = 0;
        }
        else {
            move = -(reel_width-slide_width);
        }
    }
    new Effect.Move($(slide), {
        x:move,
        y:0,
        duration:0.6,
        mode:'absolute',
        afterFinish:function() {
            blockclick = 0;
            movetotal = Math.abs(move);
            cpage = movetotal/slide_item_width;
            ccount = 0;
            $$('#reel_pages a').each(function(e) {
                if($(e).getAttribute('rel') == 'pager') {
                    if(ccount == cpage) $(e).addClassName('active');
                    else $(e).removeClassName('active');
                    ccount = ccount+1;
                }
            });
        }
    });

    return false;
}
function autoRunReel() {
    setTimeout(function() {
        scroller($('scrollnext'), 'n');
        autoRunReel();
    },5000);
}
function is_int(value){
    if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
        return true;
    } else {
        return false;
    }
}
