var itemwidth = "428";
window.onresize=resize_acc;
document.onkeydown=what_key;
var key;

function what_key(event){
    if (!$.browser.msie)
        key=parseInt(event.keyCode)
    else
        key=parseInt(window.event.keyCode)
}

function tpres(item){
    if ((key > 48 && key < 58) || (key == 32) || (key > 64 && key < 91) || (key==188))
        $("#send").attr("value","g");

}

var pic = new Object();
for (n=0;n<9;n++){
    eval("pic.m"+n+"p = new Image();");
    eval("pic.m"+n+"a = new Image();");
    eval("pic.m"+n+"p.src = \"/img/m"+n+".gif\"");
    eval("pic.m"+n+"a.src = \"/img/m"+n+"_a.gif\"");
}

function take_id(item){
    id = $(item).attr("id");
    id_=id.substring(5)
    return id_
}

function show_sub(item){
    id = take_id(item)
    $("#sub_"+id).show();
}
function hide_sub(item){
    id = take_id(item)
    $("#sub_"+id).hide();
}

function activ_ico(item){
    id = take_id(item)
    $("#m_img"+id).attr("src",eval("pic.m"+id+"a.src")) 
}
function pasiv_ico(item){
    id = take_id(item)
    // alert (menu_pic[id].src)
    $("#m_img"+id).attr("src",eval("pic.m"+id+"p.src")) 
}

    
$(document).ready(function(){

    //$.getScript('//yandex.st/share/share.js')

    if (!$("#ctl00_generalContent_LblMessage").html()) $("#ctl00_generalContent_LblMessage").hide()

    $(".test").hrzAccordion({
        eventTrigger:"mouseover",
        openOnLoad:1,
        cycle: false
    });
    resize_acc()

    $(".icon_menu").mouseover(
        function(){
            activ_ico(this);
            show_sub(this);
        }
        )
    $(".icon_menu").mouseout(
        function(){
            pasiv_ico(this);
            hide_sub(this);
        }
        )
    if ( $("#map_body").css("width") )
        loadMap();

    if ($('input[name=c_phone]').html() == '') {
        $('input[name=c_phone]').parents('form').append('<input type="hidden" name="sec" value="332">');
    }

})

function resize_acc(){
    x = $(window).width()-335;
    if (x<660) x=660
    $("#gray_bg").css("width",x+17);
    $("#accordion").css("width",x-20);
    h_x=Math.round((x-20 - itemwidth)/acc_count);
    $(".handle_bg").css("width",h_x)
    $(".handle").css("width",h_x)
    $(".hand_dark").css("width",h_x)
}



var dmenu=false;

function sub_menu(id,type) {
	
    var hideTime=1000;

    if(document.getElementById('sub_menu_'+id)) {
	
        if(type==1)
        {
			
            dmenu=id;
			
            for(n=0;n<30;n++) {
                if(document.getElementById('sub_menu_'+n)) {
                    document.getElementById('sub_menu_'+n).style.display='none';
                }
            }
            if(document.getElementById('sub_menu_'+id))
            {
                document.getElementById('sub_menu_'+id).style.display='block';
            }
        } else if (type==2) {
		
            dmenu='';
            setTimeout("if(!dmenu) document.getElementById('sub_menu_"+id+"').style.display='none';", hideTime); 
			
        }
	
    }

}

function changeDep(num)
{
    for(n=0;n<40;n++)
    {
        if(document.getElementById('dep'+num+'_'+n))
        {
            var fl=true;
            if(document.getElementById('dep'+num+'_'+n).className=='sub_menu1')
            {
                document.getElementById('dep'+num+'_'+n).className='sub_menu1_act';
				
            }
            else
            {
                document.getElementById('dep'+num+'_'+n).className='sub_menu1';
            }
        }
    }
	
    if(fl)
    {
        return true;
    }
    else
    {
        return false;
    }
}
function over_t(item){
    $(item).attr("class","tour_menu_a")
}
function out_t(item){
    $(item).attr("class","tour_menu")
}

function select_city(dir_u){
    city_d =  $("#city").attr("value")
    if (city_d)  urls = dir_u.split("/")
    //alert(urls[1])
    if (!urls[1] || urls[1]!="countrys" )  document.location="/?city="+city_d
    else  {
        new_u = "/"+urls[0]+"/"+urls[1]+"/"+urls[2] +"/"+ city_d + "/"
        //if(urls[4]) new_u = new_u + urls[4] + "/"
        //alert(new_u)
        document.location=new_u
    }
}

function select_country(){
    country =  $("#go_conutry").attr("value")
    document.location="/main/countrys/"+country
}

var actor;
var txt1
var txt2
function sub_aspform(){
    $("#login").attr('name', 'ctl00$Login$ctl02');
    $("#pass").attr('name', 'ctl00$Login$ctl02');
    $('form[name=aspnetForm1]').append('<input type="hidden" name="ctl00$Login$ctl03" value="OK" /><input type="hidden" name="__EVENTARGUMENT" value="" /><input type="hidden" name="__EVENTTARGET" value="" /><input type="hidden" name="__LASTFOCUS" value="" /><input type="hidden" name="__VIEWSTATE" value="" />').submit();
}

function focus_inp(item,val){
    if ($(item).val() ==val) $(item).attr("value","")
}
time=0
function sent_recall(){
    name= $("#feed_name").val();
    phone= $("#feed_phone").val();
    if (!phone || phone=="Мой телефон...") return;
    n=0;
    items = $(".feed_time")
    $.each(items,function(index, element){
        if ($(element).attr("checked")) time=n
        n++
    }
    )

    $.post(          
        '/incs/recall.php',
        {
            name:name,
            phone:phone,
            time:time
        },
        onc_Success
        );  
}

function onc_Success(date){
    // alert(date)
    text1=""
    if (time==0) text1=" сейчас"
    if (time==1) text1=" через час"
    if (time==2) text1=" в течении дня"
    text ="<p>Вам перезвонят"
    $("#call_txt").html(text+text1)
}


/**
* @author Falchenko Maxim aka be3
* @plugin_page http://tops.net.ua/jquery_addtocopy/
* @desc Adds a link to the copied text
* @version 1.2
* @example
* $("#content").addtocopy();
* @license free
**/
jQuery.fn.addtocopy = function(usercopytxt) {
    var options = {htmlcopytxt: '<br>More: <a href="'+window.location.href+'">'+window.location.href+'</a><br>', minlen: 1, addcopyfirst: false}
    $.extend(options, usercopytxt);
	var copy_sp = document.createElement('span');
	copy_sp.id = 'ctrlcopy';
	copy_sp.innerHTML = options.htmlcopytxt;
	return this.each(function(){
		$(this).mousedown(function(){$('#ctrlcopy').remove();});
		$(this).mouseup(function(){
			if(window.getSelection){	//good times 
				var slcted=window.getSelection();
				var seltxt=slcted.toString();
				if(!seltxt||seltxt.length<options.minlen) return;
				var nslct = slcted.getRangeAt(0);
				seltxt = nslct.cloneRange();
				seltxt.collapse(options.addcopyfirst);
				seltxt.insertNode(copy_sp);
				if (!options.addcopyfirst) nslct.setEndAfter(copy_sp);
				slcted.removeAllRanges();
				//slcted.addRange(nslct);
			} else if(document.selection){	//bad times
				var slcted = document.selection;
				var nslct=slcted.createRange();
				var seltxt=nslct.text;
				if (!seltxt||seltxt.length<options.minlen) return;
				seltxt=nslct.duplicate();
				seltxt.collapse(options.addcopyfirst);
				seltxt.pasteHTML(copy_sp.outerHTML);
				if (!options.addcopyfirst) {nslct.setEndPoint("EndToEnd",seltxt); nslct.select();}
			}
		});
  });
}

// search
$(document).ready(function() {
    $(document)[0].oncontextmenu = function() {return false;}
    
    $('.search_box form').live('submit',function() {
        var sError = '';
        if(!$('#search_country').val() || $('#search_country').val() == 0) {
            sError = 'Выберите страну';
        }
        else if ($('#search_from').val().length == 0) {
            sError = 'Введите дату "с"';
        }
        else if ($('#search_to').val().length == 0) {
            sError = 'Введите дату "по"';
        }
       
        if (sError) {
            alert(sError);
            return false;
        }
       
        return true;
    });    
   
    $('#search_country,#search_city').live('change', function() {
        if ($('#search_country').val() > 0) {
            $.get('/main/tours', {
                data: $('#search_country').parents('form').serialize(),
                'do': 'search' 
            }, function(data){
                $('#search_city,#search_season').removeAttr('disabled');
                data = $.parseJSON(data);               
                $('#search_city').html(data.cities);
                $('#search_season').html(data.seasons);
            });
        } else {
            $('#search_city,#search_season').attr('disabled','disabled');
        }
    });
    if (jQuery.fn.datepicker)
        $(".datepicker").datepicker({
            dateFormat: 'dd.mm.yy', 
            minDate: '+3d',
            dayNamesMin:['Вс','Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб'],
            firstDay: 1,
            monthNames:['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'],
            beforeShow: function(date, inst){
                if ($(inst).attr('id') == 'search_to') {
                    if ($('#search_from').datepicker('getDate')) {
                        var minData = $('#search_from').datepicker('getDate');
                        minData = new Date(minData.getFullYear(), minData.getMonth(), minData.getDate() + 1);
                        return {
                            minDate: minData
                        };
                    }
                }
                else if ($(inst).attr('id') == 'search_from') {
                //                if ($('#search_to').datepicker('getDate')) {
                //                    var maxData = $('#search_to').datepicker('getDate');
                //                    return {
                //                        maxDate: maxData
                //                    };
                //                }
                }
            },
            onSelect: function (date, inst) {
                if ($(inst).attr('id') == 'search_from') {
                    var date = $('#search_from').datepicker('getDate');
                    date = new Date(date.getFullYear(), date.getMonth()+1, date.getDate());
                    $('#search_to').datepicker('setDate', date);
                }
            }
        });
    
    $(function(){
        $("#main_content:not(.main_content)").addtocopy({
            htmlcopytxt: '<br><br><br>При копировании ссылка на oostmakrt.ru обязательна!&nbsp;<a href="'+window.location.href+'">'+window.location.href+'</a>'
            });
    });
});
