(function($){

	$.fn.extend({

		init_Slider: function(e){
			
			return this.each(function(){
				
				var oParent = $(this);
				var oItem = oParent.find(e.oItem);
				var oHolder = oParent.find(e.oHolder);
				var oController = oParent.find(e.oController);
				var nWidth = oItem.width();
				var nPadding = parseInt(oItem.css('paddingLeft'), 10) + parseInt(oItem.css('paddingRight'), 10);
				var nMargin = parseInt(oItem.css('marginLeft'), 10) + parseInt(oItem.css('marginRight'), 10);
				var nBorder = parseInt(oItem.css('borderLeftWidth'), 10) + parseInt(oItem.css('borderRightWidth'), 10);
				var nVisItem = parseInt(oHolder.width()/nWidth,10);

				nWidth += (nPadding)? nPadding:0;
				nWidth += (nMargin)? nMargin:0;
				nWidth += (nBorder)? nBorder:0;

				oParent.data('nVisItem',nVisItem);
				oParent.data('nCurrItem',0);

				oHolder.width(nWidth*oItem.length);
				
				oController.bind({'mousedown':function(){
					$(this).not(".disabled").addClass('click');
				},'mouseup':function(){
					$(this).removeClass('click');
				},'mouseout':function(){
					$(this).removeClass('click');
				}});

				if (oItem.length <= nVisItem) {
					oController.hide();
				} else {

					oController.filter(".prev").addClass('disabled').bind('click',function(){
						var nCurrItem = oParent.data('nCurrItem');
						oController.filter(".next").removeClass('disabled');
						nCurrItem -= 1;		
						if (nCurrItem<0) {
							nCurrItem=0;
						} else if (nCurrItem<1) {
							$(this).addClass('disabled');
						}
						oParent.data('nCurrItem',nCurrItem); // set new
						oHolder.animate({'left':-(nCurrItem*nWidth)},e.slideTime,e.easing);	
						return false;
					});

					oController.filter(".next").bind('click',function(){
						var nCurrItem = oParent.data('nCurrItem');
						var nVisItem = oParent.data('nVisItem');
						oController.filter(".prev").removeClass('disabled');
						nCurrItem += 1;		
						if (nCurrItem>oItem.length-nVisItem) {
							nCurrItem=oItem.length-nVisItem;
						} else if (nCurrItem>oItem.length-nVisItem-1) {
							$(this).addClass('disabled');
						}
						oParent.data('nCurrItem',nCurrItem); // set new
						oHolder.animate({'left':-(nCurrItem*nWidth)},e.slideTime,e.easing);		
						return false;
					});

				}

			});

		}

	});

})(jQuery);




$(document).ready(function(){

	// webkit browsers load CSS and JS at the same time so make them wait a bit more
	if (jQuery.browser.safari && document.readyState != "complete") { setTimeout( arguments.callee, 100 ); return; }

	if ($("div#footer").length>0) {
		$("div#footer").init_Slider({
			'slideTime': 500,
			'easing': 'easeOutQuint',
			'oHolder': 'div.articles div.holder',
			'oItem': 'div.articles div.article',
			'oController': "div.controllers a"
		});
	}

	if ($("div.projects").length>0) {
		$('div.projects').init_Slider({
			'slideTime': 500,
			'easing': 'easeOutQuint',
			'oHolder': 'div.holder',
			'oItem': 'div.project',
			'oController': "div.controllers a"
		});
	}


	
	if ($.isFunction($.fn.slider)===true) {

		//scrollpane parts
		var scrollPane = $('div.projects');
		var scrollContent = scrollPane.find('div.holder');
		
		//build slider
		var scrollbar = $("div.scrollbar").slider({
			slide:function(e, ui){
				if( scrollContent.width() > scrollPane.width() ){ 
					scrollContent.css('marginLeft', Math.round( ui.value / 100 * ( scrollPane.width() - scrollContent.width() )) + 'px');
					//console.log(ui.value / 100 * scrollContent.width() );
				} else { 
					scrollContent.css('marginLeft', 0);
				}
			}
		});

	}
	
	if ($('div.screenshot a').length>0) {
		$('div.screenshot a').bind('click',function(){
			var src = $(this).attr('href');
			var index = $(this).parent().index();
			$('div.project-view div.canvas img').attr('src',src);
			$('div.screenshot a').removeClass('active').eq(index).addClass('active');
			$('div.dots a.dot').removeClass('active').eq(index).addClass('active');
			return false;
		}).eq(0).trigger('click');
	}

	if ($('div.dots a.dot').length>0) {
		$('div.dots a.dot').bind('click',function(){
			var src = $(this).attr('href');
			var index = $(this).index();
			$('div.project-view div.canvas img').attr('src',src);
			$('div.screenshot a').removeClass('active').eq(index).addClass('active');
			$('div.dots a.dot').removeClass('active').eq(index).addClass('active');
			return false;
		});
	}




	if ($.isFunction($.fn.hint)===true) {
		$('form input[title!=""]').hint();
		$('form textarea[title!=""]').hint();
	}
	
	if ($.isFunction($.fn.colorbox)===true) {
		$('a.colorbox').colorbox();
	}



});
