//////////////////////////////////////////////
//                               //
//    Copyright 2009, James Clarke     //
//    - Adnet Communications           //
//                               //
//////////////////////////////////////////////


window.addEvent('domready', function(){

//vars and utils

   var   pageAddresses = {"home":"http://www.stikineenergy.com/s/Home.asp",
             "corporate":"http://www.stikineenergy.com/s/Management.asp",
             "management":"http://www.stikineenergy.com/s/Management.asp",
             "corporateDirectory":"http://www.stikineenergy.com/s/CorporateDirectory.asp",
             "financialStatements":"http://www.stikineenergy.com/s/FinancialStatements.asp",
             "shareStructure":"http://www.stikineenergy.com/s/ShareStructure.asp",
             "projects":"http://www.stikineenergy.com/s/FracSand_SilicaProject.asp",
             "NEBC":"http://www.stikineenergy.com/s/FracSand_SilicaProject.asp",
             "huskie":"http://www.stikineenergy.com/s/HuskieUranium.asp",
             "maps":"http://www.stikineenergy.com/s/FracSand_SilicaProject.asp?ReportID=400453",
             "photos":"http://www.stikineenergy.com/s/FracSand_SilicaProject.asp?ReportID=400455",
	     	 "photogallery":"http://www.stikineenergy.com/s/PhotoGallery.asp",
			 "video":"http://www.stikineenergy.com/s/Videos.asp",
             "techreports":"http://www.stikineenergy.com/s/FracSand_SilicaProject.asp?ReportID=400456",
             "investors":"http://www.stikineenergy.com/s/StockInfo.asp",
             "stockInfo":"http://www.stikineenergy.com/s/StockInfo.asp",
             "articles":"http://www.stikineenergy.com/s/Articles.asp",
             "researchReports":"http://www.stikineenergy.com/s/ResearchReports.asp",
             "articlesAndComments":"http://www.stikineenergy.com/s/ArticlesAndComments.asp",
             "relatedSites":"http://www.stikineenergy.com/s/RelatedSites.asp",
             "presentations":"http://www.stikineenergy.com/s/Presentations.asp",
             "upcomingEvents":"http://www.stikineenergy.com/s/UpcomingEvents.asp",
             "news":"http://www.stikineenergy.com/s/NewsReleases.asp",
			 "news2011":"http://www.stikineenergy.com/s/NewsReleases.asp?DateRange=2011/01/01...2011/12/31",
             "news2010":"http://www.stikineenergy.com/s/NewsReleases.asp?DateRange=2010/01/01...2010/12/31",
             "news2009":"http://www.stikineenergy.com/s/NewsReleases.asp?DateRange=2009/01/01...2009/12/31",
             "news2008":"http://www.stikineenergy.com/s/NewsReleases.asp?DateRange=2008/01/01...2008/12/31",
             "news2007":"http://www.stikineenergy.com/s/NewsReleases.asp?DateRange=2007/01/01...2007/12/31",
             "newsArchive":"http://www.stikineenergy.com/s/NewsReleases.asp?DateRange=2003/01/01...2006/12/31",
             "qwikreport":"http://www.stikineenergy.com/s/QwikReport.asp",
             "contact":"http://www.stikineenergy.com/s/ContactUs.asp",
             "requestInfo":"http://www.stikineenergy.com/s/InformationRequest.asp",
             "sendMsg":"http://www.stikineenergy.com/s/SendMessage.asp"};


   function checkAnchor(){ //Returns hash value
         var hash = window.location.hash;
         if(hash.length > 1){

            var out = hash.substring(1);
            if(out.indexOf('||') > 0){
               out = out.substring( 0,out.indexOf('||') );
            // alert(out);
            }
            return out;
         }else{
            return false;
         }
      }

   function getUrlVars(name){ // returns URL variable value
         name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
         var regexS = "[\\?&]"+name+"=([^&#]*)";
         var regex = new RegExp( regexS );
         var results = regex.exec( window.location.href );
         if( results == null ){
            return "";
         }else{
            return results[1];
         }
      }



//INIT
   var siteMove;
   var lastMenu;
   var curAnchor;
   var animCol = new Object(); // collection of animation classes, referenced by name;

   function init(){

   // Build pages
      var nav = $('home').getElement('div[class=nav]');
      var pagesForNav = $("site").getElements('div[class=page]').each(function(item,i){
            if(i>=1){
               nav.clone().injectInside($(item).getElement('div[class=header]'));
            }
         });
      $('site').getElements('li').each(function(item){
         item.addEvent('mouseenter',function(e){
            item.addClass('sfhover');
         });
         item.addEvent('mouseleave',function(e){
            item.removeClass('sfhover');
         });
      });
      var menus = $('site').getElements('div[class=header]').each(function(item){
            item.getElements('ul').each(function(myUL){

                     myUL.addEvent('mouseenter',function(e){
                     // e = new Event(e).stop();
                     // e = e.target || e.srcElement;
                        myUL.addClass('over');
                        //myUL.setStyle('background','#FFFFFF');
                     });

                     myUL.addEvent('mouseleave',function(e){
                     // e = new Event(e).stop();
                     // e = e.target || e.srcElement;
                        //myUL.setStyle('background','none');
                        myUL.removeClass('over');
                     });
            });
         });

   // set up scrolling
   // SmoothScroll.implement({getAnchor:function(){return JC_Target;},setAnchor:function(pass){JC_Target=pass;}});
   // siteMove = new SmoothScroll({duration: 2000, transition: Fx.Transitions.Quad.easeInOut});

      siteMove = new Fx.Scroll(window,{duration: 2000, transition: Fx.Transitions.Quad.easeInOut});

      var links = $('site').getElements('a').each(function(item){
            item.addEvent('click',function(e){
               var ev = new Event(e).stop();
                  //ev = ev.target || ev.srcElement;
					ev = ev.srcElement ? ev.srcElement : ev.target;
                  ev = ev.href;
                  ev = ev.substring(ev.indexOf('#')+1);
               // alert(ev);

                  // animCol[curAnchor].start(1,0);
   //off for testing $(curAnchor).getElement('div[class=content]').setStyle('overflow','hidden');

                  curAnchor = ev;

               //    animCol[ev].start(0,1);

                  siteMove.toElement($(ev));
                  loadHTML(ev);
                  window.hashChange = function(e){e = new Event(e).stop() };
                  window.location.hash = ev;


            });
         });
         var anim = $('site').getElements('div[class=page]').each(function(item){
               item.getElement('div[class=content]').setStyle('overflow','hidden');
           });
         //var anim = $('site').getElements('div[class=page]').each(function(item){
         //          var name = item.id;
         //       // alert(name);
         //          animCol[name] = new Fx.Style(item,'opacity',{duration:1000}).set(0);
         //       });




      }

      function loadInit(){
         var initialAnchor = checkAnchor();

         if(initialAnchor){
         // alert(initialAnchor);
            curAnchor = initialAnchor;
            //siteMove.setAnchor(initialAnchor);
            loadHTML(initialAnchor);
            siteMove.toElement($(initialAnchor));
         }else{
            curAnchor = 'home';
            //siteMove.setAnchor('home');
            loadHTML('home');
            siteMove.toElement($('home'));
         }

         pageResize();

         window.addEvent('resize', function(){

         //    alert('resize called');

               //var item = $(siteMove.getAnchor()).getElement('div[class=content]');
               var item = $(curAnchor).getElement('div[class=content]');


               var h = window.getHeight();
               var w = window.getWidth();

               item.setStyle('width', w - 120);
               item.setStyle('height', h - 264);
         });
      }

//LOAD

//Find address and start AJAX
      function loadHTML(pass){
      // alert(pass);
         var inner = $(pass).getElement('div[class=content]');
         
         var x =  new Ajax(pageAddresses[pass],{
            method: 'get',
            update: inner,
            onComplete: AJAXSUCCESS()}).request();

            x.addEvent('onFailure',function(){
               AJAXFAIL();
            });
      }
      //start new
      function loadAddressInElement(address,targetElement){
         targetElement = $(targetElement).getElement('div[class=content]');
      // var currentHash = window.location.hash;
      //    currentHash += "||"+address;
      //    window.location.hash = currentHash;

         var p =  new Ajax(address,{
            method: 'get',
            update: targetElement,
            onComplete: AJAXSUCCESS()}).request();

            p.addEvent('onFailure',function(){
               AJAXFAIL();
            });
      }
      //end new
      function AJAXSUCCESS(){

           var item = $(curAnchor).getElement('div[class=content]');
      //    animCol[curAnchor].start(0,1);
            item.setStyle('overflow','scroll');

         var h = window.getHeight();
         var w = window.getWidth();

            item.setStyle('width', w - 120);
            item.setStyle('height', h - 264);

      //start new
            function setLinkLogic(){
                  var links = $(curAnchor).getElement('div[class=content]').getElements('a').each(function(item){

                              item.addEvent('click', function(e){
                                 var ev = new Event(e);//.stop();
                                    //ev = ev.target || ev.srcElement;
								 var evt = ev.srcElement ? ev.srcElement : ev.target;
                                 var out = evt.href;
                                    out = out.substring(out.indexOf('.com')+4 );
                                    out = out.substring(out.indexOf('.') );
                                    out.toLowerCase();
                                    if(out.indexOf('?') > 0){
                                       out = out.split('?');
                                       out = out[0];
                                    }
                                  // alert(out);
                                    function isHTML(){
                                       var chkExt = out;
                                       if( (chkExt == ".asp")||(chkExt == ".htm")||(chkExt == ".html") ){
                                          if(evt.target == "_blank"){
                                             return false;
                                          }else{
                                             return true;
                                          }
                                       }else{
                                          return false;
                                       }
                                    }
                                    if(isHTML()){

                                       loadAddressInElement(evt.href,curAnchor);
                                    
                                    }else{
                                       window.open(evt.href,"","menubar=1");
                                    }
									
									if(out == ".jpg"){
										if(document.all)ev.stop();	
									}else{
										ev.stop();	
									}
                              });

                        });
                    }
					
					setLinkLogic.delay(500);
         //end new
      }

      function AJAXFAIL(){
         alert('loadhtml Failed. \n\r Page may not exist, please retry.');
      }
      
 function AJAXFAIL2(){
         alert('loadAddressInElement Failed. \n\r Page may not exist, please retry.');
      }

//RESIZE
   //Scale pages
      function pageResize(){

         var H = window.getHeight()-20;
         var W = window.getWidth()-20;

         // replace getAnchor with 'current target' variable.
         //var item = $(siteMove.getAnchor() || 'home' ).getElement('div[class=content]');
         var item = $(curAnchor || 'home' ).getElement('div[class=content]');

         var h = window.getHeight();
         var w = window.getWidth();

         item.setStyle('width', w - 120);
         item.setStyle('height', h - 280);

      }

      var lastHash=null;
      function checkHash(){
         var curHash = window.location.hash;

         if((curHash != lastHash) && (lastHash!=null) ){
            //var anchor = siteMove.getAnchor();
            var anchor = curAnchor;
               loadHTML(anchor);

         }else{

         }
         lastHash = curHash;
      }
   // setInterval(checkHash, 30);


      init();
      loadInit();


});
