jQuery(document).ready(function() {




jQuery.fn.extend({
  slideRightShow: function() {
    return this.each(function() {
        $(this).show('slide', {direction: 'right'}, 1000);
    });
  },
  slideLeftHide: function() {
    return this.each(function() {
      $(this).hide('slide', {direction: 'left'}, 1000);
    });
  },
  slideRightHide: function() {
    return this.each(function() {
      $(this).hide('slide', {direction: 'right'}, 1000);
    });
  },
  slideLeftShow: function() {
    return this.each(function() {
      $(this).show('slide', {direction: 'left'}, 1000);
    });
  }
});

(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
            
           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);
function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
function trim(str) {
	return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}




//enter page and setup portfolio

var tempArray1 = jQuery("#sub-content div");
var tempArray = [];
//alert(tempArray1.length);
if (tempArray1!=null) {
	//tempArray1= trim(tempArray1);
	//tempArray1 = tempArray1.split(" ");
	//remove empty elements
	for (i=0;i<=tempArray1.length-1;i++) {
		if (tempArray1[i]!='') {
			tempArray[i]=tempArray1[i];
			jQuery(tempArray[i]).hide();
			jQuery('<li></li>').appendTo('#controls');
		}
	}
	jQuery(tempArray[0]).show();
	jQuery('#controls li').first().addClass('selected');
	var count = 0;
	var timer = setInterval(cycleSlideShow,5000);
}

function cycleSlideShow() {
			
			var limit = tempArray.length-1;
			//jQuery("#sub-content div").css({'height' : '533px', 'position':'absolute'});
			//alert('t')
			//slideRightElement(0,1);
		if (currentSection==limit) {
			slideRightElement(tempArray.length-1,0);
			currentSection = 0;
			jQuery('#controls li').removeClass('selected');
			jQuery('#controls li').eq(currentSection).addClass('selected');
		}		
		else {
			slideRightElement(currentSection,currentSection+1);
			currentSection = currentSection +1;
			jQuery('#controls li').removeClass('selected');
			jQuery('#controls li').eq(currentSection).addClass('selected');

			}	
		//setRightBtn();
		
}

//transition
function slideLeftElement(sectionOut,sectionIn){
		//setButtons(sectionIn);
		//setSection(sectionIn);
		//setEachBtn(sectionIn);//dev-1
	//currentSection = searchArray(sectionIn);
	var tempSec = "#sub-content div#feature-" + sectionIn;
	var tempSec1 = "#sub-content div#feature-" + sectionOut;
	jQuery(tempSec1).hide("slide", { direction: "right" }, 500);
	jQuery(tempSec).show("slide", { direction: "left" }, 500);
	}


function slideRightElement(sectionOut,sectionIn){
		//setButtons(sectionIn);
		//setSection(sectionIn);
		//setEachBtn(sectionIn);//dev-1
	//currentSection = searchArray(sectionIn);
	
	var tempSec = "#sub-content div#feature-" + sectionIn;
	var tempSec1 = "#sub-content div#feature-" + sectionOut;
	jQuery(tempSec1).hide("slide", { direction: "left" }, 500);
	jQuery(tempSec).show("slide", { direction: "right" }, 500);
	}
//slideRightElement(in,out)
//transitions
//setSection
function setSection(section) {
	var sec = section.substring(0,3);
	jQuery("#sub-nav ul li").css({'background-position' : '0px -50px'});
	jQuery("#sub-nav ul li").removeClass('selected');
	tempVar = '#sub-nav ul li.'+sec;
	jQuery(tempVar).addClass('selected');
	jQuery(tempVar).css({'background-position' : '0px 0px'});
};
//setButtons

//slideLeftElement(in,out)

//mouse events
jQuery("#sub-content div").click(
		function(){
			clearInterval(timer);	
		}
							
							);
jQuery(".left-arrow").click(
		function(){
			jQuery(this).die('click',slideLeftElement);
					clearInterval(timer);	
			var limit = tempArray.length-1;

		if (currentSection==0) {
			slideLeftElement(0,tempArray.length-1);
			currentSection = tempArray.length-1;
						jQuery('#controls li').removeClass('selected');
			jQuery('#controls li').eq(currentSection).addClass('selected');

		}		
		else {
			slideLeftElement(currentSection,currentSection-1);
			currentSection = currentSection -1;
						jQuery('#controls li').removeClass('selected');
			jQuery('#controls li').eq(currentSection).addClass('selected');

			}
		});	

	
	jQuery(".right-arrow").click(
								 
		function() {	
					clearInterval(timer);	

			var limit = tempArray.length-1;
			//jQuery("#sub-content div").css({'height' : '533px', 'position':'absolute'});
			//alert('t')
			//slideRightElement(0,1);
		if (currentSection>=limit) {
			slideRightElement(tempArray.length-1,0);
			currentSection = 0;
			jQuery('#controls li').removeClass('selected');
			jQuery('#controls li').eq(currentSection).addClass('selected');
		}		
		else {
			slideRightElement(currentSection,currentSection+1);
			currentSection = currentSection +1;
			jQuery('#controls li').removeClass('selected');
			jQuery('#controls li').eq(currentSection).addClass('selected');
			}	
		//setRightBtn();
		}
		);
	jQuery("#portfolio-btns ul li.right-btn").hover(
		function(){
			var whatsec = jQuery(this).attr('class').split(' ');
			var whatbtn = "#portfolio-btns ul li."+whatsec[0]+" img.hidden-btn";
			jQuery(whatbtn).fadeIn(100);		
		},
		function(){
			jQuery("#portfolio-btns ul li.right-btn img.hidden-btn").fadeOut(100);
			
		});
//left clicks - slideLeftElement(in,out)
//right - slideRightElement(in,out)
//each-btn clicks - find current and increment +/- depending on <=> -compareSelectedVsCurrent(sel)
//logo clicks - find current and increment +/- depending on <=> -compareSelectedVsCurrent(sel)
jQuery("#portfolio-cats ul li img").click(
		function(){
			var whatbtn = jQuery(this).attr('class').split(' ');
			compareSelectedVsCurrent(whatbtn[0]);
});	
//section clicks set substring(3)-1
jQuery("#sub-nav ul li").click(
	function(){
		
	if (findClass(this)[1]=='selected'){}
	else {
		var sec = findClass(this)[0].substring(0,3)+'-1';
		//tempPos = searchArray(sec);
		compareSelectedVsCurrent(sec);
	}
});
//utility functions

//search order array
function searchArray(pos) {
	for (i=0;i<=tempArray.length-1;i++) {
		if (tempArray[i]==pos) {
		var position = i;
		}
	}
	return position;
};
//compare 
function compareSelectedVsCurrent(sel){

if(searchArray(sel)<currentSection) {
	
	slideLeftElement(tempArray[currentSection],sel);
}
if(searchArray(sel)==currentSection) {} 

if(searchArray(sel)>currentSection) {
	slideRightElement(tempArray[currentSection],sel);
	}
}
//find class of element
function findClass(inSection) {
	var whatsec = jQuery(inSection).attr('class').split(' ');
	return whatsec;
	
}

function startSlideShow() {
		var limit = tempArray.length-1;
		if (currentSection==limit) {
			slideRightElement(tempArray[tempArray.length-1],tempArray[0]);
		}		
		else {
			slideRightElement(tempArray[currentSection],tempArray[currentSection+1]);
			}
}

function loadSection(section) {
	var tempSec = ".portfolio-content ul li." + section;
	jQuery(".portfolio-content ul li").hide();
	jQuery(tempSec).fadeIn(100);
};

//big function 
function setButtons(section) {
	var tempReset = resetButtons.clone(true);
	jQuery("#portfolio-btns").replaceWith(tempReset);
	var sec = section.substring(0,3);
	var tempLen = '.portfolio-content ul li.'+ sec;
 	var len = jQuery(tempLen).length;
	var tempbtn = jQuery("#portfolio-btns ul li.each-btn").clone(true);
	
	jQuery("#portfolio-btns ul li.each-btn").addClass(sec+"-1");

	for (i=2;i<=len;i++)
	{
		var eachbtn = tempbtn.clone(true);
		var nextClass = sec+"-"+(i);	
		eachbtn.addClass(nextClass);
		eachbtn.insertBefore("#portfolio-btns ul li.marker");
	
	}
	
	jQuery("#portfolio-btns ul li.each-btn").click(
			function(){
				var whatbtn = jQuery(this).attr('class').split(' ');
				if (whatbtn[2]=='selected'){}
				else {
					compareSelectedVsCurrent(whatbtn[1]);
				jQuery("#portfolio-btns ul li.each-btn img.hidden-btn").fadeOut(100);
				}
		});
	jQuery("#portfolio-btns ul li.each-btn").hover(
		function(){
			var whatsec = jQuery(this).attr('class').split(' ');
			var whatbtn = "#portfolio-btns ul li."+whatsec[1]+" img.hidden-btn";
			jQuery(whatbtn).fadeIn(100);		
		},
		function(){
			var whatsec = jQuery(this).attr('class').split(' ');
			if (whatsec[2]!='selected') {
				jQuery("#portfolio-btns ul li.each-btn img.hidden-btn").fadeOut(100);
			}
		});
};




function setEachBtn(btn) {
	tempEach = "#portfolio-btns ul li." + btn;
	jQuery("#portfolio-btns ul li.each-btn").css({'background-position' : '0px -18px'});
	jQuery("#portfolio-btns ul li.each-btn").removeClass('selected');
	jQuery(tempEach).css({'background-position' : '0px 0px'});
	jQuery(tempEach).addClass('selected');
};




var rightButton = jQuery('.right-arrow');

var resetButtons =   jQuery("#portfolio-btns").clone(true); 	 
var currentSection = 0;

//setButtons('dev');
//setSection('dev');
//loadSection('dev-1');
jQuery("#portfolio-btns").remove();
//var timerPortfolio = setInterval(startSlideShow,5000);
//setEachBtn('dev-1');
});


