$(document).ready(function() {
	
	/* Roll hover images */
	var preload = new Array();
    $('img.roll').each(function (i) {
        //let's preload
        var img = new Image();
        img.src = this.src.replace(/([_-])off([._-])/, '$2on$2');
        preload.push(img);
        $(this).hover(
            function () { // over
                $(this).attr('src',this.src.replace(/([_-])off([._-])/, '$1on$2'));
            },
            function () { // out
                $(this).attr('src',this.src.replace(/([_-])on([._-])/, '$1off$2'));
            }
        );
    });

	// contact default value
	$('#name').defaultValue($('#name').attr('title'));
	$('#email').defaultValue($('#email').attr('title'));
	$('#comment').defaultValue($('#comment').attr('title'));

	// portfolio
	var slideWidth = 550;
	
	$('.slideshow .prev').css('opacity', .4);
	$('.slideshow .container').each(function(){
		$(this).css('width', $(this).find('img').length * slideWidth);
	});
	
	$('.slideshow .prev').click(function() {
		var slideshow = $(this).closest('.slideshow');
		var container = slideshow.find('.container');
		var page = Math.round(Math.abs(parseInt(container.css('left'))) / slideWidth);
		
		slideshow.find('.next').css('opacity', 1);
		
		if (page > 0)
			container.animate({left: --page * -slideWidth})
		
		if (page == 0)
			slideshow.find('.prev').css('opacity', .4);
	});
	$('.slideshow .next').click(function() {
		var slideshow = $(this).closest('.slideshow');
		var container = slideshow.find('.container');
		var maxPage = container.find('img').length - 1;
		var page = Math.round(Math.abs(parseInt(container.css('left'))) / slideWidth);
		
		slideshow.find('.prev').css('opacity', 1);
		
		
		if (page < maxPage)
			container.animate({left: ++page * -slideWidth})
		
		if (page == maxPage)
			slideshow.find('.next').css('opacity', .4);
	});

	/*$('#main-container').scrollview({
	    grab : "assets/img/openhand.cur",
	    grabbing : "assets/img/closedhand.cur"
	});*/
	
	$.localScroll.defaults.axis = 'x';

	$.localScroll({
		target: '#main-container',
		easing: "easeOutExpo",
		queue: true,
		duration: 800
	});
	$('#main-container').scrollTo(0);
	
	accordion($('.accordion'));
});

function accordion (container)
{
	var current = false;

	$('> div', container).css('display', 'none');
	$('> h3', container).click(function() {
		if (this != current) {
			$(this).parent().find('> div').slideUp();
			$(this).next().slideDown().show(function(){$(this).show();});
			current = this;
		} else {
			$(this).next().slideUp();
			current = false;
		}
	});
}

/**
 * ScrollView - jQuery plugin 0.1
 *
 * This plugin supplies contents view by grab and drag scroll.
 *
 * Copyright (c) 2009 Toshimitsu Takahashi
 *
 * Released under the MIT license.
 *
 * == Usage =======================
 *   // apply to block element.
 *   $("#map").scrollview();
 *   
 *   // with setting grab and drag icon urls.
 *   //   grab: the cursor when mouse button is up.
 *   //   grabbing: the cursor when mouse button is down.
 *   //
 *   $("#map".scrollview({
 *     grab : "images/openhand.cur",
 *     grabbing : "images/closedhand.cur"
 *   });
 * ================================
 */

/*(function() {
    function ScrollView(){ this.initialize.apply(this, arguments); }
    ScrollView.prototype = {
        initialize: function(container, config){
                // setting cursor.
                var gecko = navigator.userAgent.indexOf("Gecko/") != -1;
                var opera = navigator.userAgent.indexOf("Opera/") != -1;
                var mac = navigator.userAgent.indexOf("Mac OS") != -1;
                if (opera) {
                    this.grab = "default";
                    this.grabbing = "move";
                } else if (!(mac && gecko) && config) {
                    if (config.grab) {
                       this.grab = "url(\"" + config.grab + "\"),default";
                    }
                    if (config.grabbing) {
                       this.grabbing = "url(" + config.grabbing + "),move";
                    }
                } else if (gecko) {
                    this.grab = "-moz-grab";
                    this.grabbing = "-moz-grabbing";
                } else {
                    this.grab = "default";
                    this.grabbing = "move";
                }
                
                // Setup Mouse gestures
                var vx = 0, vy = 0;//, px = 0, py = 0, justClicked = false;
                
                // Get container and image.
                this.m = $(container);
                this.i = this.m.children().css("cursor", this.grab);
                
                this.isgrabbing = false;
                
                // Set mouse events.
                var self = this;
                this.i.mousedown(function(e){
                        vx = 0;
                        vy = 0;
                        self.startgrab();
                        this.xp = e.pageX;
                        this.yp = e.pageY;
                        return false;
                }).mousemove(function(e){
                        if (!self.isgrabbing) return true;
						
						justClicked = true;
						self.m.clearQueue();
						vx = -(e.pageX - this.xp);
                        vy = -(e.pageY - this.yp);

						self.scrollTo(this.xp - e.pageX, this.yp - e.pageY);
						
						this.xp = e.pageX;
                        this.yp = e.pageY;
						
                        return false;
                })
                .mouseout(function(){ self.stopgrab(); })
                .mouseup(function(){
                        self.stopgrab();
                        
						if (justClicked) {
							
                        	self.m.clearQueue().animate({ scrollLeft: '+=' + (vx * 3), scrollTop: '+=' + (vy * 3) }, 600, "easeOutCubic");
                        }
						justClicked = false;
                        
						//justClicked = true;
                        setTimeout( function() { justClicked = true; }, 600);
                })

        },

        startgrab: function(){
                this.isgrabbing = true;
                this.i.css("cursor", this.grabbing);
        },
        stopgrab: function(){
                this.isgrabbing = false;
                this.i.css("cursor", this.grab);
        },
        scrollTo: function(dx, dy){
                var _m = this.m;
                var x = _m.scrollLeft() + dx;
                var y = _m.scrollTop() + dy;
                _m.scrollLeft(x).scrollTop(y);
				//_m.clearQueue().animate({ scrollLeft: x, scrollTop: y}, 20, "easeOutCubic");
        }
    };
    
    jQuery.fn.scrollview = function(config){
        return this.each(function(){
            new ScrollView(this, config);
        });
    };
})(jQuery);*/
