
var debug=null;
  
includeJavaScript('/js/chalkControl/object.js');
includeJavaScript('/js/chalkControl/autoLoad.js');

includeStylesheet();


/**
 * Global Variable
 */
var currentObjName = 'home';
var currentGroup = null;
var currentImage = null;
var nextImage = null;
var ajaxBusy=0;
var delayremove=0;




/**
 * DOM ready actions
 */
document.observe("dom:loaded", resizeWindow );
Event.observe( window, 'load', prepareInitialView, false );
Event.observe( window, 'load', addPreloads, false );

/**
 * Prepare initial view
 */
function prepareInitialView()
{
  resizeWindow();
  var home = ('<div id="home" style="display:none">');
  home += ('  <div class="home1"><img src="/images/spacer.gif" alt="Liberate your space" id="small2" /></div>');
  home += ('  <div class="home2"><img src="/images/home/design_clarity.gif" alt="design clarity" /></div>');
  home += ('  <div class="home3"><img src="/images/spacer.gif" alt="Liberate your space" id="small1" /></div>');
  home += ('  <div class="home4"><h2>london studio</h2><p>7 Park Street<br>Borough Market<br>London SE1 9AB<br>United Kingdom</p><p>+44 (0)20 7403 6132</P><p><a href="http://www.designclarity.net/contact/" style="text-decoration:underline;">Contact us</a></p></div>');
  home += ('  <div class="home5"><img src="/images/spacer.gif" alt="Liberate your space" id="small3" /></div>');
  home += ('  <div class="home6"><a href="#about"/><img src="/images/home/about.gif" alt="About" id="aboutBtn" /></a></div>');
  home += ('  <div class="home7"><h2>sydney studio</h2><p>Studio 204<br>61 Marlborough St<br>Surry Hills NSW, 2010<br>Australia</p><p>+61 (0)2 9319 0933</p><p><a href="http://www.designclarity.net/contact/" style="text-decoration:underline;">Contact us</a></p></div>');
  
//  home += ('  <div class="home8"><img src="/images/spacer.gif" alt="Liberate your space" id="small4" /></div>');
  home += ('  <div class="home8">&nbsp;</div>');
  home += ('  <div class="home9">&nbsp;</div>');
  home += ('  <div class="home10"><img src="/images/home/projects.gif" alt="Projects" id="projectsBtn" /><a href="/blog-home" target="_blank" id="blogButton"></a></div>');
  home += ('  <div id="chalkdesign"><span class="right"></span><span class="left">&copy; Design Clarity 2012</span></div>');
  home += ('  <div id="bird"><img src="/images/home/bird.gif" alt="" /></div>');
  home += ('  </div>');
  $('container').innerHTML = home;
  swfReplacement();
 
  // Position the Home Box
  object['home'].left = -(getWindowWidth()-object['home'].width)/2;
  object['home'].setup();
  
  
  // Set the container Top Margin
  var containerTop = Math.ceil( (getWindowHeight()-$('container').offsetHeight)/3 )+30;
  $('container').setStyle( {marginTop: containerTop+'px'} );
  $('container').setStyle( {top: '0px'} );
  
  // Activate the about and home buttons
  prepareButtons();
  
  // Autoload pages?
  try
  {
    if( autoload ) { setTimeout( autoloader, 1000) };
  }
  catch(e)
  {
    var autoload=null;
  }
}


function swfReplacement()
{
  // Update the Flash Boxes
  if( sAdjust() )
  {
 swfobject.embedSWF("/flash/fader460x305.swf?xF=homeBigbox", "homeBigbox", "460", "305", "9.0.0");
    swfobject.embedSWF("/flash/small1.swf", "small1", "150", "150", "9.0.0");
    swfobject.embedSWF("/flash/small2.swf", "small2", "150", "150", "9.0.0");
    swfobject.embedSWF("/flash/small3.swf", "small3", "150", "150", "9.0.0");
//    swfobject.embedSWF("/flash/small4.swf", "small4", "150", "150", "9.0.0");
  }
  else
  {
    swfobject.embedSWF("/flash/fader460x305.swf?xF=homeBigbox", "homeBigbox", "460", "305", "9.0.0");
    swfobject.embedSWF("/flash/small1.swf", "small1", "150", "150", "9.0.0");
    swfobject.embedSWF("/flash/small2.swf", "small2", "150", "150", "9.0.0");
    swfobject.embedSWF("/flash/small3.swf", "small3", "150", "150", "9.0.0");
//    swfobject.embedSWF("/flash/small4.swf", "small4", "150", "150", "9.0.0");
  }
}


function addPreloads()
{
  object['projects'].preload();
  object['about'].preload();
  object['peopleAtClarity'].preload();
}



/**
 * Load object into container
 *
 * @param objName - String : Name of the object to load
 */
function loadObject( objName )
{
  var newObject = object[objName];
  if( !ajaxBusy )
  { 
    ajaxBusy=objName;
    var path = '/include/'+objName+'.php';
    if( object[newObject.parent].parent == 'projects' )
    {
      path = '/include/projectLayout.php?name='+objName+'&group='+newObject.parent;
    }
    else if( newObject.parent == 'peopleAtClarity' )
    {
      path = '/include/peopleLayout.php?name='+objName;
    }
        
    if( newObject.contents==null )
    {  
      new Ajax.Request(path,
      {
        method:'get',
        onComplete: function(transport){
          var response = transport.responseText || "no response text";
          
          newObject.contents = response;
          forceClosedObjects( newObject.level, 0 );
          newObject.addToPage();
          newObject.center();
        }
      });
    }
    else
    {
      forceClosedObjects( newObject.level, 0 );
      newObject.addToPage();
      newObject.center();
      setTimeout("ajaxBusy=0;", 1500);
    }
  }
}



 
function removeObjects( level, center ) 
{
  for ( var i in object )
  {
    if( object[i].level==level && object[i].open==1 )
    {
      object[i].close();
      if( center && object[i].parent )
      {
        object[object[i].parent].center();
      }
    }
  } 
}




function forceClosedObjects( level, center )
{
  for ( var i in object )
  {
    if( object[i].level==level && object[i].open==1 )
    {
      object[i].forceClose();
      if( center && object[i].parent )
      {
        object[object[i].parent].center();
      }
    }
  } 
}



function goHome( start )
{
  if( !ajaxBusy )
  {
    delayremove=1;
    object['home'].center()
    removeObjects(3, 0);
    removeObjects(2, 0);
    removeObjects(1, 0);
    setTimeout( function()
    {
      for ( var i in object )
      {
        if( object[i].open )
        {
          object[i].forceClose();
        }
      };
      delayremove=0;
    }, 1500 )
    setTimeout("$('projectsBtn').src=images['projects'];$('aboutBtn').src=images['about'];", 1000);
  }
}


/**
 * Update the main image displayed
 */
function updateImage( groupName, imageName )
{
  var currentGroup = groupName;
  var nextImage = imageName;
  var imagePath;

 
  if( sAdjust() )
  {
    imagePath ='/wp/wp-content/uploads/'+groupName+'-'+imageName+'-460x460.jpg';
  }
  else
  {
    imagePath ='/wp/wp-content/uploads/'+groupName+'-'+imageName+'-460x460.jpg';
  }
  //if this is the first pass then set all images to the faded position
  for(var x=1; x<10; x++)
  { 
    if( $('pLayout'+x+'Btn') ) { 
      if( x!=imageName )
      {
        $('pLayout'+x+'Btn').setStyle( {opacity:0.5} );
      }
      else
      {
        $('pLayout'+x+'Btn').setStyle( {opacity:1} );
      }
    };
  };
  
  $('pLayoutImg').setStyle( {opacity:0} );
  $('pLayoutImg').src='/images/spacer.gif';  
  
  var newImage = new Image;
  newImage.src=imagePath; 
  newImage.onload = function() {

	if (BrowserDetect.browser == 'Safari') 
	{
		$('pLayoutImg').setStyle({
			opacity: 0,
			height: newImage.height + 'px',
			width: newImage.width + 'px'
		});
	}
	
    $('pLayoutImg').fade({ duration:0.5, from:0.1, to:1 } );
    
	if( $('pLayoutImg'+nextImage+'Btn') )
    {
      $('pLayoutImg'+nextImage+'Btn').setStyle( {opacity:1} );
    }
	
    $('pLayoutImg').src = imagePath;
  } 
  currentImage = groupName+imageName;
}




function turnMenusOff( objName )
{  
  for ( var i in object )
  {
	if( object[i].parent==object[objName].parent && object[i].id!=objName && $((object[i].id+'Btn')) )
    {
	  $(object[i].id+'Btn').setStyle( {opacity:0.5} );
      if( sAdjustSize()=='1024' )
      {
       $(object[i].id+'Btn').src='/images/'+object[i].category+'/'+object[i].parent+'/'+object[i].id+'.gif';
      }
      else
      {
        $(object[i].id+'Btn').src='/images/'+object[i].category+'/'+object[i].parent+'/'+object[i].id+'.gif';
      }
    }
  } 

  if( sAdjustSize()=='1024' )
  {
    $(objName+'Btn').src='/images/'+object[objName].category+'/'+object[objName].parent+'/'+object[objName].id+'On.gif';
  }
  else
  {
    $(objName+'Btn').src='/images/'+object[objName].category+'/'+object[objName].parent+'/'+object[objName].id+'On.gif';
  }
  $(objName+'Btn').setStyle( {opacity:1} );
}




function resetMenus( parent )
{
  for ( var i in object )
  {
    if( object[i].parent==parent && $((object[i].id+'Btn')) )
    {
      $(object[i].id+'Btn').setStyle( {opacity:1} );
      
      if( parent=='projects' )
      {
        $(object[i].id+'Btn').src='/images/'+object[i].category+'/'+object[i].parent+'/'+object[i].id+'.gif';
      }
      else if( parent=='about' )
      {
        $(object[i].id+'Btn').src='/images/'+object[i].parent+'/'+object[i].id+'.gif';
      }
    }
  }
}


function sAdjust()
{
//	return '/1024';
  if( screen.width<=1 || screen.height<1)
  {
    return '/1024';
  }
  
  return '';
}


function sAdjustSize()
{
//	return 1024;
  if( screen.width<=1 || screen.height<1 )
  {
    return 1024;
  }
  
  return false;
}

function resizeWindow()
{
//	return false;
  window.moveTo(0,0);
  window.resizeTo(screen.width, screen.height );
}

function closeProjects()
{
  if( BrowserDetect.browser == 'Explorer' && BrowserDetect.version == 6 )
  {
    if( $('food-on-the-run') ) $('food-on-the-run').setStyle( {display:'none'} );
    if( $('environmental-branding') ) $('environmental-branding').setStyle( {display:'none'} );
    if( $('exhibitions-merchandising') ) $('exhibitions-merchandising').setStyle( {display:'none'} );
    if( $('project-management') ) $('project-management').setStyle( {display:'none'} );
    if( $('restaurants-cafes') ) $('restaurants-cafes').setStyle( {display:'none'} );
    if( $('retail-commercial') ) $('retail-commercial').setStyle( {display:'none'} );
  }
}


/**
 * Get the inner width of the window
 */
function getWindowWidth()
{
  var width;
  
  if(BrowserDetect.browser == 'Explorer')
  {
    width = document.documentElement.clientWidth;
  }
  else
  {
    width = window.innerWidth;
  }
  return width;
}
 

 
/**
 * Get the inner height of the window
 */
function getWindowHeight()
{
  var height;
  if(BrowserDetect.browser == 'Explorer')
  {
    height = document.documentElement.clientHeight;
  }
  else
  {
    height = window.innerHeight;
  }
  return height;
}


/**
 * initalise the home buttons
 */
function prepareButtons()
{
  if( $('projectsBtn') )
  {
    Event.observe( $('projectsBtn'), 'mouseover', function(){ if( !object['projects'].open ){ this.src=images['projectsOn']; this.style.cursor='pointer'; }}, false );
    Event.observe( $('projectsBtn'), 'mouseout',  function(){ if( !object['projects'].open ){ this.src=images['projects'];   }}, false );
    Event.observe( $('projectsBtn'), 'click',     function(){ if( !object['projects'].open ){ this.src=images['projectsOn']; loadObject('projects') }}, false );
  }

  if( $('aboutBtn') )
  {
    Event.observe( $('aboutBtn'), 'mouseover', function(){ if( !object['about'].open ){ this.src=images['aboutOn']; this.style.cursor='pointer'; }}, false );
    Event.observe( $('aboutBtn'), 'mouseout',  function(){ if( !object['about'].open ){ this.src=images['about'];   }}, false );
    Event.observe( $('aboutBtn'), 'click',     function(){ if( !object['about'].open ){ this.src=images['aboutOn']; loadObject('about') }}, false );
  }
}



/**
 * Call to include the Javascripts
 */
function includeJavaScript(jsFile)
{
  document.write('<script type="text/javascript" src="' + jsFile + '"></scr' + 'ipt>'); 
}

/**
 * Call to include the Stylesheets
 */
function includeStylesheet()
{
  document.write('<link rel="stylesheet" type="text/css" media="screen" href="/css/main.css" />');
  document.write('<!--[if IE 6]><link rel="stylesheet" type="text/css" media="screen" href="/css/ie6.css" /><![endif]-->');
  document.write('<!--[if IE 7]><link rel="stylesheet" type="text/css" media="screen" href="/css/ie7.css" /><![endif]-->');
}
 
