/**
 * 
 * @author Kris Cieslak
 * @version v1.0 (beta)
 * @license (Creative Commons) http://creativecommons.org/licenses/by-nc-sa/3.0/
 *
 * //digitalinsane.com
 */
var YD=YAHOO.util.Dom;
var YE=YAHOO.util.Event;
var YA=YAHOO.util.Anim;
var YC=YAHOO.util.Connect;
var $ = function (el) { return YD.get(el);}
var img=new Image;
var query='';
// -=-=-=-=-=-=-=-=-=-=-=-=-=-==--=-=-=-=-=-==--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//  HTML CONTROL
// -=-=-=-=-=-=-=-=-=-=-=-=-=-==--=-=-=-=-=-==--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
function btnSearch () { 
    if (!YD.hasClass('txtQuery','notext')) {   
      var query=$('txtQuery').value;
      getIMAGE.query=query.replace(/ /g,'+');
      getIMAGE.execute();
	}
                      }
function pSearch (e) {
	  if (YE.getCharCode(e)==13) btnSearch();
                      }
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==--=-=-==--==-=--==--=
//  A N I M A T I O N S
// -=-=-=-=-=-=-=-=-=-=-=-=-=--=--=-=-=-=--=-=---=--=-=-=--=-=-=-=-=-=-----=
var blob = function (el,w,h,s,z) {
    if ($(el).nodeName=='IMG') {
    var at1 = {
        width: { to: w+s, unit: 'px' },
        height: { to: h+s, unit: 'px' }    };
    var at2 = {
         width: { to: w, unit: 'px' },
         height: { to: h, unit: 'px' } };
    }        
    if ($(el).nodeName=='SPAN') {
    var at1 = {
        fontSize: { from: w, to: w+s, unit: '%' } }
    var at2 = {
        fontSize: { from: w+s, to: w, unit: '%' }}        
    }
    this.a=new YA(el,at1,0.25,YAHOO.util.Easing.eraseOut);
    this.a.onComplete.subscribe(function () {
               var b=new YA(el,at2,0.25,YAHOO.util.Easing.eraseOut);             
               b.animate(); 
               b.onComplete.subscribe(function() {
                   if (z) z();
               } ); }
                );
}
blob.prototype.start = function () { this.a.animate(); } 
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==--=-=-==--==-=--==--=
//  F L I C K R images
// -=-=-=-=-=-=-=-=-=-=-=-=-=--=--=-=-=-=--=-=---=--=-=-=--=-=-=-=-=-=-----=
var getIMAGE = function () {
    this.query;
    var data = function () {
        this.title;    
        this.Src;
        this.thbSrc;
    }
    var imgData=new Array;
    return {
        execute: function () {
                var callback = {
                    success: function (o) {
                             if (window.ActiveXObject) { 
                                  var doc=new ActiveXObject("Microsoft.XMLDOM");
                                  doc.async="false";
                                  doc.loadXML(o.responseText);  }
                             else { 
                                  var parser=new DOMParser();
                                  var doc=parser.parseFromString(o.responseText,"application/xml");  }
                             var x=doc.documentElement;
                             if (x.getAttribute('stat')=='ok') 
                                {
                                 var p=x.getElementsByTagName('photo');
                                 for (var i=0;i<p.length;i++) {
                                   imgData[i]=new data;
                                   imgData[i].title=p[i].getAttribute('title');
                                   imgData[i].thbSrc='http://farm'+p[i].getAttribute('farm')+'.static.flickr.com/'+p[i].getAttribute('server')+'/'+p[i].getAttribute('id')+'_'+p[i].getAttribute('secret')+'_s.jpg';
                                   imgData[i].Src='http://farm'+p[i].getAttribute('farm')+'.static.flickr.com/'+p[i].getAttribute('server')+'/'+p[i].getAttribute('id')+'_'+p[i].getAttribute('secret')+'.jpg';
                                 }
                                 getIMAGE.makeThumbs();
                                }
                    },
                    failure: function (o) {},
                    argument: { p1:"p1", p2:"p2" } 
                }
                var domain=document.location.href;
                var command = '?method=flickr.photos.search&text='+this.query+'&per_page=5&api_key=3a9676e36f78f0d815348637f7ea9174';
                 if (domain.indexOf('www.digitalinsane.com')!=-1)
                  url = 'http://www.digitalinsane.com/api/yahoo/flickrproxy.php?yws_path='+encodeURIComponent(command);
                  else 
                url = 'http://digitalinsane.com/api/yahoo/flickrproxy.php?yws_path='+encodeURIComponent(command);
                var request = YC.asyncRequest('GET', url,callback);
        },
        makeThumbs: function () {
               var preloader = function (im) {
                       YD.setStyle('start','display','none');
                       YD.setStyle('preloader','display','block');
                       var ivID=window.setInterval(function (){
                                             if (im.complete) { 
                                                 window.clearInterval(ivID);
                                                 YAHOO.puzzle.init(); 
                                                 
                                                 } },50);
               }
               $('img0').innerHTML=''; 
               $('img1').innerHTML='';
               $('img2').innerHTML='';
               $('img3').innerHTML='';
               $('img4').innerHTML='';        
               YD.setStyle(YD.getElementsByClassName('im','div','imgcont'),'background','none');
               for (var i=0;i<imgData.length;i++) {
                   var n=document.createElement('img');
                   n.id='i'+i;
                   n.src=imgData[i].thbSrc;
                   $('img'+i).appendChild(n);
               }
               YE.addListener('i0','click',function () { img=null; img=new Image; img.src=imgData[0].Src; preloader(img); });
               YE.addListener('i1','click',function () { img=null; img=new Image; img.src=imgData[1].Src; preloader(img); });
               YE.addListener('i2','click',function () { img=null; img=new Image; img.src=imgData[2].Src; preloader(img); });
               YE.addListener('i3','click',function () { img=null; img=new Image; img.src=imgData[3].Src; preloader(img); });
               YE.addListener('i4','click',function () { img=null; img=new Image; img.src=imgData[4].Src; preloader(img); });
        } 
    }
} ();
// -=-==--=-=-=-=-=-=--=--=-=-=-=-=-=-=-=-==-=--=-=-==--=-=-=-=-=-=-=-=-=-=-=
//   P U Z Z L E
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-
YAHOO.namespace('puzzle');
YAHOO.puzzle = function () {
       
   var rand=new Array;
   var box=new Array;

// Randomize array -==-=-=-=-==--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=   
   function FillRandom()   {
          for (i=0;i<15;i++) rand[i]=i;
        var i=rand.length;
        while ( --i ) {
             var j = Math.floor( Math.random() * ( i + 1 ) );
             var tempi = rand[i];
             var tempj = rand[j];
             rand[i] = tempj;
             rand[j] = tempi; } 

   }
// aBox object =--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=   
   var aBox = function () {
       this.id;
       this.nr;       
   }
   aBox.prototype.setSize = function () {
       this.width=Math.round(Board.width/4);
       this.height=Math.round(Board.height/4);
       YD.setStyle(this.id,'width',this.width+'px');
       YD.setStyle(this.id,'height',this.height+'px');       
   }
   aBox.prototype.setPos = function(nr) {
       var y=Math.floor(nr/4);
       var x=nr-(4*y);
       YD.setStyle(this.id,'top',(this.height*y)+'px');
       YD.setStyle(this.id,'left',(this.width*x)+'px'); 
       this.nr=nr;
       }
   aBox.prototype.setBG = function(nr) {
       var y=Math.floor(nr/4);
       var x=nr-(4*y);
       YD.setStyle(this.id,'background-position',(this.width*x*(-1))+'px'+' '+(this.height*y*(-1))+'px');
   }       
// Set container's width and height and prepare boxes -=-=-=-=-=-=-=-= 
   var Board = function (imgW,imgH) {
     Board.width=imgW;
     Board.height=imgH;
     YD.setStyle('cont','width',Board.width+'px');
     YD.setStyle('cont','height',Board.height+'px');
     YD.setStyle(YD.getElementsByClassName('box','div','cont'),'background','#000000 url('+img.src+') no-repeat');
     FillRandom();
     var n;     
     for (var i=0;i<16;i++) box[i]=null;
     for (var i=0;i<16;i++) {
        if (i<15) n=rand[i]; else n=i; 
        box[n]= new aBox();
        box[n].id='b'+n;
        box[n].setSize();
        box[n].setPos(i);
        box[n].nr=i;
        box[n].setBG(n);
     }
     YD.setStyle(box[15].id,'background','none');
   }
// Reset (ESC Key) -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
   function Restart() {
     FillRandom();
     var n;     
     for (var i=0;i<16;i++) {
        if (i<15) n=rand[i]; else n=i; 
        box[n].id='b'+n;
        box[n].setSize();
        box[n].setPos(i);
        box[n].nr=i;
        box[n].setBG(n);
     }
     YD.setStyle(box[15].id,'background','none');
   }    
// Check empty box --=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
   function toUp() {
          var y=Math.floor(box[15].nr/4);
       var x=box[15].nr-(4*y);
       if (y==3) return -1; else return (4*(y+1)+x);
   }
   function toDown() {
       var y=Math.floor(box[15].nr/4);
       var x=box[15].nr-(4*y);
       if (y==0) return -1; else  return (4*(y-1)+x);
   }
   function toLeft() {
       var y=Math.floor(box[15].nr/4);
       var x=box[15].nr-(4*y);
       if (x==3) return -1; else return ((4*y)+(x+1));       
   }
   function toRight() {
          var y=Math.floor(box[15].nr/4);
       var x=box[15].nr-(4*y);
       if (x==0) return -1; else  return ((4*y)+(x-1));
   }
// Find Box -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
   function findBox(dotnr) {
         for (var i=0; i<14; i++) {
          if (box[i].nr==dotnr) break;
      }
      return i;
   }
// Swap boxes -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==--=-=-=-=-=-=-=-=-=-=-=-=
   function swapBox(nr2) {
          if (nr2>=0) {
       var n=box[15].nr;    
       box[15].setPos(box[findBox(nr2)].nr);
       box[findBox(nr2)].setPos(n);
                  }
   }
// GameOver -=--=-=-==-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==--=-=-=-=-=
   function GameOver() {
       var n=true;
       for (var i=0;i<16;i++)
            if (box[i].id!=('b'+box[i].nr)) n=false;
       if (n) {
        YAHOO.puzzle.lockKey();
        YD.setStyle(YD.getElementsByClassName('n','h1','cont'),'display','none');        
        YD.setStyle(box[15].id,'background','#000000 url('+img.src+') no-repeat');
        box[15].setBG(15);
       } 
                
   }   
// Control -=-=-=-==-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=   
   function UpKey () {  swapBox(toUp());  }   
   function DownKey () {  swapBox(toDown());  }
   function LeftKey () {  swapBox(toLeft());  }
   function RightKey () {  swapBox(toRight());  }
    return {
        lockKey: function () {
            YE.removeListener(document,'keydown');
        },
        unLockKey: function (e) {
            switch (YE.getCharCode(e)) {
              case (40): DownKey(); break; 
              case (37): LeftKey(); break;
              case (39): RightKey(); break;
              case (38): UpKey(); break;
              case (27): Restart(); break;
            }
            GameOver();
        },
        init: function () {
            YD.setStyle(YD.getElementsByClassName('n','h1','cont'),'display','block');
            YD.setStyle('preloader','display','none');
            YAHOO.puzzle.lockKey();
            Board(408,408);
            YE.addListener(document,'keydown',YAHOO.puzzle.unLockKey);
    }
}} ();
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//  Main Init
// -==-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
function init() {
    YD.addClass('txtQuery','notext');	
    var y=new blob('logo',68,38,10);
    var f=new blob('flogo',98,26,10);
    var t3=new blob('t3',100,0,50);
    var t2=new blob('t2',100,0,50,function () { t3.start();});
    var t1=new blob('t1',100,0,50,function () { t2.start();});
    YE.addListener('btnSearch','click',btnSearch);
    YE.addListener('txtQuery','keydown',pSearch);
	YE.addListener('txtQuery','focus',function () {
	  if (YD.hasClass('txtQuery','notext')) { 
		YD.removeClass('txtQuery','notext');
		$('txtQuery').value=''; 
		}
	});
	YE.addListener('txtQuery','blur',function () {
	  if ($('txtQuery').value=='') {
		YD.addClass('txtQuery','notext');
		$('txtQuery').value='Search for photos';
	  }	 
	});
    YE.addListener('logo','mouseover',function () {y.start(); });
    YE.addListener('flogo','mouseover',function () {f.start(); });
    		  
		  
	
    YE.onAvailable('start',function () {t1.start(); });
	$('txtQuery').value='Search for photos';
    YAHOO.puzzle.init();
	
}
YE.addListener(window,'load',init);