var chatDialog = null;
var chatDialogVisible = false;

function getQuerystring(key, default_)
{
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

function init() {

  if (document.getElementById('ofrRightCol')) {
    var dest = document.getElementById('ofrRightCol'); 
    if (document.getElementById('ctl03_divPaymentLink')) dest.appendChild(document.getElementById('ctl03_divPaymentLink'));  
    if (document.getElementById('offer_chatLink')) dest.appendChild(document.getElementById('offer_chatLink'));     
    if (document.getElementById('ctl04_printCont')) dest.appendChild(document.getElementById('ctl04_printCont'));
  }
  
  var frmFilter = document.getElementById('frmFilter');


  if (document.getElementById('midTypes1')) {
     var a = frmFilter.types.options;
     document.getElementById('midTypes1').value = a[1].value;
     document.getElementById('midTypes2').value = a[2].value;
     document.getElementById('midTypes3').value = a[3].value;
  }  
  
  var img = document.getElementById('offerImage');
  if (img) { 
    if (getQuerystring('offer') == '3803144') {
      img.parentNode.innerHTML = '<a href="http://alera-server.com/ITC/#/BG/home" target="_blank">'+img.parentNode.innerHTML+'</a>';
      return;
    }
    
    if (img.width == 480) {
      img.parentNode.innerHTML = '<a href="'+img.src+'" target="_blank">'+img.parentNode.innerHTML+'</a>';
    }
  }
  
  if (document.getElementById('marqueeHolder') &&   
      document.getElementById('galleryContent') &&
      document.getElementById('marqueeHolder').offsetWidth < 
      document.getElementById('galleryContent').firstChild.offsetWidth * galleryElements
      ) {
        
      document.getElementById('galleryContent').style.width = document.getElementById('galleryContent').firstChild.offsetWidth * galleryElements + galleryElements * 10;    
      
      if (document.getElementById('aGalBtnLeft')) {
        var a = document.getElementById('aGalBtnLeft');
        a.onmousedown = startScrollLeft;
        a.onmouseout = stopScroll;
        a.onmouseup = stopScroll;   
      }
      if (document.getElementById('aGalBtnRight')) {
      
        var a = document.getElementById('aGalBtnRight');
        a.onmousedown = startScrollRight;
        a.onmouseout = stopScroll;    
        a.onmouseup = stopScroll;   
      }
  }
  
  	var handleOk = function() {
		this.hide();
		chatDialogVisible = false;
	};


  if (document.getElementById('chatDialog')) {
      chatDialog = new YAHOO.widget.SimpleDialog("chatDialog", 
		     { width: "200px",
		       close: false,
		       context:["chatLinkPanel","tl","bl"],
		       visible: false,
		       iframe: true,
		       constraintoviewport: true,
		       buttons: [ { text:"OK", handler:handleOk, isDefault:true }]
		     } );
      chatDialog.render();
      document.getElementById('chatDialog').style.display = '';
  }
  initHomeCombos();
 }


function initHomeCombos() {
    if (document.getElementById('cmbLocalities1') != null) {
        var cmb = null;
        cmb = new DynamicCombo("./alncs/f15/asp/combo.asp?id=1166&lang=" + lang, document.getElementById('cmbLocalities1'));
        cmb.createNoSelectedItem = true;
        cmb.noSelectedName = '-- ' + area + ' --';
        cmb.fill();
    }
    
    if (document.getElementById('cmbLocalities2') != null) {
        var cmb = null;
        cmb = new DynamicCombo("./alncs/f15/asp/combo.asp?id=1167&lang=" + lang, document.getElementById('cmbLocalities2'));
        cmb.createNoSelectedItem = true;
        cmb.noSelectedName = '-- ' + area + ' --';
        cmb.fill();
    }
    
    if (document.getElementById('cmbLocalities3') != null) {
        var cmb = null;
        cmb = new DynamicCombo("./alncs/f15/asp/combo.asp?id=1176&lang=" + lang, document.getElementById('cmbLocalities3'));
        cmb.createNoSelectedItem = true;
        cmb.noSelectedName = '-- ' + area + ' --';
        cmb.fill();
    }
}
	
  function showHideChatDialog(senderLink) {
    if (!chatDialog) return;
    chatDialog.cfg.setProperty("context",[senderLink.parentNode,"tl","bl"]); 
    if (chatDialogVisible) {chatDialog.hide();chatDialogVisible=false;} else {chatDialog.show();chatDialogVisible=true;};
    
  }
    
  
/*var GoogleTalkStatusChecker = new function() {
  this.url = 'http://www.google.com/talk/service/badge/Show?tk=z01q6amlqpmk578holertau9nsnurrv6gt7n93534ja1o155voumaujbc4ddbk66vvupjh8t0e39nvj7oj1fnfu53jlq929s4jnutdjgiiimqe3t1ni41v1cabf8vgsufm1a54nhnbck0b9dq954e3o545pde2qbru5snjg7n6coqjqmqv8nuhqu8he0tp3pck0';
  
  this.success=function(o){setGTStatus(o.statusCode);},
  this.failure=function(o){setGTStatus(false);},
	
  this.check=function() {
  alert(this.url);
  	   YAHOO.util.Connect.asyncRequest('GET', this.url, this);
  }

};

function setGTStatus(online) {
  alert(online);
}

function refreshGoogleTalkStatus() {
  GoogleTalkStatusChecker.check();
}
*/

function scrollRight() {
  var C = document.getElementById('galleryContent');
  
  var m = C.style.marginLeft;
  m = m.replace('px', '') * 1; 
  if (m >= 0) m = 0;
  C.style.marginLeft = (m + 5).toString() + 'px';
}
function scrollLeft() {
  var H = document.getElementById('marqueeHolder');
  var C = document.getElementById('galleryContent');
  
  var m = C.style.marginLeft;
  m = m.replace('px', '') * 1; 
  
  if (m <= H.offsetWidth - C.offsetWidth) m = H.offsetWidth - C.offsetWidth;
  
  C.style.marginLeft = (m - 5).toString() + 'px';
}

var intGal;

function startScrollRight() {
  intGal = window.setInterval(scrollLeft, 5);
}
function startScrollLeft() {
  intGal = window.setInterval(scrollRight, 5);
}
function stopScroll() {
  window.clearInterval(intGal);
}


window.onload=function() {init();}

