

function main_custom () {

	this.global = new Array();

	this.change_big_select = function(){
            
		$(".change_select").each(function(){
                        if ($(this).hasClass("big")) var s_class = "big";
                        else var s_class = "";
                        
                        var z = $(this).attr("z");
                        if (!z) z = 3 ;
                        
			var name = $(this).attr("name");

			var f_child = $(this).find("option:eq(0)");

			var l = $(this).find("option").length;
			if (l > 4) l = 4;
			var h = l * 32;
                        
			var final  = "<div class='s_name " +s_class + "' id='name_"+name+"' onclick='custom.show_drop(\"" + name + "\");'>" + f_child.text() + "</div>";
				final += "<input type='hidden' name='" + name + "' value='" + f_child.val() + "' title='" + f_child.text() + "' id='val_" + name + "' />";
				final += "<div class='drop " +s_class + "' id='drop_" + name + "' style='z-index:"+z+"'>";
				
				final += "<div class='drop_center'>";
			$(this).find("option").each(function(){
				var v = $(this).val();
				var t = $(this).text();

				final += "<div class='op' value='" + v + "' onclick='custom.change_val(\"" + name + "\" , $(this))'>" + t + "</div>";
				})

				final += "</div>";
				final += "</div>";
			var p = $(this).parent("div");
			p.html(final).css({"position" : "relative" , "z-index" : z});
			})
		}

	this.hide_drop = function(){
		$(".drop").hide();
		}

	this.show_drop = function(id){

		this.hide_drop();


		$("#drop_" + id) . show();
		}

	this.change_val = function(id , obj){
		var v = obj.attr("value");
		var t = obj.text();
		$("#val_" + id).val(v).attr({"title" : t});

		this.hide_drop();

		$("#name_" + id).text(t);
		}

        this.change_small_select = function(){

		$(".mini_select").each(function(){
			var name = $(this).attr("name");
                        var f_child = $(this).find("option:selected");
                        var ind = $(this).find("option").index(f_child);
                        
			var final  = "";
                            final += "<input type='hidden' name='" + name + "' value='" + f_child.val() + "' title='" + f_child.text() + "' class='val_" + name + "' />";
			$(this).find("option").each(function(i){
				var v = $(this).val();
				var t = $(this).text();
                                if (i == ind )
                                    var cl = "active";
                                else cl = "";
				final += "<div class='mini_op mini_op_"+name+" " +cl+"' vv='" + v + "' onclick='custom.change_mini_val(\"" + name + "\" , $(this))'>" + t + "</div>";
				})

	
			var p = $(this).parent("div");
			p.html(final);
			})
		}

        this.change_mini_val = function(id , obj){
		var v = obj.attr("vv");
		var t = obj.text();

                $(".mini_op_" + id).removeClass("active");
                obj.addClass("active");
                
                $(".mini_op_" + id + "[vv='"+v+"']").addClass("active");

		$(".val_" + id).val(v);

		}

        this.change_radio = function (obj,name){
                var v = obj.children("input").val();

                $("." + name).removeClass("active");
                obj.addClass("active");

                if (v == "RoundTrip")
                    custom.global['search_type'] = "flights_1";
                if (v == "OneWay")
                    custom.global['search_type'] = "flights_2";
                if (v == "MultiLeg")
                    custom.global['search_type'] = "flights_3";
                
               
                
            }
	this.login = function(){
		$(".overlay").css({"opacity" : 0 , "display" : "block"}).animate({"opacity" : 1} , 500);

		$(".popups").css({"opacity" : 0 , "display" : "block"}).animate({"opacity" : 1} , 500);

		$(".pops").hide();
		$(".popup_login").show();
		}



	this.close_pop = function(){
		$(".overlay").animate({"opacity" : 0} , 500 , function(){
			$(this).css({"display" : "none"});
			});

		$(".popups").animate({"opacity" : 0} , 500 , function(){
			$(this).css({"display" : "none"});
			});
		}
        this.slide_init = function(){
		var total_slides = $("#slider .inner a").length;
		var w = total_slides * 296;

		$(".inner").width(w);

		custom.global['curr_slide'] = 0;
		custom.global['total_slide'] = total_slides;

	}

	this.slide_step = function(step){

		custom.global['curr_slide'] = step;
		var l = (step - 1 ) * 296;

		$(".inner").animate({"left" : (l * (-1)) + "px"} , 500);

                $(".control").removeClass("active");
                $(".control:eq("+(step - 1)+")").addClass("active");

	}

        this.init_timer = function(){
            var this_step = custom.global['curr_slide'] + step;
            if (this_step < 0)
                    this_step = total_steps;
            if (this_step > total_steps)
                    this_step = 0;
        }

        this.init_autocomplete = function(){
            
            $(".autocomplete").keyup(function(e){
                var x = e.keyCode;
                if (x != 40 && x != 38 && x != 13){

                    custom.global['auto_index'] = 0;
                    
                    var obj = $(this);
                    var inp = $(this).attr("name");
                    var city = $(this).val();

                    var link = $(this).attr("link");

                    if (!link)
                        var l = "/en/search_city";
                    else var l = "/en/search_hotel";
                    if (city.length >= 2){
                        $.get(l , {"city" : city , "input" : inp} , function(data){

                            var flague = $("#autocomplete").length;
                            if (flague){
                                $("#arr").remove();
                                $("#autocomplete").remove();
                            }


                            $("body").append(data);
                            var off = obj.offset();
                            var xx = obj.attr("xx");
                            var yy =obj.attr("yy");
                            if(!xx) xx = 38;
                            if(!yy) yy = 34;
                            $("#autocomplete").css({"top" : off.top + xx*1 , "left" : off.left + 2}).show();
                            $("#arr").css({"top" : off.top + yy*1 , "left" : off.left + 10}).show();

                            var final_l = 0;
                            $("#autocomplete .auto_option").each(function(){
                                final_l += $(this).height() + 11;
                            })
                            if (final_l > 220) final_l = 220;
                            $("#autocomplete").height(final_l);

                            custom.global['auto'] = true;
                            custom.global['auto_total'] = $(".auto_option").length;
                        })
                    }
                }
            })
        }

        this.autocomplete = function(input , text , code, img){
            $("#"+input).val(code);
            $("input[name='"+input+"']").val(text).focus();
            
            $("input[name='"+input+"']").parent("div").parent("td").find("img").attr('src',img)
            
            $("#autocomplete , #arr").remove();
            
            custom.global['auto'] = false;
            custom.global['block_submit'] = false;

            custom.change_destination();

            var al = $("input[name='"+input+"']").attr("allias");
            var v = text;

            $("input[allias='"+al+"']").val(v);
            
           
            
            var al = $("#"+input).attr("allias");
            var v = code;

            $("input[allias='"+al+"']").val(v);
            
        }

        this.init_picker = function(){
            Date.format = 'mm/dd/yyyy';
            $('.d_picker').val("").datePickerMultiMonth({"numMonths":2}).val("mm/dd/yyyy");

//            $('input[name="External_FlightFareSearch_ReturnDate"]').change(function(){
//
//                                var d_2 = new Date($(this).val());
//                                var d_1 = new Date($('input[name="External_FlightFareSearch_DepartureDate"]:eq(0)').val());
//
//                                $.get("/en/ajax" , {"ac" : "check_date" , "val_1" : d_1, "val_2" : d_2 } , function(){
//                                    if (data){
//                                        alert(data);
//                                    }
//                                })
//                                return false;
//                });

//            $(".d_picker").focus(function(){
//                var obj = $(this);
//                var inp = $(this).attr("name");
//                var date = $(this).val();
//
//                $.get("/en/datepicker" , {"date" : date , "input" : inp} , function(data){
//
//                        var flague = $("#datepicker").length;
//                        if (flague){
//                            $("#datepicker").remove();
//                        }
//
//                        $("body").append(data);
//                        var off = obj.offset();
//                         var xx = obj.attr("xx");
//
//                            if(!xx) xx = 38;
//
//                        $("#datepicker").css({"top" : off.top + xx*1 , "left" : off.left + 2}).show();
//
//                    })
//
//            })
        }

        this.change_date = function(date , inp , obj){

            $("input[name='"+inp+"']").val(date);
            $("input[name='val_"+inp+"']").val(obj.attr("date"));

            $(".day").removeClass("active");
            obj.addClass("active")

            var al = $("input[name='"+inp+"']").attr("allias");
            var v = date;

            $("input[allias='"+al+"']").val(v);

            custom.change_destination()
        }

        this.close_calendar = function(){
            $("#datepicker").remove();
        }

        this.change_month = function(date , inp){
            var obj = $("input[name='"+inp+"']");
            $.get("/en/datepicker" , {"date" : date , "input" : inp} , function(data){

                $("#datepicker").remove();

                $("body").append(data);
                var off = obj.offset();
                
                $("#datepicker").css({"top" : off.top + 38 , "left" : off.left + 2}).show();

                var v = obj.val();
                if (v){
                    $("td[date='"+v+"']").addClass("active");
                }
            })
        }



        this.check_form = function(){
            
            var type = custom.global['search_type'];

            var flague = true;
            if (custom.global['auto']){
                
                var obj = $(".auto_option").eq(custom.global['auto_index'] -1);
                var ev = obj.attr("onclick");
                if (ev) eval(ev);

                
                return false
                
            }
            else {
                if (type == "flights_1"){
                    if ( !$('input[name="Search_From"]').val().length ||  !$('input[name="Search_To"]').val().length )
                        {
                            flague = false;
                            alert("Please complete all the fileds");
                        }
                    if ( (!$('input[name="External_FlightFareSearch_DepartureDate"]:eq(0)').val().length ||  $('input[name="External_FlightFareSearch_DepartureDate"]:eq(0)').val() == "mm/dd/yyyy" ||
                         !$('input[name="External_FlightFareSearch_ReturnDate"]:eq(0)').val().length ||  $('input[name="External_FlightFareSearch_ReturnDate"]:eq(0)').val() == "mm/dd/yyyy") && flague)
                        {
                            flague = false;
                            alert("Please complete all the fileds");
                        }
                    if (flague){

                        var d1 = $('input[name="val_External_FlightFareSearch_DepartureDate"]:eq(0)').val();
                        var d2 = $('input[name="val_External_FlightFareSearch_ReturnDate"]:eq(0)').val();

                        d1 = new Date(d1);
                        d2 = new Date(d2);

                        var diff = d2 - d1;
                        var days = Math.round(diff/(1000*60*60*24));
                        if (days < 1){
                            flague = false;
                            alert("'Check-in Date' should be earlier than 'Check-out Date'");
                        }
//
//                        $.get("/en/ajax" , {"ac" : "check_date" , "val_1" : $('input[name="External_FlightFareSearch_DepartureDate"]:eq(0)').val(), "val_2" : $('input[name="External_FlightFareSearch_ReturnDate"]:eq(0)').val()} , function(data){
//                            if (data){
//                                flague = false;
//                                alert(data);
//                            }
//                        })
                    }
                } // end flights_1
                if (type == "flights_2"){
                    if ( !$('input[name="Search_From_3"]').val().length ||  !$('input[name="Search_To_3"]').val().length )
                        {
                            flague = false;
                            alert("Please complete all the fileds");
                        }
                    if ( (!$('input[name="External_FlightFareSearch_DepartureDate"]:eq(1)').val().length ||  $('input[name="External_FlightFareSearch_DepartureDate"]:eq(1)').val() == "mm/dd/yyyy") && flague)
                        {
                            flague = false;
                            alert("Please complete all the fileds");
                        }
                   
                } // end flights_2

                if (type == "flights_3"){
                    if ( !$('input[name="Search_From_31"]').val().length ||  !$('input[name="Search_To_31"]').val().length )
                        {
                            flague = false;
                            alert("Please complete all the fileds");
                        }
                    if ( (!$('input[name="EF_DepartureDate1"]').val().length ||  $('input[name="EF_DepartureDate1"]').val() == "mm/dd/yyyy") && flague)
                        {
                            flague = false;
                            alert("Please complete all the fileds");
                        }

                } // end flights_3

                if (type == "hotels"){
                    if ( !$('input[name="External_HotelSearch_City"]').val().length )
                        {
                            flague = false;
                            alert("Please complete all the fileds");
                        }
                    if ( (!$('input[name="External_HotelSearch_CheckInDate"]').val().length ||  $('input[name="External_HotelSearch_CheckInDate"]').val() == "mm/dd/yyyy" ||
                         !$('input[name="External_HotelSearch_CheckOutDate"]').val().length ||  $('input[name="External_HotelSearch_CheckOutDate"]').val() == "mm/dd/yyyy") && flague)
                        {
                            flague = false;
                            alert("Please complete all the fileds");
                        }
                    if (flague){
                        var d1 = $('input[name="val_External_HotelSearch_CheckInDate"]').val();
                        var d2 = $('input[name="val_External_HotelSearch_CheckOutDate"]').val();

                        d1 = new Date(d1);
                        d2 = new Date(d2);

                        var diff = d2 - d1;
                        var days = Math.round(diff/(1000*60*60*24));
                        if (days < 1){
                            flague = false;
                            alert("'Check-in Date' should be earlier than 'Check-out Date'");
                        }

//                        $.get("/en/ajax" , {"ac" : "check_date" , "val_1" : $('input[name="External_HotelSearch_CheckInDate"]').val(), "val_2" : $('input[name="External_HotelSearch_CheckOutDate"]').val()} , function(data){
//                            if (data){
//                                flague = false;
//                                alert(data);
//                            }
//
//                        })
                        
                    }

                } // end hotels


                if (type == "cars"){
                    if ( !$('input[name="Search_From"]').val().length ||  !$('input[name="Search_To"]').val().length )
                        {
                            flague = false;
                            alert("Please complete all the fileds");
                        }
                    if ( (!$('input[name="External_CarSearch_PickupDate"]').val().length ||  $('input[name="External_CarSearch_PickupDate"]').val() == "mm/dd/yyyy" ||
                         !$('input[name="External_CarSearch_DropOffDate"]').val().length ||  $('input[name="External_CarSearch_DropOffDate"]').val() == "mm/dd/yyyy") && flague)
                        {
                            flague = false;
                            alert("Please complete all the fileds");
                        }
                    if (flague){
                        var d1 = $('input[name="val_External_CarSearch_PickupDate"]').val();
                        var d2 = $('input[name="val_External_CarSearch_DropOffDate"]').val();

                        d1 = new Date(d1);
                        d2 = new Date(d2);

                        var diff = d2 - d1;
                        var days = Math.round(diff/(1000*60*60*24));
                        if (days < 1){
                            flague = false;
                            alert("'Check-in Date' should be earlier than 'Check-out Date'");
                        }

//                        $.get("/en/ajax" , {"ac" : "check_date" , "val_1" : $('input[name="External_CarSearch_PickupDate"]').val(), "val_2" : $('input[name="External_CarSearch_DropOffDate"]').val()} , function(data){
//                            if (data){
//                                flague = false;
//                                alert(data);
//                            }
//                        })
                    }

                } // end cars


                if (type == "cruise"){
                    
                    if ( (!$('input[name="StartDate"]').val().length ||  $('input[name="StartDate"]').val() == "mm/dd/yyyy") && flague)
                        {
                            flague = false;
                            alert("Please complete all the fileds");
                        }
                    

                } // end cruise
            
            return flague;
            //return false;
            }
        }

        this.send_promo = function(){
            var from = $(".promo").find("input[name='from']").val();
            var to = $(".promo").find("input[name='to']").val();
            var mail = $(".promo").find("input[name='mail']").val();
            
            $.post("/en/best-offer.html" , {"from" : from , "to" : to , "mail" : mail} , function(data){
                $(".promo").html(data).css({"background" : "url('/theme/site_theme/img/promo_2.png') no-repeat center top "})

            })
        }

        this.new_promo = function(){
            $.get("/en/best-offer.html" , function(data){
                $(".promo").html(data).css({"background" : ""})
            })
        }

        this.check_date = function(val_1 , val_2){
            $.get("/en/ajax" , {"ac" : "check_date" , "val_1" : val_1 , "val_2" : val_2} , function(data){
                if (data){
                    alert(data);
                }
            })
        }

        this.change_destination = function(){

            if (custom.global['destination'] == "flights"){
                
                var type = custom.global['search_type'];
                
                if (type == "flights_1"){
                    custom.global['search_from'] = $('input[name="Search_From"]').val()
                    custom.global['search_to'] = $('input[name="Search_To"]').val();
                    custom.global['date_from'] = $('input[name="val_External_FlightFareSearch_DepartureDate"]:eq(0)').val()
                    custom.global['date_to'] = $('input[name="val_External_FlightFareSearch_ReturnDate"]:eq(0)').val()
                }
                
                
                if (type == "flights_2"){
                    custom.global['search_from'] = $('input[name="Search_From_3"]').val()
                    custom.global['search_to'] = $('input[name="Search_To_3"]').val()
                    custom.global['date_from'] = $('input[name="val_External_FlightFareSearch_DepartureDate"]:eq(1)').val()
                    custom.global['date_to'] = "";
                }
                
                

                
                    
                
                var city_from = custom.global['search_from']//$("input[name='Search_From']").val();
                var city_to = custom.global['search_to']//$("input[name='Search_To']").val();
                var date_from = custom.global['date_from'] //$("input[name='val_External_FlightFareSearch_DepartureDate']").val();
                var date_to = custom.global['date_to']//$("input[name='val_External_FlightFareSearch_ReturnDate']").val();
                
                if (custom.global['last_location_from'] != city_from){
                    custom.global['last_location_from'] = city_from;

                    var from_l = city_from.length;
                    var ind = 0;
                    for (i=1 ; i<=30 ; i++){

                        setTimeout(function(){
                            ind++;

                            if (ind <= from_l) var t = city_from[ind -1 ];
                                else t = "";

                            if ( t ){
                                $("#leaving_from .letter:eq(" + (ind - 1) + ")").html( t );
                                
                            }
                            else $("#leaving_from .letter:eq(" + (ind - 1) + ")").html("");

                        } , i*100 );
                    }
                    
                }

                if (custom.global['last_location_to'] != city_to){
                    custom.global['last_location_to'] = city_to;

                    var from_t = city_to.length;
                    var ind_2 = 0;
                    for (i_2=1 ; i_2<=30 ; i_2++){

                        setTimeout(function(){
                            ind_2++;

                            if (ind_2 <= from_t) var t_2 = city_to[ind_2 -1 ];
                                else var t_2 = "";

                            if ( t_2 ){
                                $("#leaving_to .letter:eq(" + (ind_2 - 1) + ")").html( t_2 );
                                
                            }
                            else $("#leaving_to .letter:eq(" + (ind_2 - 1) + ")").html("");

                        } , i_2*100 );
                    }
                }


                if (custom.global['last_date_from'] != date_from){
                    custom.global['last_date_from'] = date_from;

                    var d_from = date_from.length;
                    var ind_3 = 32;
                    for (i_3 = 32 ; i_3 <= 48 ; i_3++){

                        setTimeout(function(){
                            ind_3++;

                            if (ind_3 - 32 <= d_from ) var t_3 = date_from[ind_3 - 33 ];
                                else var t_3 = "";

                            if ( t_3 ){

                                $("#leaving_from .letter:eq(" + (ind_3 - 1) + ")").html( t_3 );
                            }
                            else $("#leaving_from .letter:eq(" + (ind_3 - 1) + ")").html("");

                        } , (i_3 - 30)*100 );
                    }
                }

                if (custom.global['last_date_to'] != date_to){
                    custom.global['last_date_to'] = date_to;

                    var d_to = date_to.length;
                    var ind_4 = 32;
                    for (i_4 = 32 ; i_4 <= 48 ; i_4++){

                        setTimeout(function(){
                            ind_4++;

                            if (ind_4 - 32 <= d_to ) var t_4 = date_to[ind_4 - 33 ];
                                else var t_4 = "";

                            if ( t_4 ){

                                $("#leaving_to .letter:eq(" + (ind_4 - 1) + ")").html( t_4 );
                            }
                            else $("#leaving_to .letter:eq(" + (ind_4 - 1) + ")").html("");

                        } , (i_4 - 30)*100 );
                    }
                }


            }


        }

    this.allias_text = function(){
        $("input[allias]").change(function(){
            var al = $(this).attr("allias");
            var v = $(this).val();

            $("input[allias='"+al+"']").val(v);
        })
    }

    this.main_init = function(){
        custom.change_big_select();
        custom.change_small_select();
        custom.slide_init();
        custom.init_autocomplete();
        custom.init_picker();
        //custom.allias_text();
        
        custom.global['auto_play'] = true;

        //Cufon.replace('.menu a, .green_but');

        $("#slider").hover(function(){custom.global['auto_play'] = false} ,
            function(){custom.global['auto_play'] = true} );
    }

}

var custom = new main_custom();

$(document).ready(function(){
		
        //custom.main_init();
        
                setInterval(function(){
                    if (custom.global['auto_play']){
                        var this_step = custom.global['curr_slide'] + 1;
                        var total_steps = custom.global['total_slide'] ;

                        if (this_step > total_steps)
                                this_step = 1;

                        custom.slide_step(this_step)
                    }
                } , 8000 , custom)

        $(document).click(function(e) {
            setTimeout(function(){
                var $clicked = $(e.target);

                if (!$clicked.parents().hasClass("auto_option") && !$clicked.hasClass("autocomplete")){
                    $("#autocomplete").remove();
                    $("#arr").remove();
                    
                    custom.global['auto'] = false;
                }

                if (!$clicked.parents().hasClass("datepicker") && !$clicked.hasClass("d_picker")){
                    $("#datepicker").remove();
                }

                if (!$clicked.parents().hasClass("drop") && !$clicked.hasClass("s_name")){
                    $(".drop").hide();
                }
            } , 500)
        });


        $("#flight1").click(function()
           {
               $("#search").load('/?ac=ajax&task=flights_search');
               $("#flight1").addClass("flights_active");
               $("#car1").removeClass("cars_active");
               $("#hotel1").removeClass("hotels_active");
               $("#cruise1").removeClass("cruise_active");
           })
            $("#hotel1").click(function()
           {
                $("#hotel1").addClass("hotels_active");
                $("#flight1").removeClass("flights_active");
                $("#car1").removeClass("cars_active");
                $("#search").load('/?ac=ajax&task=hotels_search');
                $("#cruise1").removeClass("cruise_active");
           })
            $("#car1").click(function()
           {
                $("#car1").addClass("cars_active");
                $("#flight1").removeClass("flights_active");
                $("#hotel1").removeClass("hotels_active");
                $("#search").load('/?ac=ajax&task=cars_search');
                $("#cruise1").removeClass("cruise_active");
           })
           $("#cruise1").click(function()
           {
                $("#car1").removeClass("cars_active");
                $("#flight1").removeClass("flights_active");
                $("#hotel1").removeClass("hotels_active");
                $("#search").load('/?ac=ajax&task=cruise_search');
                $("#cruise1").addClass("cruise_active");
           })

     $(".m_galery").hover(function(){

         $(this).children(".m_st").stop().animate({"padding-top" : 30} , 300)

     } , function(){
         $(this).children(".m_st").stop().animate({"padding-top" : 8} , 300)
     });

})


$(document).keyup(function(e){
    var x = e.keyCode;
    if (custom.global['auto'] && (x==40 || x==38)){
            if (x == 40){
                var ind = custom.global['auto_index'] + 1;
                if (ind > custom.global['auto_total']) ind = 1;


            }
            if (x == 38){
                var ind = custom.global['auto_index'] - 1;
                if (ind < 1) ind = custom.global['auto_total'];
            }

            custom.global['auto_index'] = ind;
            
            $(".auto_option").removeClass("selected");
            $(".auto_option").eq(ind -1).addClass("selected");

            $("#autocomplete").scrollTo($(".auto_option").eq(ind -1));
        }
    if (custom.global['auto'] && x == 13 ){
        
        
    }
    // 40 down
    //38 up
})

function randomFromTo(from, to){
   return Math.floor(Math.random() * (to - from + 1) + from);
}
