

function doit(_parent, _formname, _itemname, _action, _value, _text) 
{

	if (_parent)
	{
		var f = parent.document.getElementById(_formname);
	}
	else
	{
		var f = document.getElementById(_formname);
	} //if
//alert(f);
//	base = f[_itemname];
//	root = f[_itemname];

	var base = document.getElementById(_itemname);
	var root = document.getElementById(_itemname);
//alert(base);
    switch (_action) {
        case 'add' :
            ++root.length;

            root.options[root.length-1].text  = _text;
            root.options[root.length-1].value = _value;
        break;
        
        case 'getlength':
        	return root.length;
        break;
        
        //copy all the elements from source (_value) select
        case 'copyfrom':
        	var src = document.getElementById(_value);

        	//clear the array first
        	doit(0,_formname,_itemname,'clear','','');

        	for (var i=0; i < src.length; i++)
        	{
				doit(0,_formname,_itemname,'add', src.options[i].value, src.options[i].text);
        	} //for
        break;

        case 'clear' :
            $meret = base.length-1;
            for($i=$meret; $i > -1; $i--) {
                    base.options[$i] = null;
            } //for
        break;
        
		case 'del' :
			$meret = base.length-1;
			for($i=$meret; $i > -1; $i--) { 
				if (base[$i].selected) {
					base[$i] = null;
				} //if
			} //for
		break;

		case 'setselval' :
            meret = base.length-1;
			for($i=meret; $i > -1; $i--) 
			{ 
				if (base[$i].value == _value) 
				{
					base[$i].selected = true;
				} //if
			} //for
		break;

		case 'setselvalonly' :
			meret = base.length-1;
			tru = false;
        		
			for($i=meret; $i > -1; $i--) 
			{ 
				if (base[$i].value == _value) 
				{
					base[$i].selected = true;
					tru = true;
				} //if
			} //for
			if (!tru)
			base[0].selected = true;
		break;

		case 'getselval' :
			return root.options[root.selectedIndex].value;
		break;

		case 'getseltext' :
			return root.options[root.selectedIndex].text;
		break;

        case 'selectall' :
			for($i=0; $i < base.length; $i++) {
				base[$i].selected = true;
            } //for
        break;
        
        case 'moveup' :
			tmp = new Array(2);
			sel = base.selectedIndex;
			all = base.length;
			if (sel > 0) {
				irany = -1;
				tmp[0] = base[sel+irany].text;
				tmp[1] = base[sel+irany].value;
				
				base[sel+irany].text = base[sel].text;
				base[sel+irany].value = base[sel].value;
				base[sel+irany].selected = true;
				base[sel].text = tmp[0];
				base[sel].value = tmp[1];
				base[sel].selected = false;
			} //if
        break;
        
        case 'movedown' :
			tmp = new Array(2);
			sel = base.selectedIndex;
			all = base.length;
			if (sel < all-1) {
				irany = 1;
				tmp[0] = base[sel+irany].text;
				tmp[1] = base[sel+irany].value;
				
				base[sel+irany].text = base[sel].text;
				base[sel+irany].value = base[sel].value;
				base[sel+irany].selected = true;
				base[sel].text = tmp[0];
				base[sel].value = tmp[1];
				base[sel].selected = false;
			} //if
        break;
    } //switch
} //function


$(document).ready(function() {
	//Tooltips
	$(".tip_trigger").hover(function(){
		tip = $(this).find('.tip');
		tip.show(); //Show tooltip
	}, function() {
		tip.hide(); //Hide tooltip		  
	}).mousemove(function(e) {
		var mousex = e.pageX + 20; //Get X coodrinates
		var mousey = e.pageY + 20; //Get Y coordinates
		var tipWidth = tip.width(); //Find width of tooltip
		var tipHeight = tip.height(); //Find height of tooltip
		
		//Distance of element from the right edge of viewport
		var tipVisX = $(window).width() - (mousex + tipWidth);
		//Distance of element from the bottom of viewport
		var tipVisY = $(window).height() - (mousey + tipHeight);
		  
		if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
			mousex = e.pageX - tipWidth - 20;
		} if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
			mousey = e.pageY - tipHeight - 20;
		} 
		tip.css({  top: mousey, left: mousex });
	});





	//Tabs   
	//Default Action
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content
	
	//On Click Event
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active content
		return false;
	});




    // Slider
	//Set Default State of each portfolio piece
	$(".paging").show();
	$(".paging a:first").addClass("active");
		
	//Get size of images, how many there are, then determin the size of the image reel.
	var imageWidth = $(".window").width();
	var imageSum = $(".image_reel img").size();
	var imageReelWidth = imageWidth * imageSum;
	
	//Adjust the image reel to its new size
	$(".image_reel").css({'width' : imageReelWidth});
	
	//Paging + Slider Function
	rotate = function(){	
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

		$(".paging a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
		
		//Slider Animation
		$(".image_reel").animate({ 
			left: -image_reelPosition
		}, 500 );
		
	}; 
	
	//Rotation + Timing Event
	rotateSwitch = function(){		
		play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
			$active = $('.paging a.active').next();
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 7000); //Timer speed in milliseconds (3 seconds)
	};
	
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$(".image_reel a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation
	});	
	
	//On Click
	$(".paging a").click(function() {	
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation
		return false; //Prevent browser jump to link anchor
	});	

});


