var HelpOnStatus = 0;

$(document).ready(function() {

    $().dateSelectBoxes('ctl00_ContentPlaceHolderMain_ddlMonth', 'ctl00_ContentPlaceHolderMain_ddlDay', 'ctl00_ContentPlaceHolderMain_ddlYear');
    $().dateSelectBoxes('ctl00_ContentPlaceHolderMain_dateControl_ddlMonth', 'ctl00_ContentPlaceHolderMain_dateControl_ddlDay', 'ctl00_ContentPlaceHolderMain_dateControl_ddlYear');
    $().dateSelectBoxes('ctl00_ContentPlaceHolderMain_dateBirthday_ddlMonth', 'ctl00_ContentPlaceHolderMain_dateBirthday_ddlDay', 'ctl00_ContentPlaceHolderMain_dateBirthday_ddlYear');

    $("#q").val("SEARCH");
    $("#q").focus(function() {
        $("#q").val("");
    });

    $("#q").blur(function() {
        $("#q").val("SEARCH");
    });

    $(".styled-select select").bind('change', function() {
        var index = $(".styled-select select").index(this);
        var val = $(".styled-select select:eq(" + index + ")").find("option[value='" + this.value + "']").text();
        $(".styled-select .select-overlay:eq(" + index + ")").html(val);
    });

    $(".styled-select .select-overlay").each(function(index, el) {
        var selectedText = $(".styled-select select:eq(" + index + ")").find("option[selected='true']").text();
        var firstText = $(".styled-select select:eq(" + index + ")").find("option:eq(0)").text();
        selectedText == null ? $(el).html(firstText) : $(el).html(selectedText);
    });

    $(".styled-radiobutton input").bind('click', function() {
        var index = $(".styled-radiobutton input").index(this);
        $(".radiobutton-text").attr('class', 'radiobutton-text');
        $(".styled-radiobutton .radiobutton-text:eq(" + index + ")").attr('class', 'radiobutton-text radiobutton-text-active');
    });

    $(".styled-radiobutton input").each(function(index, el) {
        if ($(el).attr('checked') == true) {
            $(".styled-radiobutton .radiobutton-text:eq(" + index + ")").attr('class', 'radiobutton-text radiobutton-text-active');
        }
    })

    $("#revert").bind('click', function() {
        var from = $("#ctl00_ContentPlaceHolderMain_textFrom").val();
        var to = $("#ctl00_ContentPlaceHolderMain_textTo").val();
        $("#ctl00_ContentPlaceHolderMain_textFrom").val(to);
        $("#ctl00_ContentPlaceHolderMain_textTo").val(from);
        return false;
    });

  

    initAirlinesLogos();
    initAirlinesEventClick();
    setPositionsForHelpBlocks();
    initializeHelpOn();

    $("#helpon").click(function() {
        if (getCookie('helpon') == '0' || getCookie('helpon') == '') {
            $("#helpon").attr('class', 'bubble-active');
            $(".box-help").each(function(index, el) {
                var currentclass = $(el).parent().attr('class');
                $(el).parent().attr('class', currentclass + ' active-popup');
            });

            setCookie('helpon', 1, 1);
            return false;
        }

        if (getCookie('helpon') == 1) {
            $("#helpon").attr('class', 'bubble');
            $(".box-help").each(function(index, el) {
                var currentclass = $(el).parent().attr('class');
                currentclass = currentclass.replace(' active-popup', '');
                $(el).parent().attr('class', currentclass);
            });
            $('#hiddenHelpOnFlag').val(0);
            setCookie('helpon', 0, 1);
            return false;
        }
    })


    //    $().dateSelectBoxes('ctl00_ContentPlaceHolderMain_ddlMonth', 'ctl00_ContentPlaceHolderMain_ddlDay', 'ctl00_ContentPlaceHolderMain_ddlYear');

})



function initializeHelpOn() {
    if (getCookie('helpon') == '0' || getCookie('helpon') == '') {
        $("#helpon").attr('class', 'bubble');
        $(".box-help").each(function(index, el) {
            var currentclass = $(el).parent().attr('class');
            currentclass = currentclass.replace(' active-popup', '');
            $(el).parent().attr('class', currentclass);
        });
        setCookie('helpon', 0, 1);
        return false;

    }
    if (getCookie('helpon') == 1) {
        $("#helpon").attr('class', 'bubble-active');
        $(".box-help").each(function(index, el) {
            var currentclass = $(el).parent().attr('class');
            $(el).parent().attr('class', currentclass + ' active-popup');
        });
        setCookie('helpon', 1, 1);
        return false;

    }
}


var containerWidth = 0;
var scrollBarWidth = 1065;
var scrollBarPosition = 0;
var scrollBarLimit = 0;
var scrollStep = 100;
var scrollBlock = false;

function initAirlinesLogos(){
	
	$("#logos-container .logo img").each(function(index, el){
		containerWidth += ( parseInt($(el).attr('width')) + 4 );
	})
	
	$("#logos-container").css('width',containerWidth + "px");
	
	scrollBarLimit = -(containerWidth - 1000);
	
	$("#scroll-container #arrow-right").bind('click',function(){
		if ( scrollBarPosition > scrollBarLimit && scrollBlock == false ){
			$("#scroll-container #arrow-left").css('display','block');
			scrollAirlineLogo(scrollBarPosition - scrollStep);
		}
		return false;
	})
	
	$("#scroll-container #arrow-left").bind('click',function(){																	 
		if ( scrollBarPosition < 100 && scrollBlock == false ){
			$("#scroll-container #arrow-right").css('display','block');
			scrollAirlineLogo(scrollBarPosition + scrollStep);
		}
		return false;
	})			
	
	
}

function scrollAirlineLogo(leftPx) {
	scrollBlock = true;
	$("#logos-container").animate({left:leftPx},function(){
		if ( scrollBarPosition <= scrollBarLimit ){
			$("#scroll-container #arrow-right").css('display','none');
		}
		if ( scrollBarPosition >= 100 ){
			$("#scroll-container #arrow-left").css('display','none');
		}
		scrollBlock = false;
	});
	scrollBarPosition = leftPx;
}


function initAirlinesEventClick() {
    $("#logos-container .logo img").bind('click', function() {
        $("#phone").html($(this).attr("phone"));
        $("#website").html($(this).attr("website"));
        $("#large-logo-container .clogo img").attr("src", $(this).attr("logourl"));
        $("#description").html($(this).attr("description"));
    })
}

function popUp(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=535,height=435');");
}
function popUpNew(url) {
   var day = new Date();
   var id = day.getTime();
   eval("page" + id + " = window.open(url, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=734,height=696');");
}


function close() {
    $('#downloadformcontainer').css('display', 'none');
    $('#windowShadow').css('display', 'none');
    $('#ctl00_ContentPlaceHolderMain__reValidatorEmail').css('display', 'none');
    $('#ctl00_ContentPlaceHolderMain__revHomePhone').css('display', 'none');
}

function setPositionsForHelpBlocks() {
   $(".box-help").each(function(index, el) {
      var classAttribute = $(el).attr('class');
      var isLeft = /left/.test(classAttribute);

      var offsetY = $(el).attr('offsety');
      if (offsetY == undefined) offsetY = 0;

      var offsetX = $(el).attr('offsetx');
      if (offsetX == undefined) offsetX = 0;
      offsetX = parseInt(offsetX);

      $(el).css("margin-top", (offsetY - $(el).height() - 5) + "px");
      if (isLeft) {
         $(el).css("margin-left", (offsetX - $(el).width() + 30) + "px");
      }
      else {
         $(el).css("margin-left", (offsetX + $(el).parent().width() - 30) + "px");
      }
   })
}


function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
}


function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

