﻿$(document).ready(function() {
  
   $('#btnSearchBoxSearch').click(function() {
   window.location = '/property/searchresult.aspx?t=' + $('#st').val() + '&gpt=' + $('#LeftNavi1_QuickSearch1_cboproptype').val() + 
                     '&pt=&ds=' + $('#LeftNavi1_QuickSearch1_cboDistrict').val() + '&mp=' + URLEncode($('#LeftNavi1_QuickSearch1_txtMinPrice').val()) + 
                     '&xp=' + URLEncode($('#LeftNavi1_QuickSearch1_txtMaxPrice').val()) + '&k=' + URLEncode($('#LeftNavi1_QuickSearch1_txtKeyword').val()) +
                     '&mbr=' + URLEncode($('#LeftNavi1_QuickSearch1_txtMinBed').val()) + '&xbr=' + URLEncode($('#LeftNavi1_QuickSearch1_txtMaxBed').val()) + 
                     '&mbt=' + URLEncode($('#LeftNavi1_QuickSearch1_txtMinBath').val()) + '&xbt=' + URLEncode($('#LeftNavi1_QuickSearch1_txtMaxBath').val()) + 
                     '&mbu=' + URLEncode($('#LeftNavi1_QuickSearch1_txtMinSize1').val()) + '&xbu=' + URLEncode($('#LeftNavi1_QuickSearch1_txtMaxSize').val()) + 
                     '&sby=';
  });
  
  
  $('#submitSaveList').click(function() {
   callAjaxGrid('UP', $('#pid').val());
});

//  var carousellist=$('#mycarousel li').remove();
  $('#mycarousel').jcarousel({
  visible: 5,
   scroll: 5
//  wrap: 'circular',
//  initCallback: function (carousel){
//  $('#submitSaveList').bind('click', function() {carousel.scroll(1, 0);});
//  $("#leftMenu_mySavedList").bind('click', function() {carousel.scroll(1, 0);});
//  },
//  itemVisibleInCallback: {onBeforeAnimation: function (carousel, item, i, state , evt){
//  var idx = carousel.index(i, carousellist.length);
//  carousel.add(i,$(carousellist[idx-1]).html());}},
//  itemVisibleOutCallback: {onAfterAnimation: function (carousel, item, i , state, evt) {carousel.remove(i);}}
  });

//  $('#mycarousel').jcarousel({visible:5});
  $("#Enquiry > ul").tabs();
  $("#EnquiryBtm > ul").tabs();

  ComputeLoan();
});


function callAjaxGrid(st, pid) {
  $('#result').html('<div class="loader" title="Loading....."></div>');
  $.ajax({
    url: '/ajax_server/property/svr_updatesavelist.aspx',
    data:
        'st=' + st + '&pid=' + pid ,
    type: 'GET',
    datatype: 'application/xml',
    timeout: 100000,
    cache: false,
    error: function(err) {
      $('#result').empty();
      //alert('There is something wrong with the server, or your session is expired, please relogin or try again...','Error');
    },
    success: function(xml) {
      //    alert($(xml).text().length);
      $('#result').empty();
      var redirect = $(xml).find('redirect').text();
      var message = $(xml).find('message').text();
      var total = $(xml).find('total').text();
      if(redirect.length > 0){
        //window.location = redirect + '?redirect=ls&' + window.location.search;
        window.location = redirect + '?redirect=' + $('#loginRedirect').val();
      }        
      else     
      {
       $('#mySavedList_dialog').dialog("open");
        if(message == 'success'){
          $('#totalShortList').html('(' + total + ')');
          callAjaxGridSaveList('u');
         } 
        else
        {
          callAjaxGridSaveList('a');
         }
      }
    }
  });
}
  
  function openPrintWin(contentURL) {
    newWindow = window.open(contentURL, null, 'height=400,width=600,status=no,toolbar=yes,menubar=no,location=no,resizable=yes,scrollbars=yes');
    window.newWindow.focus();
  }
  
  function isNumberKey(evt)
  {
     var charCode = (evt.which) ? evt.which : event.keyCode
     if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

     return true;
  } 
  
      
//  function ClickThumb(pf, desc) {
//    var img1 = new Image()
//    img1.src = pf
//    var iwidth = img1.width
//    var iheight = img1.height
//    var z
//                
//    if(iwidth > iheight){
//      if(iwidth > 540){
//        z = iwidth/540;
//          iwidth = 540;
//          iheight = iheight/z;
//        }
//    }else{
//       if(iheight > 540){
//          z = iheight/540;
//          iheight = 540;
//          iwidth = iwidth/z;
//       }
//    }
//    document.getElementById('GoTo').href = '/openMainPhoto.aspx?sPic=' + pf;
//    document.getElementById('_ctl0_imgHouse').src = pf;
//    document.getElementById('_ctl0_imgHouse').width = iwidth;
//    document.getElementById('_ctl0_imgHouse').height = iheight;
//    document.getElementById('picDesc').innerHTML = desc;
  //  }


  function ClickThumb(pf, desc) {
      var img1 = new Image()
      img1.src = pf
      var iwidth = img1.width
      var iheight = img1.height
      
      var ratio = 400 / 540;
      if (iheight / iwidth > ratio) {
          // height is the problem
          if (iheight > 400) {
              iwidth = Math.round(iwidth * (400 / iheight));
              iheight = 400;
          }
      } else {
          // width is the problem
      if (iwidth > 540) {
          iheight = Math.round(iheight * (540 / iwidth));
          iwidth = 540;
          }
      }
      document.getElementById('GoTo').href = '/openMainPhoto.aspx?sPic=' + pf;
      document.getElementById('_ctl0_imgHouse').src = pf;
      document.getElementById('_ctl0_imgHouse').width = iwidth;
      document.getElementById('_ctl0_imgHouse').height = iheight;
      document.getElementById('picDesc').innerHTML = desc;
  }

  function setImageSize(myImage, obj, size) {
    var img1 = new Image()
    var val = myImage
    img1.src = val
    var iwidth = img1.width
    var iheight = img1.height
    var z

    if (iwidth > iheight) {
      if (iwidth > size) {
        z = iwidth / size;
        iwidth = size;
        iheight = iheight / z;
      }
    } else {
      if (iheight > size) {
        z = iheight / size;
        iheight = size;
        iwidth = iwidth / z;
      }
    }
    obj.height = iheight;
    obj.width = iwidth;
  }


  function picResize(myImage, obj, maxh, maxw) {
      var img1 = new Image()
      var val = myImage
      img1.src = val
      var iwidth = img1.width
      var iheight = img1.height
        
      var ratio = maxh / maxw;
      if (iheight / iwidth > ratio) {
          // height is the problem
          if (iheight > maxh) {
              iwidth = Math.round(iwidth * (maxh / iheight));
              iheight = maxh;
          }
      } else {
          // width is the problem
      if (iwidth > maxw) {
          iheight = Math.round(iheight * (maxw / iwidth));
          iwidth = maxw;
          }
      }

      obj.height = iheight;
      obj.width = iwidth;
  }