var currentDiv='contactDiv';
var scrollEnabled=true;
var SCROLL_HEIGHT=0;

function initPage(){

$("Table_01").makePositioned();

SCROLL_HEIGHT=$("Table_01").getHeight();

// 4 safari to work, we need to make all content containers positioned first
$('mainDivCon').makePositioned();
$('portDivCon').makePositioned();
$('servicesDivCon').makePositioned();
$('clientsDivCon').makePositioned();
$('teamDivCon').makePositioned();
$('contactDivCon').makePositioned();
// and then set the top attribute

$('mainDivCon').style.top=SCROLL_HEIGHT+"px";
$('portDivCon').style.top=SCROLL_HEIGHT+"px";
$('servicesDivCon').style.top=SCROLL_HEIGHT+"px";
$('clientsDivCon').style.top=SCROLL_HEIGHT+"px";
$('teamDivCon').style.top=SCROLL_HEIGHT+"px";
$('contactDivCon').style.top=SCROLL_HEIGHT+"px";


//$('mainDiv').makePositioned();
$('portDiv').makePositioned();
$('servicesDiv').makePositioned();
$('clientsDiv').makePositioned();
$('teamDiv').makePositioned();
$('contactDiv').makePositioned();

//$('mainDiv').style.height=SCROLL_HEIGHT+"px";
$('portDiv').style.height=SCROLL_HEIGHT+"px";
$('servicesDiv').style.height=SCROLL_HEIGHT+"px";
$('clientsDiv').style.height=SCROLL_HEIGHT+"px";
$('teamDiv').style.height=SCROLL_HEIGHT+"px";
$('contactDiv').style.height=SCROLL_HEIGHT+"px";

loadPage('content.php?id=main','mainDivContent');
loadPage('content.php?id=contact',currentDiv+'Content');
new Effect.Move($('mainDivCon'), { y: -SCROLL_HEIGHT });
new Effect.Move($(currentDiv+'Con'), { y: -SCROLL_HEIGHT });
}

function changeSlide(targetDiv,targetPage){
if(currentDiv==targetDiv){
return;
}
$(currentDiv+'Td').addClassName('slideCursor');
$(targetDiv+'Td').removeClassName('slideCursor');
if(scrollEnabled==true){
if(targetPage){
loadPage(targetPage,targetDiv+'Content');
}
scrollEnabled=false;
new Effect.Parallel([
new Effect.Scale($(currentDiv), 24,{scaleY : false,scaleContent:false,scaleFrom:100,scaleMode: { originalWidth: 300 }}),
new Effect.Scale($(targetDiv), 100,{scaleY : false,scaleContent:false,scaleFrom:24,scaleMode: { originalWidth: 300 }})
],{afterFinish:enableScroll} /*, { 
  duration: 0.8,
  delay: 0.5}*/);
  $(currentDiv+'Con').style.top=SCROLL_HEIGHT+"px";
  new Effect.Move($(targetDiv+'Con'), { y: -SCROLL_HEIGHT });
currentDiv=targetDiv;
}
}

function mouseScroll(event){
var wheelData = event.detail ? event.detail * -40 : event.wheelDelta;
var element=Event.element;
$(currentDiv+'Content').makePositioned();
var top=parseFloat($(currentDiv+'Content').getStyle('top'));
var absTop=Math.abs(top);
var height=Math.abs($(currentDiv+'Content').getHeight());
//alert(height+" : "+top);
//if(!height){
//height=0;
//}
$('info').update(height+" : "+top+" : "+wheelData+" : "+(height-absTop));
if(wheelData>0){
if(top>=0){
return;
}
}else{
if(height-absTop<500 ){
return;
}
}

new Effect.Move($(currentDiv+'Content'), { y: wheelData , queue: { position: 'end', scope: 'scrollScope',limit: 1 }});

}

function loadPage(pageId,targetId){

new Ajax.Request(pageId, {
  method: 'get',
  onSuccess: function(req) {
  //alert(req.responseText);
  var jsonObj = req.responseText;
  

 
 $(targetId).update(jsonObj);
 //loadColumns(jsonObj.contents,"left");
 
 
 
    
  }, onComplete: function(req) {
  //$('loadStatus').hide();
  }
});
}
function enableScroll(){
scrollEnabled=true;
//alert('complete');
}

function showDialog(evt,pImg,nImg){

var previewElem=new Element('div',{'id':'previewBar','align':'right','class':'previewBar'});
var previewCloseBar=new Element('span',{'id':'previewCloseBar','class':'previewCloseBar','title':'Close'}).update('&nbsp;');
previewElem.appendChild(previewCloseBar);
var previewImg=new Element('img',{'id':'preview','style':'background-image: url("images/icon-loading.gif");background-repeat: no-repeat;background-position: center center;'});
var previewSlide=new Element('div',{'id':'previewSlide','align':'center','class':'previewBar'});
if(pImg!='null'){
var previewPrev=new Element('span',{'id':'s'+pImg,'style':'background-image: url("images/prev.jpg");','class':'previewArrow','title':'Previous' }).update('&nbsp;');
previewSlide.appendChild(previewPrev);
Event.observe(previewPrev,'click',preNav);
}

if(nImg!='null'){
var previewNext=new Element('span',{'id':'s'+nImg,'style':'background-image: url("images/next.jpg");','class':'previewArrow','title':'Next' }).update('&nbsp;');
previewSlide.appendChild(previewNext);
Event.observe(previewNext,'click',preNav);
}

previewImg.src=evt;
/*if(!previewImg.complete){
//$('loadStatus').show();
Event.observe(previewImg, 'load', imgLoaded);
Event.observe(previewImg, 'abort', imgLoaded);
Event.observe(previewImg, 'error', imgLoaded);
}

Event.observe(previewImg, 'mousedown', cancelEvent);
Event.observe(previewImg, 'mousemove', cancelEvent);*/
previewCloseBar.onclick=hideDialog;

$('previewCell').appendChild(previewElem);
$('previewCell').appendChild(previewImg);
$('previewCell').appendChild(previewSlide);

var docWidth=document.viewport.getWidth();
var docHeight=document.viewport.getHeight();

$('dialog').setStyle({width:docWidth+'px',height:docHeight+'px'});
$('dialog').setOpacity(0.8);
$('dialog').show();
$('previewDiv').show();
var docWCenter=Math.abs(($('dialog').getWidth)-(previewElem.getWidth()));
var docHCenter=($('dialog').getHeight()/2)-(previewElem.getHeight()/2);
docHCenter=Math.floor(docHCenter);
}
function preNav(event){
var element = Event.element(event);
var eid=element.id.substring(1);
hideDialog();
$(eid).onclick.call();
}
function hideDialog(){
$('previewDiv').hide();$('dialog').hide();$('previewSlide').remove();$('previewBar').remove();$('preview').remove();
}

