//<script type="text/javascript">
// Delay Plugin for jQuery
// - http://www.evanbot.com
// - © 2008 Evan Byrne

jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});

	return this;
};

function rand ( n )
{
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}
function centerItem(theitem){
var screenh=$(window).height();
var itemh = $('#'+theitem).height();
var newheight = (screenh - itemh)/2;
if(newheight >0){
  $('#'+theitem).css('margin-top',newheight+'px');

}


}

function realignBubbles(){
  //this is all wrong
   $('.portfolio_links li').each(function(){
var orig = $(this).attr('rel');
array = orig.split('||');
var wrappos = $('#wrapper').offset();
var origt = array[0];
var left = array[1];
origt = parseInt(origt.substring(0,(origt.length-2)));
origt = origt+wrappos.top;
left = parseInt(left.substring(0,(left.length-2)));
left = left+wrappos.left;
$(this).css('left',left+'px');
$(this).css('top',origt+'px');
   });

}
////

window.panel =initial_page;
window.curport = 'none';
window.viewport = 'none';

  $(document).ready(function(){
    notices();
    $('.temp').remove();
   $('.portfolio_links a').html('');
  loadRemainingPages();

    centerItem('wrapper');
    realignBubbles();

    //set li title attribs to rel - avoiding w3c errors :-)

    //move pagination to bottom
var pagetop = $('#bottom').offset().top-66;
var pageleft = $('#mainmenu').offset().left+116;
$('#pagination').css('left',pageleft).css('top',pagetop)
    //bind pagination
     $("#pagination a").click(function(){
       if( $('#animating').val() == 1)return false;
       var subject = $(this).attr('id');
       changePage(subject,1);
     return false;
     }).bind("mouseenter",function(){
       var subject = $(this).attr('id');
var subject = subject.substring(4);
var myclass = $(this).attr('class');
if(myclass != 'current'){
 $(this).children().attr('src',base_url+'images/pagination/'+subject+'o.png');
}

    }).bind("mouseleave",function(){
       var subject = $(this).attr('id');
var subject = subject.substring(4);
//alert('images/menu/'+subject+'_mo.png');
var myclass = $(this).attr('class');
//alert(myclass);
if(myclass != 'current'){
   $(this).children().attr('src',base_url+'images/pagination/'+subject+'.png');
}

    });
    //bind bubbles
     $(".portfolio_links li").bind("mouseenter",function(){
       if($.browser.msie){
 $(this).children().show();
       }else{
 $(this).children().fadeIn();
       }

    }).bind("mouseleave",function(){
             if($.browser.msie){
 $(this).children().hide();
       }else{
 $(this).children().fadeOut();
       }
    }).bind("click",function(){
             if( $('#animating').val() == 1)return false;
      var link = $(this).attr('rel');
      moveBubble($(this).attr('id'))

    });
    // bind main menu
     $("#mainmenu a").click(function(){
              if( $('#animating').val() == 1)return false;
var prev = window.panel;
var subject = $(this).attr('href');
subject = subject.split('/');
subject =  subject[subject.length-1];
 subject = subject.substring(0,(subject.length));

    if(window.panel != subject){
      if(window.viewport != 'none'){
      closePortfolioDetails();
      $(this).delay(1100,function(){
          panelChange(prev,subject);
       });
      }else{
      panelChange(prev,subject);
      }

      }
     return false;
     }).bind("mouseenter",function(){
       var subject = $(this).attr('href');
subject = subject.split('/');
subject =  subject[subject.length-1];
 subject = subject.substring(0,(subject.length));
       $(this).children().attr('src',base_url+'images/menu/'+subject+'_mo.png');
    }).bind("mouseleave",function(){
       var subject = $(this).attr('href');
subject = subject.split('/');
subject =  subject[subject.length-1];
 subject = subject.substring(0,(subject.length));
//alert('images/menu/'+subject+'_mo.png');
       $(this).children().attr('src',base_url+'images/menu/'+subject+'.png');
    });     });

  function panelChange(prev,subject){
     $('#animating').val('1');
         changePage('page1',0);
        port_out(window.curport);
     $('#headimage, #'+prev).fadeOut(function(){
     $('#headimage').attr('src',base_url+'images/'+subject+'.png').fadeIn();
     $('#'+subject).fadeIn(function(){
            $('#animating').val('0');
          });

    });

        if(subject=='portfolio'){
                setTimeout("port_in('portfolio1')",500);

     }


           window.panel=subject;
           updateTitle();
  }
$(document).ready(function(){

    });

 function port_out(ulid){
   if($("#newbubble").length > 0 ){
     closePortfolioDetails();
   }
   window.curport = 'none';
   var speed = 800+rand(600);

   $('#'+ulid+' li').each(function(){
$(this).children().show();
$(this).css('background-image','url(none)');
var top=$(this).css('top');
var left=$(this).css('left');
var array= $(this).children().attr('longdesc');
var array=array.split('||');
var width= array[0];
var height = array[1];


var newtop = parseInt(top.substring(0,(top.length-2)))+parseInt((height.substring(0,(height.length-2))/2));

var newleft = parseInt(left.substring(0,(left.length-2)))+parseInt((width.substring(0,(width.length-2))/2));
 if($.browser.msie && jQuery.browser.version.substr(0,3) < 7){
$(this).children().css('height',0).css('width',0);
$(this).css('top',newtop+'px').css('left',newleft+'px');
$(this).hide();
 }else{
$(this).children().animate({
      "height": 0, "width": 0
    }, { queue: false, duration: speed },function(){});

$(this).animate({
      "top":newtop+'px' , "left": newleft+'px'}, { queue: false, duration: speed }).css('height',0).css('width',0);
      }
   });   }
 function port_in(ulid){

   if(window.curport == ulid){
     return false;
   }
   $('#animating').val('1');
   if(window.curport !='none'){
     var delay=1000;
     port_out(window.curport);

   }else{
     var delay=0;
   }
   if(($.browser.msie && jQuery.browser.version.substr(0,3) < 7) ){

    var delay=0;
      }
 $(this).delay(delay,function(){
   $('#'+ulid+' li').each(function(){
  var speed = 800+rand(600);

var bg=$(this).children().attr('src');
var height=$(this).children().css('height');
var width=$(this).children().css('width');
var array= $(this).children().attr('longdesc');
var array=array.split('||');
var width= array[0];
var height = array[1];
var top=$(this).css('top');
var left=$(this).css('left');
$(this).children().css('height',0);
$(this).children().css('width',0);
var newtop = top.substring(0,(top.length-2))-(height.substring(0,(height.length-2))/2);
var newleft = left.substring(0,(left.length-2))-(width.substring(0,(width.length-2))/2);
 if(($.browser.msie && jQuery.browser.version.substr(0,3) < 7)){
$(this).show();
$(this).css("top",newtop+'px').css("left",newleft+'px');
$(this).children().css("height", height).css("width", width);
 $(this).css('background-image','url('+bg.substring(0,(bg.length-7))+'.png)');
$(this).children().hide();

$(this).css('height',height);
$(this).css('width',width);
   $('#animating').val('0');

  }else{

$(this).animate({
      "top":newtop+'px' , "left": newleft+'px'
    }, speed );
$(this).children().animate({
      "height": height, "width": width
    }, speed,function(){
 $(this).parent().css('background-image','url('+bg.substring(0,(bg.length-7))+'.png)');
 if($.browser.msie){
$(this).hide();
 }else{
$(this).fadeOut();
 }

$(this).parent().css('height',height);
$(this).parent().css('width',width);
 $('#animating').val('0');
    });

  }

});


window.curport=ulid;
updateTitle();
//hide all other items completly
$('.portfolio_links').each(function(){

if($(this).attr('ID') != ulid){


$(this).children('li').each(function(){
$(this).css("background-image","none").css('height','0px').css('width','0px');
});

}

});

});

 }
function moveBubble(bubble){
     $('#animating').val('1');
  //set viewport to block other links
  window.viewport = bubble;
//step1: create bubble from li element,
//step2: fadecurrent bubbles out. hide current bubble
//step3: move copied bubble to the right place.

//get bubble link details.
var top = $('#'+bubble).offset().top;
var left = $('#'+bubble).offset().left;
var height = $('#'+bubble).css('height');
var width = $('#'+bubble).css('width');
var rel = $('#'+bubble).attr('rel');
var src = $('#'+bubble).children().attr('src');
src = src.substring(0,(src.length-7));
src+='.png';
//step1
//alert('new bubble top: '+top+' left: '+left+' height: '+height+' width: '+width+' src: '+src);
var bubblecss = 'style="position:absolute; z-index:101; top: '+top+'px; left: '+left+'px; height: '+height+'; width: '+width+';"';
var newbubble = '<img id="newbubble" '+bubblecss+' src="'+src+'" longdesc="'+height+'||'+width+'||'+top+'||'+left+'||'+bubble+'"/>';
$('body').append(newbubble);
//step2
$('#'+bubble).hide();
$('#'+curport).fadeOut();
//step3
var newtop = $('#mainmenu').offset().top;
var newleft = $('#mainmenu').offset().left;
$('#newbubble').animate({
      "height":'65px' , "width": '65px',  "left": newleft+125+'px',  "top": newtop-17+'px'
    }, 'slow', function(){
      createPortfolioDetails(rel);
    })
updateTitle();
}
function moveBubbleOut(){

  //reset viewport
   window.viewport = 'none';
//get old bubble positions
var array = $('#newbubble').attr('longdesc');
array = array.split('||');
height=array[0];
width=array[1];
mtop=array[2];
left=array[3];
origbubble=array[4];
$('#newbubble').animate({
      "height":height , "width": width,  "left": left+'px',  "top": mtop+'px'
    }, 'slow',function(){
    $('#newbubble').remove();
    $('#'+origbubble).show();
     $('#animating').val('0');
    }).fadeOut();

    $('#'+curport).fadeIn('slow');
    updateTitle();
}

function createPortfolioDetails(rel){
var rel= rel.split('||');
var subject = rel[2];
var newtop = $('#mainmenu').offset().top;
var newleft = $('#mainmenu').offset().left+143;
$('#'+window.panel).fadeOut('slow');
var style=" style='top:"+newtop+"px; left:"+newleft+"px; display:none;' ";
var view="<div id='portDetails' "+style+"></div>";
$('body').append(view);
var pwidth = '639px';
var pheight = '461px';
$('#portDetails').animate({
      "height":pheight , "width": pwidth
    }, 'slow', function(){
      portBackLink();
      portfolioDetails(subject);
      $('#animating').val('0');
    });

}
function portBackLink(){
var newtop = $('#mainmenu').offset().top-13;
var newleft = $('#mainmenu').offset().left+704;
var style=" style='top:"+newtop+"px; left:"+newleft+"px; position:absolute; z-index: 101' ";
var view="<a id='portBackLink' href='#' "+style+"><img src='"+base_url+"images/layout/projectreturn.png'></a>";
$('body').append(view);
$('#portBackLink').bind("mouseenter",function(){
       $(this).children().attr('src',base_url+'images/layout/projectreturn_mo.png');
    }).bind("mouseleave",function(){
       $(this).children().attr('src',base_url+'images/layout/projectreturn.png');
    }).bind("click",function(){
       if( $('#animating').val() == 1)return false; 
      closePortfolioDetails();
      return false;
    });
}
function portfolioDetails(subject){
var newtop = $('#mainmenu').offset().top+16;
var newleft = $('#mainmenu').offset().left+163;
$.post(base_url+"pages/portfolio/projects/"+subject+".php", { action: "getproject", project: subject },
  function(data){
$('#portfolioDetails').html(data).css('left',newleft+'px').css('top',newtop+'px').fadeIn();
setTimeout ( "$('.prettyPhoto').prettyPhoto({theme:'light_rounded'})", 20 );

  });



}
function removePortfolioDetails(){

$('#portfolioDetails').fadeOut().css('left','-1000').css('top','-1000').html('');

}
function closePortfolioDetails(){
   $('#animating').val('1');
removePortfolioDetails();
$('#portBackLink').fadeOut('slow');
$('#'+window.panel).fadeIn('slow');
$('#portDetails').animate({
      "height":1 , "width": 1
    }, 1000, function(){
      $('#portDetails, #portBackLink').remove();
     moveBubbleOut();
    });

updateTitle();
}
function changePage(page,port){
  var mynumber = page.substring(4);
var cursrc = $('#pagination .current img').attr('src');
cursrc = cursrc.substring(0,(cursrc.length-5));
cursrc += '.png';
$('#pagination .current img').attr('src',cursrc);
  $('#pagination .current').attr('class','');
  $('#'+page).attr('class','current');
var mysrc = base_url+'images/pagination/';
mysrc += mynumber;
mysrc += 'a.png';
$('#'+page+' img').attr('src',mysrc);
$(".portfoliopage").hide();
$("#portfoliopage"+mynumber).show();
if(port ==1){
port_in("portfolio"+mynumber);
}
updateTitle();
}


function loadRemainingPages(){
//load by ajax all of the other pages, so that we have nice seo, and a fast page too.
//page1



$('.portfoliopage').each(function(){
      var page = $(this).attr('id');
       $.post(base_url+"ajax.php",  { action: "page", page:page},
          function(data){
          $('#'+page).html(data);
        });
});
if(initial_page != 'portfolio'){
//the bubbles have to be there from the begining so we can manipulate them :(
//instead, we'll load the data for the pages individually

}else{
  var portfoliopage = initial_subpage.replace('page','')

port_in('portfolio'+portfoliopage);
changePage('page'+portfoliopage,0);
if(initial_project!= 'na'){
  //if its a specific project they came to, display that project
  //once all the animation is FINISHED
 if($.browser.msie){
 delay = 10;
 }else{
   delay = 1900;
 }
 $(this).delay(delay,function(){
moveBubble(initial_project);
   });
}


}
if(initial_page != 'services'){
        $.post(base_url+"pages/services.php",  { action: "ajax"},
          function(data){
      $('#services').html(data);
        });
}
if(initial_page != 'contact'){
        $.post(base_url+"pages/contact.php",  { action: "ajax"},
          function(data){
      $('#contact').html(data);
         setTimeout('unobfuscate()',20);
        });
}
if(initial_page != 'about'){
        $.post(base_url+"pages/about.php",  { action: "ajax"},
          function(data){
      $('#about').html(data);
        });
}
       setTimeout('unobfuscate()',20);

}
function sendForm(){
var name = $('#name').val();
var email = $('#email').val();
var message = $('#message').val();
  $.post(base_url+"ajax.php",  { action: "sendmail", name: name, email: email, message:message},
          function(data){
if(data == 'done'){
$.popup.show('Thank you for your interest', 'Your message has been sent.');
}else if(data == 'name'){
$.popup.show('Thank you for your interest', 'Unfortunately your message was not sent <br /> as the name you gave is too short.');
}else if(data == 'msg'){
$.popup.show('Thank you for your interest', 'Unfortunately your message was not sent <br /> as the message you left is too short.');
}else if(data == 'email'){
$.popup.show('Thank you for your interest', 'Unfortunately your message was not sent <br /> as the email you gave is invalid.');  
}



        });
}
 function unobfuscate(){
   $('.obfMe').each(function(){
   var text= $(this).html();
   text = text.replace('||','@');
   $(this).replaceWith("<a href='mailto:" + text + "?subject=Comments from Contact Page' title='email me'>" + text + "</a");


   });
 }
function notices(){
 if($.browser.msie || $.browser.safari){

       if($.browser.msie){
      browser='Internet Explorer';
      problem = 'not all animations are supported by ';
    }
    if($.browser.safari){
      browser='Safari';
      problem = 'not all animations work with ';
    }

  //  message = 'Hi, Thanks for stopping by. Unfortunately '+problem+browser+'... for best results, please use FireFox.';
   // $('#notices').show().css('width',($(window).width()-22)+'px').html(message);


  }
}

//new function to change title of page depending on content

function updateTitle(page){
db = 'na';
if(window.panel == 'portfolio'){
if(window.viewport != 'none'){
  href = $('#'+window.viewport+' a').attr('href');
  href = href.split('/');
  href=href[(href.length-1)];

  href=href.split('_');

  href = href.splice(1,href.length);
  href = href.join('_');
page = 'portfolio/projects/'+href;
}else if(window.curport != 'none'){
db = 'page';
page = window.curport.replace('portfolio','');
}else{
page = window.panel;
}
}else{
page = window.panel;
}

          $.post(base_url+"ajax.php",  { action: "title", tag: "title", page: page, db: db},
          function(data){
        document.title = data;
        });

}

//</script>
