// global variables
var s1,s2,s3,s4,s5,s6;
var cur=0,next,running=false, imgSrc=new Array(), gMap = false;
var flist,timeOut, direction, loading=true, timeOutAutoPlay, DHotel;
var artImgList = [], selIndex = 0, overLap, insideConHeight = howMuch = 0;
/*_-----------------------------------------_*/
function observing (){
    //initialize();
    var getdivs = $A($$('div.containers')).compact();    
    var tabs    = $A($('tablist').getElementsByTagName('a')).compact();                
    getdivs.each(function(el){$(el).hide();});
    $(getdivs[window.location.href.include('#')?2:0]).show();
    $(tabs[window.location.href.include('#')?2:0]).className = 'selected';
    //$('galMid').hide();
    tabs.each(function(e){
                     Event.observe(e,'click', function() {
                          getdivs.each(function(el){$(el).hide();});
                          $(getdivs[tabs.indexOf(this)]).show();
                          tabs.each(function(ell){$(ell).className = '';});
                          $(this).className = 'selected';
                          if ( tabs.indexOf(this) == 2 ) {createAllSliders();}
                          //if ( tabs.indexOf(this) == 3 ) {if (!gMap){initialize(); gMap = true;}} // google map initilizer
                          if($('photoVideo'))$('photoVideo').hide();
                          if ( tabs.indexOf(this) == 0 ) { 
                            if($('photoVideo')){$('photoVideo').show(); }
                          }
                         }
                     );
              }); // end tabs.each
    
    if ( window.location.href.include('#') ) { if($('photoVideo'))$('photoVideo').hide(); createAllSliders();}
    if ($('insideCont'))
     artImgList = $A($('insideCont').childElements()).compact();    
    if ( artImgList.size() > 0 ) { 
       $(artImgList[selIndex]).setStyle({border:'2px solid #FF0000',width:'46px',height:'46px'});
       overLapL = document.createElement('div');
       $('galViewers').appendChild(overLapL);
       overLapL.id = 'showPreImages'
       overLapL.onmouseout = function(event){$('showPreImages').hide();}
       overLapL.onclick = movePrev;
       
       $('showPreImages').hide();
       //return;
       overLapR = document.createElement('div');
      $('galViewers').appendChild(overLapR);
       overLapR.id = 'showNxtImages'
       overLapR.onmouseout = function(event){$('showNxtImages').hide();}
       overLapR.onclick = moveNext;
        $('showNxtImages').hide();
        Event.observe('layer','mousemove', function(event) {
                                      if ( artImgList.size() <= 1 ) return false;
                                      elm = Event.element(event);
                                      pos = $(elm).cumulativeOffset();
                                      cPos = Event.pointerX(event) - pos[0];
                                      if ( (cPos > 0) && (cPos < 150) ) {
                                          $('showPreImages').setStyle({left:'0px',top:'0px'}).show();
                                          $('showNxtImages').hide();
                                      }
                                      else if ( (cPos > 350) && (cPos < 500) ) {
                                          $('showNxtImages').setStyle({left:'350px',top:'0px'}).show();
                                          $('showPreImages').hide();
                                      }
                                      else {
                                          if ( $('showPreImages') ) {$('showPreImages').hide();}
                                          if ( $('showNxtImages') ) {$('showNxtImages').hide();}
                                      }
                                   });
      
   } // end if
   if ($('insideCont')) {
       insideConHeight  = parseInt(($('insideCont').offsetHeight - 360) / 60);
       howMuch = insideConHeight
       if ( insideConHeight > 0 ) {
          insideConHeight = parseInt(145/insideConHeight); 
       }
       var elms = $('insideCont').childElements();
       for ( ii=0; ii<elms.length; ii++){
            Event.observe(elms[ii],'mouseover', thumbIn);
            Event.observe(elms[ii],'mouseout', thumbOut);
            Event.observe(elms[ii],'click', showLarges);
        }
       
       
   }
  //alert(insideConHeight);
   Event.observe(document, 'keyup', function(event){
                                  if( (event.keyCode == 39) || (event.keyCode == 40) ) { moveNext(); }// next
                                  else if ( (event.keyCode == 37) || (event.keyCode == 38) ) { movePrev();  }
                                });
} // end function
/*_-----------------------------------------_*/
function moveNext() {
   selIndex = parseInt(selIndex)+1;
   FiIndex = (Math.abs(parseInt($('insideCont').getStyle('top')))/60)*3+17;
   if ( ( selIndex > FiIndex)  ) {moveTowards('n');}
   if (selIndex > artImgList.size()-1) { 
       selIndex = 0; 
        if ( $('insideCont').offsetHeight > 360) { 
           $('indicator').setStyle({width:'5px'});
           $('insideCont').setStyle({top:'0px'});
        }
   }
   //if ( (selIndex > 15) &&  ((selIndex - 15) % 3 == 0) ){ moveTowards('n'); }
   showLarge(artImgList[selIndex], selIndex);
}
function movePrev() {
    selIndex = parseInt(selIndex)-1;
    FiIndex = (Math.abs(parseInt($('insideCont').getStyle('top')))/60)*3;
    if ( ( selIndex < FiIndex) && (selIndex > 0)  ) {
      moveTowards('p');
    }
   if (selIndex < 0 ) { 
      selIndex = artImgList.size()-1; 
       if ( $('insideCont').offsetHeight > 360) { 
          $('insideCont').setStyle({top:(-1)*($('insideCont').offsetHeight-360)+'px'});
          $('indicator').setStyle({width:(howMuch*insideConHeight)+'px'});
       }
   }
    showLarge(artImgList[selIndex], selIndex);
}



function createAllSliders() {
   // drag drop the rating images
   // do not change the variable name 
   s1 = createSlider('staffPointSlider', 'staffPointCont', 'staffPointContBg', 'staffPoint');
   s2 = createSlider('roomQualitySlider', 'roomQualityCont', 'roomQualityBg', 'roomQuality');
   s3 = createSlider('bedComfortSlider', 'bedComfortCont', 'bedComfortBg', 'bedComfort');
   s4 = createSlider('CleanliNessSlider', 'CleanliNessCont', 'CleanliNessBg', 'CleanliNess');
   s5 = createSlider('ValueForMoneySlider', 'forMoneyCont', 'ValueForMoneyBg', 'ValueForMoney');
   s6 = createSlider('locationSlider', 'locationCont', 'locationBg', 'location');
   incSlider('staffPoint','staffPointContBg',s1, 'initial');    
   incSlider('roomQuality','roomQualityBg',s2, 'initial');    
   incSlider('bedComfort','bedComfortBg',s3, 'initial');    
   incSlider('CleanliNess','CleanliNessBg',s4, 'initial');    
   incSlider('ValueForMoney','ValueForMoneyBg',s5, 'initial');    
   incSlider('location','locationBg',s6, 'initial');    
}
/*_-----------------------------------------_*/
// tool tips for room type
function showRTF(This, val, type, event) {
   if ($('popUps'))$('popUps').remove();
   elm = document.createElement('div');
   document.body.appendChild(elm);
   $(elm).addClassName('popUp');
   $(elm).id = 'popUps';
  // Element.writeAttribute($(elm),{class:'popUp',id:'popUps'});
   // find position where to show i.e find the source element and then find source element position
   pos = $(This).cumulativeOffset();
   posX = Event.pointerX(event);
   posY = Event.pointerY(event);
   //alert(posX)
   $(elm).setStyle({left:posX+'px',top:posY+15+'px',zIndex:0,position:'absolute'});
   $(elm).innerHTML = '<div class="popUpBottom"><div class="popBG"><div class="topPart">Facilities Of This Room</div><div class="facPart">'+val+'</div><div class="topPart">Accomodation Capability</div><div>'+type+'</div></div></div>';
   $(elm).show(); 
}
function moveCur (event) {
   posX = Event.pointerX(event);
   posY = Event.pointerY(event);
   if ($('popUps')) { 
      left = ($('popUps').className == 'popUp1') ? posX - 500 : posX;
      $('popUps').setStyle({left:left+'px',top:posY+15+'px'});
   }
}
function showRating(This, event, val) {
   elm = document.createElement('div');
   document.body.appendChild(elm);
   $(elm).addClassName('popUp1');
   $(elm).id = 'popUps';
  // Element.writeAttribute($(elm),{class:'popUp',id:'popUps'});
   // find position where to show i.e find the source element and then find source element position
   pos = $(This).cumulativeOffset();
   posX = Event.pointerX(event);
   posY = Event.pointerY(event);
   //alert(posX);
   $(elm).setStyle({left:posX-500+'px',top:posY+15+'px',zIndex:0,position:'absolute',overflow:'hidden'});
   $(elm).innerHTML = '<div class="popUpBottom"><div class="popBG"><div class="topPart">Average Rating By User</div><div>'+val+'</div></div></div>';
   $(elm).show(); 
     
}
function rmvRTF() {
  if ($('popUps')) { $('popUps').remove(); }
}



document.observe('dom:loaded', observing);
/*_-----------------------------------------_*/

/*_-----------------------------------------_*/
function thumbIn() {
   $(this).setStyle({border:'2px solid #FF0000',width:'46px',height:'46px'});
}
function thumbOut() {
   $(this).setStyle({border:'0px solid #FF0000',width:'50px',height:'50px'});
   $(artImgList[selIndex]).setStyle({border:'2px solid #FF0000',width:'46px',height:'46px'});
}
function showLarges(event) {
  var elm = Event.element(event);
  var index = artImgList.indexOf(elm);
  showLarge(elm, index)
}
function showLarge(This, index) {
  //alert(index);
  var loadingImage = new Image();
  loadingImage.id = 'loading';
  loadingImage.src = './images/loading.gif';
  $('galViewers').appendChild(loadingImage);
  // get position of center
  allPos = $('largView').cumulativeOffset();
  $('loading').setStyle({top:'150px',left:'230px',position:'absolute',zIndex:1000});
  var oImage = new Image();
  var src =   artImgList[index].src.replace('_thm','_img');
  oImage.src = src;
  Event.observe($(oImage), 'load', function(event){ if($('loading')){$('loading').remove();}});
  $('largView').src =     src;
  //if($('loading')){$('loading').remove();}
  artImgList.each(function(elm){$(elm).setStyle({border:'0px solid #FF0000',width:'50px',height:'50px'})});
  $(This).setStyle({border:'2px solid #FF0000',width:'46px',height:'46px'});
  selIndex = index;
  $('nowShowing').innerHTML = parseInt(selIndex)+1;
  
}
var timeF;
function moveTowards(d) {
   obj = $('insideCont');
   oldTop = parseInt(obj.getStyle('top'));
   if ( d=='n' ) {
      newTop = oldTop - 60; if ( Math.abs(newTop) > ($('insideCont').offsetHeight - 360) ) {  stopScrolling();return false; }
      $('indicator').setStyle({width:$('indicator').offsetWidth+insideConHeight+'px'});
   }
   else {
      newTop = oldTop + 60; if ( newTop > 0 ) { stopScrolling();return false; }
      w = ($('indicator').offsetWidth-insideConHeight) < 5 ? 5 : $('indicator').offsetWidth-insideConHeight; 
      $('indicator').setStyle({width:w+'px'});
   }
   obj.setStyle({top:newTop+'px'});    
}
function thumbPre() {
  stopScrolling(); timeF = setInterval("moveTowards('p')",400);
}
function thumbNxt() {
  stopScrolling(); timeF = setInterval("moveTowards('n')",400);
}
function stopScrolling() {
  if (timeF) clearInterval(timeF);
}

/*_-----------------------------------------_*/
// sliders for review tabs
function createSlider(handle, track, contentBg, inputField) {
     return new Control.Slider($(handle), $(track), {
      range:$R(0,400),
      increment: 74,  
      values: [0,74,148,222,296,372],
      restricted:true,
      onSlide: function(value) {
         v = parseInt(value/74);
         $(inputField).value = v;
         val = (-1) * v * 15;
         $(contentBg).style.background = 'url(./images/slider-bg.gif) no-repeat 0 '+val+'px';
         setValues();
         
      },
      onChange: function(value) {
         v = parseInt(value/74);
         $(inputField).value = v;
         val = (-1) * v * 15;
         $(contentBg).style.background = 'url(./images/slider-bg.gif) no-repeat 0 '+val+'px';
         setValues();
      }
    });
}
/*_-----------------------------------------_*/
function incSlider(inputField, contentBg, instance, when) {
    next = parseInt($(inputField).value) + 1 ;      
    if (when == 'initial') next = next -1;
    if (next > 5) return false;
    $(inputField).value = next;
    eval(instance).setValue(next*74,0);
    val = (-1) * next * 15;
    $(contentBg).style.background = 'url(./images/slider-bg.gif) no-repeat 0 '+val+'px';
    setValues();
}
function decSlider(inputField, contentBg, instance) {
    next = parseInt($(inputField).value) - 1 ;      
    if (next < 0) return false;
    $(inputField).value = next;
    eval(instance).setValue(next*74,0);
    val = (-1) * next * 15;
    $(contentBg).style.background = 'url(./images/slider-bg.gif) no-repeat 0 '+val+'px';
    setValues();
}
function setValues() {
  tVal = [$('staffPoint'),$('roomQuality'),$('bedComfort'),$('CleanliNess'),$('ValueForMoney'),$('location')].compact();
  total = 0;
  for(i = 0; i < 6; i++ ) { 
     total += parseInt(tVal[i].value);
  }
  //alert(total);
  //avg = Math.round(total / 6);
  $('overAllRating').innerHTML  = total;
}


/*_-----------------------------------------_*/


function leaveComment(This, parentId, mId, articleId) {
  if ($('leaveComPop')) {$('leaveComPop').remove();}
  if (mId == 0 ) {
     window.location.href = './redirect.php?u='+encodeURIComponent(location.href);
     return false;
  }
  overLayWholeDocs();
  popUp  = document.createElement('div');
  document.body.appendChild(popUp);
  popUp.id = 'leaveComPop';
  pos = $(This).cumulativeOffset();
  popUp.setStyle({position:'absolute',top:pos[1]-240+'px',left:'250px',zIndex:1200});
  popUp.innerHTML = '<div class="box popup formArea pad"><span class="close"><a href="javascript:void(0);" onclick="javascript:closeOverLay();">Close (X)</a></span><strong class="hdrtxt space2">Leave your comment here</strong><input type="hidden" name="parentId" id="parentId" value="'+parentId+'" /><input type="hidden" name="articleId" id="articleId" value="'+articleId+'" /><input type="hidden" name="mId" id="mId" value="'+mId+'" /><textarea class="txtArea1 space" rows="7" cols="40" name="userComments" id="userComments"></textarea><input type="button" class="subbtn flspr" value="Submit" onclick="javascript:submitComments();" /><input type="button" class="subbtn" value="Cancel" onclick="javascript:closeOverLay();" /></div>';
  $('leaveComPop').scrollTo();
  
}
function submitComments() {
  var parms = 'parentId='+$('parentId').value+'&mId='+$('mId').value+'&hotelId='+$('articleId').value+'&reviewText='+encodeURIComponent($('userComments').value.stripTags());    
  new Ajax.Request('./postUserReviews.php',{method:'post', parameters: parms, onComplete: function(OReq){
                                            alert(OReq.responseText==1?'Your Reviews Has Been Submitted!!! \n However we will check it and publish it soon!!':OReq.responseText);
                                            closeOverLay();
                                                                            
                    }});
} // end function




/*_-----------------------------------------_*/
function closeOverLay() {
  if ($('overlay')) { 
     new Effect.Fade('overlay', { duration: 0.5}); 
      if ($('leaveComPop')) { $('leaveComPop').remove(); }
  }
}
function overLayWholeDocs() {
  var aPageSize = getPageSize();
  document.viewport.getScrollOffsets().top
  var showLoading = document.createElement('div');
  showLoading.id = 'overlay';
  document.body.appendChild(showLoading);
  $('overlay').setStyle({position: 'absolute',top:0,left:0,zIndex:90,width:aPageSize[0]+'px',height:aPageSize[1]+'px',backgroundColor:'#000',display:'none'});
  new Effect.Appear('overlay', { duration: 1.0, from: 0.0, to: 0.7 });
 }
/*_-----------------------------------------_*/
// report a hotel
function reportHotel(hotelId) {
   overLayWholeDocs();
   if ($('pWin')) { $('pWin').remove(); }
   elm = document.createElement('div');
   document.body.appendChild(elm);
   $(elm).addClassName('popUp');
   $(elm).id = 'pWin';
   pos = $('reportHotel').cumulativeOffset();
   $('reportHotel').scrollTo();
   $(elm).setStyle({left:pos[0]+'px',top:pos[1]+15+'px',zIndex:1000,position:'absolute'});
   $(elm).innerHTML = '<div class="popUpBottom"><div style="height:400px;" id="reCont"><div style="font-size:20px;">Write Your Report Below<br /><span style="font-size:12px;padding-left:20px;padding-top:10px;">Your say will be checked and necessary action will be taked asap</span></div><div>Your Name: <br /><br /><input type="text" /><br /><br />Your Valid Email: <br /><br /><input type="text" /><br /><br />Your Contact Number: <br /><br /><input type="text" /><br /><br />Your Say: <br /><br /><textarea style="height:120px;"></textarea><br /><br /><div><input class="button" type="button" value="Send Your Report &raquo;" onclick="javascript:sendReportNow();" /><span id="reportSending" style="display:none;padding-left:5px;float:left">Sending Report...Please Wait...</span></div></div></div></div>';
  $(elm).show(); 

    
}
function sendReportNow() {
    $('reportSending').setStyle({display:'inline'});
    setTimeout("closeOverLay()",2000); // should be replace with ajax call
}
/*_-----------------------------------------_*/

function sendToFriend(hotelId) {
   overLayWholeDocs();
   if ($('pWin')) { $('pWin').remove(); }
   elm = document.createElement('div');
   document.body.appendChild(elm);
   $(elm).addClassName('popUp');
   $(elm).id = 'pWin';
   pos = $('sendFriend').cumulativeOffset();
   $('sendFriend').scrollTo();
   $(elm).setStyle({left:pos[0]+'px',top:pos[1]+15+'px',zIndex:1000,position:'absolute'});
   $(elm).innerHTML = '<div class="popUpBottom"><div style="height:400px;" id="reCont"><div style="font-size:20px;">Send This Hotel to your Friend(s)</div><div>Your Name: <br /><br /><input type="text" /><br /><br />Your Valid Email: <br /><br /><input type="text" /><br /><br />Your Friends Email: [use comma(,) for multiple emails] <br /><br /><input type="text" /><br /><br />Your Say: <br /><br /><textarea style="height:120px;"></textarea><br /><br /><div><input class="button" type="button" value="Send &raquo;" onclick="javascript:sendReportNow();" /><span id="reportSending" style="display:none;padding-left:5px;float:left">Sending Emails...Please Wait...</span></div></div></div></div>';
  $(elm).show(); 
}
/*_-----------------------------------------_*/

