/**
 * This is used to toggle
 * the fields value. So if
 * you click it will clear 
 * and then revert if nothing has
 * been added
 * 
 * to init a field just call add_toggle(element_id);
 * from where ever.
 * 
 * 
 */ 
function add_toggle(field) {
    $("#"+field).focus(function() {
        if( this.value == this.defaultValue ) {
            this.value = "";
        }
    }).blur(function() {
        if( !this.value.length ) {
            this.value = this.defaultValue;
        }
    });
}


function sticky_nav(element)
{
    $(".btns").children("li").each(function(count,ele){
        $(ele).children("a:first").css("background-position","left");
    });
    $(element).children("a:first").css("background-position","right");
}

function sticky_nav_portfolio(element)
{
    $("#nav_port a").each(function(count,ele){
        $("#"+ele.id).removeClass('navbtn-selected');
    });
    $("#"+element).addClass('navbtn-selected');
}
function stick_nav(id)
{
    var classes = $("#"+id).attr("class");
	
    var tmp = new Array();
	
    tmp = classes.split(' ');
	
    for(i=0;i<tmp.length;i++)
    {
        if(tmp[i].substring(0,3) == "cat")
        {
            cat = tmp[i].substring(3,tmp[i].length);
            if(!$("#"+cat).hasClass("navbtn-selected")) sticky_nav_portfolio(cat);
        }
    }
	
}

function next_project(e)
{
    if($('#projects div.current').hasClass('last')) {
    var most_recent_id = get_most_recent_project_id();

        stick_nav(most_recent_id);
        to_project(most_recent_id, 1200);
    }
    else {
        var new_id = new_proj_id('next');
        stick_nav(new_id);
        to_project(new_id, 600);
    }
    return preventDefaultAction(e);

}
function previous_project(e) 
{
    //alert('?')
    if($('#projects div.current').hasClass('first')) {
        var last_id = get_last_project_id();
        stick_nav(last_id);
        to_project(last_id, 1200);
    }
    else {
        var new_id = new_proj_id('previous');
        stick_nav(new_id);
        to_project(new_id, 600);
    }
    return preventDefaultAction(e);
}

function most_recent_project() {
    var most_recent_id = get_most_recent_project_id();
    stick_nav(most_recent_id);
    to_project(most_recent_id, 1200);
}

function get_last_project_id() {
    return $("#projects div.last").attr("id");
}


function to_project(new_id, speed) 
{
    if(speed) {
        $("#projects").animate({
            left: 0-$('#' + new_id).position().left
        }, speed);
        set_current(new_id);
    }
    else {
        $("#projects").animate({
            left: 70-$('#' + new_id).position().left
        }, 600);
        set_current(new_id);
    }
}
function set_current(id) 
{
    $("#projects .current").removeClass('current');
    $('#' + id).addClass('current');
}

function new_proj_id(which) 
{
    current = $("#projects div.current").attr("id");
	
    if(which=='next')
        new_id = $("#projects div.current").next().attr("id");
    else
        new_id = $("#projects div.current").prev().attr("id");
    //OLD CODE -- needed to change so that updates are easy
    //	var num = current.substring(current.lastIndexOf('_') + 1, current.length);
    //	if(which == 'next')
    //		num++;
    //	else
    //		num--;
    //	var new_id = 'project_' + num;
    return new_id;
}

function preventDefaultAction(e)	{
    if (e)	{
        if (typeof e.preventDefault != 'undefined')	{
            e.preventDefault();
        }
        else	{
            e.returnValue = false;
        }
    }
    // safey for handling DOM Level 0
    return false;
}

//Created this for portfolio page
function jump_to(cat)
{
    id = $("#projects div."+cat).attr("id");
    if(id != undefined) to_project(id, 600);
	
}

function port_init()
{
    if($("#has_loaded").attr("rel") == "no")
    {
        // alert('working');
        $('#projects').load("port_cats.php",'',function(){			

            var left = 775;
            $('#projects').children().each(function(child,aab){
				
                $(aab).css('left',left);
                left+=775;				
            });
            $("#go_left").click(previous_project);
            $("#go_right").click(next_project);
			
            $('div.project_info, div.pi_background').fadeTo(5, 0);
            $('div.project_info, div.pi_background').show();			
			
            $('div.project_info').hover(
                function() {
                    $(this).stop(true,true).fadeTo('normal', 1);
                    $(this).siblings('div.pi_background').stop(true,true).fadeTo('normal', .93);
                },
                function() {
                    $(this).fadeTo('normal', 0);
                    $(this).siblings('div.pi_background').fadeTo('normal', 0);
                }
                );
			
            $("#has_loaded").attr("rel","yes");		

			Cufon.replace('h1');
			Cufon.replace('h2');
			Cufon.replace('h3');
			Cufon.replace('.servicehead');
			 
            $(".gallery a[rel^='prettyPhoto']").prettyPhoto({
                theme:'dark_rounded'
            });
        });
				
    }
}

$(document).ready(function() {
    port_init();

//init();
});
