var is_ie = ((navigator.userAgent.toLowerCase().indexOf("msie") != -1) && (navigator.userAgent.toLowerCase().indexOf("opera") == -1));
var is_ie6 = (is_ie && (navigator.userAgent.indexOf('IE 6') != -1));
var is_op = (navigator.userAgent.toLowerCase().indexOf("opera") != -1);
var __tmp = document.location.pathname.split('/')[1];
var _lang = (__tmp.length == 3) ? __tmp : 'rus';

function addOnLoadEvent() {

	var root = window.addEventListener || window.attachEvent ? window : document.addEventListener ? document : null;
	if (root) {	
		for (var i = 0; i < arguments.length; i++) {
			if ('function'  == typeof(arguments[i]))
				addHandler(root,'load',arguments[i]);
		}
	}
}

function addHandler(object, event, handler) {
  if (typeof (object.addEventListener) != 'undefined')
    object.addEventListener(event, handler, false);
  else if (typeof (object.attachEvent) != 'undefined')
    object.attachEvent('on' + event, handler);
  else {
    var handlersProp = '_handlerStack_' + event;
    var eventProp = 'on' + event;
    if (typeof (object[handlersProp]) == 'undefined') {
      object[handlersProp] = [];
      if (typeof (object[eventProp]) != 'undefined')
        object[handlersProp].push(object[eventProp]);
      object[eventProp] = function(e) {
        var ret = true;
        for (var i = 0; ret != false && i < object[handlersProp].length; i++)
          ret = object[handlersProp][i](e);
        return ret;
      }
    }
    object[handlersProp].push(handler);
  }
}

function initTB_ShowPic() {
    var imgs = document.getElementById('TB_ajaxContent').getElementsByTagName('img');
    if (imgs.length) {
            for (var i = 0; i < imgs.length; i++) {
                    var obj = imgs[i];
                    if (obj.className.indexOf('_sp') != -1) {
                            obj.style.cursor = 'pointer';
                            ShowPics.push(obj);
                            obj.onclick = function() { TB_show('',this.src.replace(/tn_/i,'')); return false;}
                    }
            }
            createShowPic();
    }    
}

var ShowPics = new Array;
/* ShowPic */
function initShowPic() {
        if (document.getElementById('content') && 'function' == typeof(ShowPic)) {
                var imgs = document.getElementById('content').getElementsByTagName('*');
                if (imgs.length) {
                        for (var i = 0; i < imgs.length; i++) {
                                var obj = imgs[i];
                                if (obj.className.indexOf('_sp') != -1) {
                                        obj.style.cursor = 'pointer';
                                        ShowPics.push(obj);
                                        obj.onclick = function() { ShowPic(this); return false;}
                                }
                        }
                        createShowPic();
                }
        }
}
addOnLoadEvent(initShowPic);
var ClickToClose = "Закрыть";
var NextHTML =  '&raquo;';//"<img src='/pict/sp/right16.gif' align='absmiddle'>";
var PrevHTML = '&laquo;';// "<img src='/pict/sp/left16.gif' align='absmiddle'>";


/* For GuestBook */

function setSmile(obj) {
  var ta = document.getElementById('gbbody');
  ta.focus();
  ta.value += obj.getAttribute('alt');
}
/*
function OpenPrint(path)
{
  var addwinw = 710;
  var addwinh = 600;
  var temp = path;
  var xpos = (screen.width-addwinw)/2;
  var ypos = (screen.height-addwinh)/2;
  qqq = window.open(temp,'openprint','fullscreen=no,channelmode=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width='+addwinw+',height='+addwinh+',top='+ypos+',left='+xpos); 
  qqq.focus();
}

function openPoe(pic)
{
  var temp = '/showpic.php?pic=' + pic + '&lang=' + _lang;
  var xpos = (screen.width-100)/2;
  var ypos = (screen.height-100)/2;
  window.open(temp,'displayWindow','fullscreen=no,channelmode=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=1,height=1,top='+ypos+',left='+xpos); 
}
*/
function fixContentSize() {
   var clientX = window.innerWidth || document.documentElement.clientWidth || document.body.parentElement.clientWidth || document.body.clientWidth;
   var contentWidth = clientX - 320;
   var obj = document.getElementById('content_container');
   if (obj) {
    obj.style.width = contentWidth + 'px';
   }
}
var __currentImg = 1;
var __dates = null;
var __regions = null;
var __prices = null;
$(document).ready(function() {
	    $('a.toggle').each(function() {$(this).bind('click', function(event) {var sub = 'div#submenu_' + $(this).attr('submenu'); $(sub).toggle('slow'); event.preventDefault();}); })
//        $('select#select_region').bind('change',function(event){toggleRegions($(this).val())});
        $('select#select_date').bind('change',function(event){toggleDates($(this).val())});
        $('select#select_hotels').bind('change',function(event){toggleHotels($(this).val())});
        $('select#select_dates').bind('change',function(event){toggleDate($(this).val())});
        $('select#select_star').bind('change',function(event){toggleStar($(this).val())});
//        $('span.img_pager').each(function() {$(this).bind('click',function(event) {__currentImg = parseInt($(this).text()); resetPager(); $(this).css('fontWeight','bold');$('img#hotelImg').attr({src: $(this).attr('chp') }); $('img#hotelImg').fadeIn();})});        
//        $('img#hotelImg').css('cursor','pointer')
//        $('img#hotelImg').bind('click',function(event) {_ShowHotelPic(__currentImg); })
//        __dates = $('table#flights tr.flight_date');        
//        __regions = $('table#spo_list tr.region');        
        $('h4.header').each(function() {var txt = $(this).text(); $(this).after('<table class="header4"><tr><td class="lft">' + txt + '</td><td class="cnt"></td><td class="rgt"></td></tr></table>'); $(this).remove() } );
        __prices = $('table.prices tr');
});
function toggleHotels(hotel) {
    var region = document.getElementById('select_region');
    var url = '?';
    if (region) {
        var url = (region.value != 0) ? '?region=' + region.value + '&' : '?';
    }
    url += (hotel != 0) ? 'hotel='+hotel : '';
    document.location.href = url;
}
function toggleDate(date) {
    var region = document.getElementById('select_region');
    var url = '?';
    if (region) {
        url = (region.value != 0) ? '?region=' + region.value + '&' : '?';
    }
    var hotel = document.getElementById('select_hotels');
    if (hotel) {
        url += (hotel.value != 0) ? 'hotel=' + hotel.value + '&' : '';
    }
    url += (date != 0) ? 'date='+date : '';
    document.location.href = url;
}
function toggleRegions(region) {
    //__regions.each(function() {if ( region != "0" && $(this).attr('region') != region) {$(this).children().hide('fast');  $(this).hide('fast')} else {$(this).children().show('fast');  $(this).show('fast')} colorizeTable()} );
    document.location.href = (region != 0) ? '?region=' + region : '?';
}
function toggleDates(date) {
    //__dates.each(function() {if ( date != "0" && $(this).attr('date') != date) {$(this).children().hide('fast');  $(this).hide('fast')} else {$(this).children().show('fast'); $(this).show('fast')} colorizeTable()} );
    document.location.href = (date != 0) ? '?date=' + date : '?';    
}
function toggleStar(star) {
    var region = document.getElementById('select_region');
    var url = '?';
    if (region) {
        var url = (region.value != 0) ? '?region=' + region.value + '&' : '?';
    }
    url += (star != 0) ? 'star='+star : '';
    document.location.href = url;
}

function resetPager() {
    $('span.img_pager').each(function() {$(this).css('fontWeight','normal')});
}
//addOnLoadEvent(fixContentSize);
var flag = false;
function colorizeTable() {
    //$('table.prices tr:visible:even').addClass('even');
    //$('//table[@class="prices"]//tr[not( @class="region_caption") and position() > 1]')..addClass('even');
    $('table.prices tr:gt(0)').each(function() {
        //console.log($(this),flag,);
        if (!jQuery.className.has(this,'region_caption')) {
            if (flag) {
                $(this).addClass('even');
            }
            flag = !flag;
        }
        
    });
    $('table.prices tr:lt(1)').removeClass('even');
    $('//table[@class="prices"]/thead//tr').addClass('header');
    $('table.prices tr:eq(0)').addClass('header');    
    $('table.selects tr:eq(0)').addClass('header');    
}
addOnLoadEvent(colorizeTable);

function _ShowHotelPic(idx) {
    document.getElementById('sp_container').getElementsByTagName('button')[idx - 1].click();
}

function expoDescr() {
    $('select#expo').change(function(){
        var val = $(this).val();
        $('div#expo_descr').css('visibility','hidden').fadeOut('fast').load('?expo=' + val, function(){
            $('div#expo_descr').css('visibility','visible').fadeIn('fast');
        })
    });
}
addOnLoadEvent(expoDescr);
window.onresize = fixContentSize;

$(function(){
	$('span#show_response_form').click(function(){
		$('#response_form').slideDown('fast');
		$(this).hide('fast');
		$('#hide_response_form').show('fast');
	}).css('cursor','pointer');
	$('span#hide_response_form').click(function(){
		$('#response_form').slideUp('fast');
		$(this).hide('fast');
		$('#show_response_form').show('fast');
	}).css('cursor','pointer');	
})
