// ----------------------------------------------------------
//
//	PaintingDb.js - ver. 1.00
//
//  Created by Jussi Pyysalo on 2009-10-24.
//  Copyright 2009, 2010 Jussi Pyysalo. All rights reserved.
//
// ----------------------------------------------------------
//


function CreateCoolirisWall(artist) {

  // ---------------------------------------------------------------------------------------------------------
  //	This function creates the embedded Cooliris 3D wall using JavaScript to fit it perfectly on the screen
  // ---------------------------------------------------------------------------------------------------------

  var cooliris_feed   = "http://paintingdb.com/rss/en/high/" + artist + ".xml";
  var other_vars      = "&amp;showChrome=false&amp;showEmbed=false&amp;showSearch=false&amp;descriptionHeight=75";
  var ImgWidth = 0, ImgHeight = 0, WinWidth = 0, WinHeight = 0, NoOfRows = 0;

  if     (typeof( window.innerWidth ) == 'number' ) { WinWidth = window.innerWidth; WinHeight = window.innerHeight; }
  else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { WinWidth = document.documentElement.clientWidth; WinHeight = document.documentElement.clientHeight; }
  else if(document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { WinWidth = document.body.clientWidth; WinHeight = document.body.clientHeight; }

  ImgWidth  = WinWidth - 380;
  ImgHeight = WinHeight - 190;
  NoOfRows = Math.round(ImgHeight / 160);
  if(NoOfRows > 7) { NoOfRows = 7 }

  document.write("<object id=\"o\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\"" + ImgWidth + "\" height=\"" + ImgHeight + "\">");
  document.write("<param name=\"movie\" value=\"http://apps.cooliris.com/embed/cooliris.swf\">");
  document.write("<param name=\"allowFullScreen\" value=\"true\">");
  document.write("<param name=\"allowScriptAccess\" value=\"always\">");
  document.write("<param name='flashvars' value=\"feed=" + cooliris_feed + "?search=neon&amp;numRows=" + NoOfRows + "&amp;style=light" + other_vars + "\">");
  document.write("<embed type=\"application/x-shockwave-flash\" src=\"http://apps.cooliris.com/embed/cooliris.swf\" flashvars=\"feed=" + cooliris_feed + "?search=neon&amp;numRows=" + NoOfRows + "&style=light" + other_vars + "\" width=\"" + ImgWidth + "\" height=\"" + ImgHeight + "\" allowFullScreen=\"true\" allowScriptAccess=\"always\"></embed>");
  document.write("</object>");
  
}

function set_cookie ( name, value, expires_year, expires_month, expires_day, path, domain, secure ) {
  var cookie_string = name + "=" + escape ( value );
  if ( expires_year ) {
    var expires = new Date ( expires_year, expires_month, expires_day );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
	cookie_string += "; path=" + escape ( path );

  if ( domain )
	cookie_string += "; domain=" + escape ( domain );

  if ( secure )
	cookie_string += "; secure";

  document.cookie = cookie_string;

}

function delete_cookie ( cookie_name ) {
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function get_cookie ( cookie_name ) {
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function SetWindowSizeCookies() {
  var WinWidth = 0, WinHeight = 0;
  if     (typeof( window.innerWidth ) == 'number' ) { WinWidth = window.innerWidth; WinHeight = window.innerHeight; }
  else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { WinWidth = document.documentElement.clientWidth; WinHeight = document.documentElement.clientHeight; }
  else if(document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { WinWidth = document.body.clientWidth; WinHeight = document.body.clientHeight; }
  var current_date = new Date;
  var cookie_year  = current_date.getFullYear();
  var cookie_month = current_date.getMonth() + 1;
  if(cookie_month == 13) { cookie_year = cookie_year + 1; cookie_month = 1; }
  var cookie_day   = current_date.getDate();
  set_cookie ( "WindowWidth", WinWidth,  cookie_year, cookie_month, cookie_day);
  set_cookie ("WindowHeight", WinHeight, cookie_year, cookie_month, cookie_day);
}

function GetWindowSize() {
  var myWidth = 0, myHeight = 0;
  if(typeof( window.innerWidth ) == 'number' ) { myWidth = window.innerWidth; myHeight = window.innerHeight; }
  else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { myWidth = document.documentElement.clientWidth; myHeight = document.documentElement.clientHeight; }
  else if(document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { myWidth = document.body.clientWidth; myHeight = document.body.clientHeight; }
  document.naviform.window_width.value=myWidth;
  document.naviform.window_height.value=myHeight;
}

function ToggleTbody(id,action) { 
  if(document.getElementById) {
    var tbod = document.getElementById(id);
    if(tbod && typeof tbod.className == 'string') { tbod.className = action }
  }
}

function toggle_visibility(id) {
  var e = document.getElementById(id);
  if(e.style.display == 'none')
    e.style.display = 'block';
  else
    e.style.display = 'none';
}

function ChangeImage(name,image) {
  document[name].src = image;
}

function ChangeBgImage(name,image) { 
  document.getElementById(name).style.background = "url(" + image + ")";
}

var info_toggle = 0;
function ToggleInfo() {
  if(info_toggle == 0) { ToggleTbody('Measurement','on');  info_toggle = 1; document.getElementById('measurement_info').innerHTML = '(<a href=\"#\" class=\"bluelink\" onClick=\"ToggleInfo();\">less</a>)'; }
  else                 { ToggleTbody('Measurement','off'); info_toggle = 0; document.getElementById('measurement_info').innerHTML = '(<a href=\"#\" class=\"bluelink\" onClick=\"ToggleInfo();\">more</a>)'; }
}





var TimeToFade = 500.0;

function fade(eid)
{
  var element = document.getElementById(eid);
  if(element == null)
    return;
   
  if(element.FadeState == null)
  {
    if(element.style.opacity == null
        || element.style.opacity == ''
        || element.style.opacity == '1')
    {
      element.FadeState = 2;
    }
    else
    {
      element.FadeState = -2;
    }
  }
   
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
  }  
}


function animateFade(lastTick, eid)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
 
  var element = document.getElementById(eid);
 
  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    element.style.filter = 'alpha(opacity = ' + (element.FadeState == 1 ? '100' : '0') + ')';
    element.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + (element.FadeState == 1 ? '100' : '0') +')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
    return;
  }
 
  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  element.style.opacity = newOpVal;
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
  element.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + (newOpVal*100) + ')';
  setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}

