$(function () {
    /* Tabs */
    $(".tabs").tabs("div.panes > div");
    $(".tabs2").tabs("div.panes2 > div");

    /* cycle */
    $('.banner-1').cycle({ timeout: 10000, speed: 2000 })

    /* Mega Box */
    var megaConfig = { /*interval: 100, sensitivity: 4,*/over: addMega, timeout: 100, out: removeMega };
    $("li.mega, .hClinic").hoverIntent(megaConfig);

    function addMega() {
        $(this).find('.sub').show().addClass("hovering");
    }
    function removeMega() {
        $(this).find('.sub').hide().parent('li').removeClass("hovering")
    }

    /* Menu Items */
    $(".info li a:contains('Pharmacy')").parent().remove();
    $(".info li a:contains('Smoking')").parent().remove();
    $(".info li a:contains('ATM')").parent().remove();

    var $bookme = $(".info li a:contains('Booking')").attr('href');
    $(".bookNow").attr('href', $bookme);

    /* Sitemap */
    $("#sitemap a:contains('Insurance')").attr('href', 'InsCompany.aspx?SectionID=5&RefID=61');

    /* Datepicker */
    $('.datepicker1, .datepicker2').datepicker();

    /* Quick guide */
    $('.r-more a').click(function () {
        $(this).each(function (index) {
            $('.bg-opacity').fadeTo('slow', 0.9);
            $('.item-1, .item-2').fadeTo('slow', 0.3);
        });
    });
    /* Video tour */
    /*$(".vidTour a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});*/
    $("a[rel^='prettyPhoto']").prettyPhoto({ theme: 'facebook' });

    /* Tenders */
    var feesWidth = $(".fees").width();
    //read more and cutting text
    $(".readerMore").width(feesWidth + 5);

    //show Auctions Tab first
    var urlSplit = window.location.href.split("&");
    var urlSlice = urlSplit.slice(1);
    var IDOne = "CatID=2";
    var IDTwo = "CatID=2#Auctions";
    if (urlSlice == IDOne || urlSlice == IDTwo) {
        $('.auctions').show().addClass("clearfix");
        $('.tenders').hide().removeClass("clearfix");
        $('.auctionsTab').parent().addClass("active").prev().removeClass();
    }
    //tenders tabs
    $('.tendersAucTab li').find('a').bind('click', function () {
        if (!$(this).parent().hasClass("active")) {
            var className = $(this).attr("class");
            if (className == "auctionsTab") {
                $('.auctions').show().addClass("clearfix");
                $('.tenders').hide().removeClass("clearfix");
                $(this).parent().parent().find(".active").removeClass("active");
                $(this).parent().addClass("active");
                console.log("auctions");
            } else if (className == "tendersTab") {
                $('.tenders').show().addClass("clearfix");
                $('.auctions').hide().removeClass("clearfix");
                $(this).parent().parent().find(".active").removeClass("active");
                $(this).parent().addClass("active");
                console.log("tenders");
            }
        }
    });

    //cut contents in tenders and auctions page
    $('.orderCont').each(function () {
        var orderCont = $(this).text();
        var cuttedCont = orderCont.substr(0, 200);
        if (cuttedCont.length >= 200) {
            $(this).text(cuttedCont + "...");
            $(this).parent().find(".readerMore").click(function () {
                $(this).parent().find(".orderCont").text(orderCont);
                $(this).fadeTo("slow", 0.5).css({ "cursor": "auto" });
            });
        } else {
            $(this).text(cuttedCont);
            $(this).parent().find(".readerMore").hide();
        }
    });

    //Goes Pink Banner
    /*$('.goesPinkBig').delay(2000).slideDown("slow").click(function(){
    var imgBig = $('.goesPinkBig').height();
    //var imgSmall = $('.smallPiece').height();
    if(imgBig >= 378){
    $(this).animate({height:'173px',  opacity: 0}, 1000);
    $('.smallPiece').delay(1000).fadeIn('medium');
    }else{
    $('.smallPiece').delay(1000).fadeOut('medium');
    $(this).animate({height:'378px',  opacity: 1}, 1000);
    }
		
    });*/

    //Toggle in Carrers page
    $('.jobTitle').toggle(function () {
        $(this).addClass('careerArrowDown').next().show('medium');
    }, function () {
        $(this).removeClass('careerArrowDown').next().hide('medium');
    });

    // ************************** Do not change below ********************************************
    if ((typeof Sys !='undefined') && (typeof Sys.WebForms != 'undefined')) {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(ASPNetAjaxEndRequestHandler);
    }
    repeatScriptOnAjax();
    // ************************** Do not change above ********************************************
    initMCenter();
});

function ASPNetAjaxEndRequestHandler(sender, args) {
    if (args.get_error() == undefined) {
        repeatScriptOnAjax();
    }
}
function repeatScriptOnAjax() {
    
	//datepicker settings
	$(".datepickerBday, .datepickerReqDay").datepicker({
		changeMonth: true,
		changeYear: true,
		showButtonPanel: true,
		yearRange: 'c-100:c+nn'
	});

    //error forms 
    $('.cBtn').click(function () {
        $(".error").each(function () {
            if ($(this).is(':hidden')) {
                $(this).parent().parent().parent().css({ "background-color": "#FFFAEE" });
            }
        });
    });

    $('.apointmentForm').find(".error").hover(function () {
        $(this).fadeOut("slow").parent().parent().parent().css({ "background-color": "#FEEBCA" });
    });

    $('.apointmentForm tr').hover(function () {
        if ($(this).find(".error").is(':visible')) {
            $(this).css({ "background-color": "#FEEBCA" });
            $(this).find(".error").fadeOut("medium");
        }
    });


	$(".mCenter").change(function(){
	   var currentValue =  $(this).val();
	   $(".speciality").find("option").each(function(){
			if(currentValue.toUpperCase() == $(this).val()){
				$(this).show().removeAttr("disabled","disabled").attr("enabled","enabled");
			}else{
				$(this).css('color','#030303').hide().attr("enabled","enabled").attr("disabled","disabled");
			}
		});
	});
	
	
	
    $(".tenderType").change(initTAFilter);
}

/*function initMCenter() {
    var mcID = $(this).val();
    $(".speciality > option").each(
                function (index) {
                    if (index == 0)
                        $(this).attr("selected", "selected");
                    if (this.value != "00000000-0000-0000-0000-000000000000") {
                        if (this.value.toLowerCase() == mcID)
                            $(this).show();
                        else
                            $(this).hide();
                    }
                });

}*/

function initTAFilter() {
    var selectedID = $(this).val();
    $(".tenderPost, .auctionsPost").each(function (index) {
        if (selectedID != "0") {
            if ($(this).hasClass("type" + selectedID))
                $(this).show();
            else
                $(this).hide();
        }
        else
            $(this).show();
    });
}
