// settings
var ROOT_URL = 'http://www.medlife.ro/';
var AJAX_URL = ROOT_URL + 'app/controllers/ajax.php';

function parseLinks() {
    var as = document.getElementsByTagName('a');
    var as_no = as.length;
    if (as_no == 0) return false;
    for (var i = 0; i < as_no; i++) {
        if (as[i].rel == 'external') as[i].target = "_blank";
    }
}

function initFeatures() {
    $('#features li').each(
        function() {
            $(this).hover(
                function () {
                    $(this).addClass('hovered');
                    if ($.browser.msie && $.browser.version == "6.0") {
                        $('.container').ifixpng();    
                    }
                    
                },
                function () {
                    $(this).removeClass('hovered');
                }
            );  
            $(this).click(
                function() {
                    var link = $(this).children('.container').children('.heading').children('a').attr('href');
                    window.location = link;
                }
            );
        }
    );
}

function initTours() {
   var so = new SWFObject(ROOT_URL + "resources/swf/pano1.swf", "cpano1", "450", "340", "8");
   so.write("pano1");
   
   var so = new SWFObject(ROOT_URL + "resources/swf/pano2.swf", "cpano2", "450", "340", "8");
   so.write("pano2");
   
   var so = new SWFObject(ROOT_URL + "resources/swf/pano3.swf", "cpano3", "450", "340", "8");
   so.write("pano3");
   
   var so = new SWFObject(ROOT_URL + "resources/swf/pano4.swf", "cpano4", "450", "340", "8", "#336699");
   so.write("pano4");
} 


function initHeaders() {
	
	/*var head1 = new SWFObject(ROOT_URL + "resources/swf/headers/1.swf", "headerAbout", "987", "108");
	head1.write("#headAbout");*/

    $('#headAbout').flash({
        src: ROOT_URL + 'resources/swf/headers/1.swf',
        width: 986,
        height: 108,
        wmode: "opaque"
    });
    
    $('#headCenters').flash({
        src: ROOT_URL + 'resources/swf/headers/2.swf',
        width: 986,
        height: 108,
        wmode: "opaque"
    });
    
    $('#headHospital').flash({
        src: ROOT_URL + 'resources/swf/headers/3.swf',
        width: 986,
        height: 108,
        wmode: "opaque"
    });
    
    $('#headLabs').flash({
        src: ROOT_URL + 'resources/swf/headers/4.swf',
        width: 986,
        height: 108,
        wmode: "opaque"
    });
    
    $('#headPeople').flash({
        src: ROOT_URL + 'resources/swf/headers/5.swf',
        width: 986,
        height: 108,
        wmode: "opaque"
    });
    
    $('#headCompanies').flash({
        src: ROOT_URL + 'resources/swf/headers/7.swf',
        width: 986,
        height: 108,
        wmode: "opaque"
    });
    
    $('#headPress').flash({
        src: ROOT_URL + 'resources/swf/headers/6.swf',
        width: 986,
        height: 108,
        wmode: "opaque"
    });
    
    $('#headGuide').flash({
        src: ROOT_URL + 'resources/swf/headers/8.swf',
        width: 986,
        height: 108,
        wmode: "opaque"
    });
}

function initMaps() {
    $('#map-grivita').flash({
        src: ROOT_URL + 'resources/swf/maps/grivita.swf',
        width: 809,
        height: 800
    });
    $('#map-basarab').flash({
        src: ROOT_URL + 'resources/swf/maps/basarab.swf',
        width: 809,
        height: 800
    });
    var map = $('#location-map').attr('class');  
    $('#location-map').flash({
        src: ROOT_URL + 'resources/swf/maps/'+map+'.swf',
        width: 809,
        height: 800
    });
    
    $('#map-favorit').flash({
        src: ROOT_URL + 'resources/swf/maps/favorit.swf',
        width: 809,
        height: 800
    });
    
    $('#map-unirii').flash({
        src: ROOT_URL + 'resources/swf/maps/unirii.swf',
        width: 809,
        height: 800
    });
    
    $('#map-mindcare').flash({
        src: ROOT_URL + 'resources/swf/maps/mindcare.swf',
        width: 809,
        height: 800
    });
    
    $('#map-garadenord').flash({
        src: ROOT_URL + 'resources/swf/maps/gara_de_nord.swf',
        width: 809,
        height: 800
    });
    
    $('#map-cantacuzino').flash({
        src: ROOT_URL + 'resources/swf/maps/cantacuzino.swf',
        width: 809,
        height: 800
    });
    
    $('#map-budimex').flash({
        src: ROOT_URL + 'resources/swf/maps/budimex.swf',
        width: 809,
        height: 800
    });
    
    $('#map-cluj').flash({
        src: ROOT_URL + 'resources/swf/maps/cluj.swf',
        width: 809,
        height: 800
    });
	$('#map-batistei').flash({
        src: ROOT_URL + 'resources/swf/maps/batistei.swf',
        width: 809,
        height: 800
    });
    $('#map-timisoara').flash({
        src: ROOT_URL + 'resources/swf/maps/timisoara.swf',
        width: 809,
        height: 800
    });
    $('#map-timisoara2').flash({
        src: ROOT_URL + 'resources/swf/maps/timisoara2.swf',
        width: 809,
        height: 800
    });
    $('#map-baneasa').flash({
        src: ROOT_URL + 'resources/swf/maps/baneasa.swf',
        width: 809,
        height: 800
    });
    $('#map-gastro').flash({
        src: ROOT_URL + 'resources/swf/maps/gastro.swf',
        width: 809,
        height: 800
    });
    $('#map-procto').flash({
        src: ROOT_URL + 'resources/swf/maps/procto.swf',
        width: 809,
        height: 800
    });
    $('#map-oste').flash({
        src: ROOT_URL + 'resources/swf/maps/oste.swf',
        width: 809,
        height: 800
    });
    $('#map-materno').flash({
        src: ROOT_URL + 'resources/swf/maps/materno_fetala.swf',
        width: 809,
        height: 800
    });
    $('#map-fizioterapie').flash({
        src: ROOT_URL + 'resources/swf/maps/fizioterapie.swf',
        width: 809,
        height: 800
    });
    $('#map-hospital').flash({
        src: ROOT_URL + 'resources/swf/maps/hospital.swf',
        width: 809,
        height: 800
    });
    $('#map-grivita2').flash({
        src: ROOT_URL + 'resources/swf/maps/grivita2.swf',
        width: 809,
        height: 800
    });
    
}

function initBody() {
   var so = new SWFObject(ROOT_URL + "resources/swf/body/body.swf", "human", "940", "500", "8", "#336699");
   so.addParam("wmode", "transparent");
   so.write("container");    
} 

function initNav() {
    $('#nav li').each(
        function() {
            if ($(this).hasClass('selected')) {
                $(this).prev().addClass('prev');
            }
                //$('#nav li').removeClass('selected');
                //$(this).addClass('selected');
        }
    );
}

function toggleAb(no) {
    if ($('#abtable'+no).hasClass('open')) {
        $('#abtable'+no).children('tbody').slideUp(function(){
            $('#abtable'+no).removeClass('open');
           // $("a.switch").html("inchide");
            $("#abtable"+no+" a.switch").html("extinde");
        });
    } else {
        $('tbody').slideUp();
        $('.open').removeClass('open');
        $('#abtable'+no).children('tbody').slideDown(function(){
            $('#abtable'+no).addClass('open');
            //$('#abtable'+no+" tbody tr td:not('.section')").css("border-right","1px solid #CDEDFC");        
            //$("a.switch").html("extinde");
            $("#abtable"+no+" a.switch").html("inchide");             
        })
        //document.location = '#abtable'+no;
    }
    return false;
}

function toggleAbS(no) {
    if ($('#abstandard'+no).hasClass('open')) {
        $('#abstandard'+no).children('tbody').slideUp(function(){
            $('#abstandard'+no).removeClass('open');
           // $("a.switch").html("inchide");
            $("#abstandard"+no+" a.switch").html("extinde");
        });
    } else {
        $('tbody').slideUp();
        $('.open').removeClass('open');
        $('#abstandard'+no).children('tbody').slideDown(function(){
            $('#abstandard'+no).addClass('open');
            //$('#abtable'+no+" tbody tr td:not('.section')").css("border-right","1px solid #CDEDFC");        
            //$("a.switch").html("extinde");
            $("#abstandard"+no+" a.switch").html("inchide");             
        })
        //document.location = '#abtable'+no;
    }
    return false;
}

function toggleDoctor(id) {
    if ($('#doctor'+id).hasClass('selected')) {
        $('#doctor'+id).removeClass('selected');
        $('#dr'+id).slideUp();
        $('#link'+id).removeClass('active');
    } else {
        $('table.doctors ul.doctor-details').slideUp();
        $('table.doctors .active').removeClass('active');
        $('table.doctors .selected').removeClass('selected');
        $('#doctor'+id).addClass('selected');
        $('#dr'+id).slideDown();
        $('#link'+id).addClass('active');
    }
    return false;
}

function toggleAnalize(id) {
    if ($('#doctor'+id).hasClass('selected1')) {
        $('#doctor'+id).removeClass('selected1');
        $('#anl'+id).slideUp();
        $('#link1'+id).removeClass('active1');
    } else {
        $('table.doctors ul.analize-details').slideUp();
        $('table.doctors .active1').removeClass('active1');
        $('table.doctors .selected1').removeClass('selected1');
        $('#doctor'+id).addClass('selected1');
        $('#anl'+id).slideDown();
        $('#link1'+id).addClass('active1');
    }
    return false;
}

function toggleDep(id) {
    var obj = $('#table'+id);
    var parent = obj.parent();
    if (parent.hasClass('open')) {
        obj.slideUp(function(){
            //obj.css({'display':'none'});
            parent.removeClass('open');
            parent.children('div.head').children('a.switch').html('extinde');            
        });
    } else {
        $('li.open table.body').slideUp(function(){
            $('li.open').removeClass('open');
        })
            obj.slideDown(function(){
                parent.children('div.head').children('a.switch').html('inchide');            
                parent.addClass('open');
            });
    }
    return false;
}

function getDoctors() {
    var clinic_id = $('#clinic_id').attr('value');
    var section_id = $('#section').attr('value');
    $.ajax({
        url: AJAX_URL,
        type: 'post',
        data: 'action=getdoc&clinic='+clinic_id+'&section='+section_id,
        success: function(transport) {
            transport = '<option></option>' + transport;
            $('#doctor').html(transport);
            $('#doctorc').fadeIn();
        }
    })
}

function getServices() {
    var doctor_id = $('#doctor').attr('value');
    $.ajax({
        url: AJAX_URL,
        type: 'post',
        data: 'action=getservices&doctor='+doctor_id,
        success: function(transport) {
            transport = '<option></option>' + transport;
            $('#service').html(transport);
            $('#servicesc').fadeIn();
        }
    })
}

function getDates() {
    var doctor_id = $('#doctor').attr('value');
    $.ajax({
        url: AJAX_URL,
        type: 'post',
        data: 'action=getdates&doctor='+doctor_id,
        success: function(transport) {
            transport = '<option></option>' + transport;
            $('#date').html(transport);
            $('#datesc').fadeIn();
        }
    })
}

function getSubmit() {
    $('#submitc').fadeIn();
}

function getSchedule() {
    var date = $('#date').attr('value');
    var doctor_id = $('#doctor').attr('value');
    $.ajax({
        url: AJAX_URL,
        type: 'post',
        data: 'action=schedule&date='+date+'&doctor='+doctor_id,
        success: function(transport) {
        }
    })
}

function getHour() {
    var date = $('#date').attr('value');
    var doctor_id = $('#doctor').attr('value');

    $.ajax({
        url: ROOT_URL+'index.php?controller=centers&action=gethours',
        type: 'post',
        data: 'action=gethours&date='+date+'&doctor='+doctor_id,
        success: function(transport) {
            transport = '<option></option>'+transport;
            $('#hour').html(transport);
            $('#hourc').fadeIn();
        }
    })
}


function getServicesLoc() {
    var clinic = $('#clinic').attr('value');
    $.ajax({
        url: AJAX_URL,
        type: 'post',
        data: 'action=servicesLoc&clinic='+clinic,
        success: function(transport) {
            transport = '<option></option>' + transport;
            $('#service').html(transport);
            $('#servicesc').fadeIn();
        }
    })
}

function initDoctor() {
    $('td.name .container').hover(
        function() {
            $(this).children('.overlayer').fadeIn('fast');
        },
        function() {
            $(this).children('.overlayer').fadeOut('fast');
        }
    );
}

function initSlider() {
    $('#sidebar #sections li').each(function() {
        $(this).hover(
            function() {
                var src = $(this).children().children('.logo').attr('src').replace('2.jpg','.jpg');
                $(this).children().children('img').attr('src',src);
                var extra = $(this).children('.extra');
                extra.slideDown('fast');
            },
            function() {
                var src = $(this).children().children('.logo').attr('src').replace('.jpg','2.jpg');
                $(this).children().children('img').attr('src',src)
                var extra = $(this).children('.extra');
                extra.slideUp('fast');
            }
        );
    });
}

function jumpTo(element) {
    $(document).scrollTo( '#'+element, 500 );
    return false;
}

function initGallery() {
    var container = $('#main-visual');
    $('ul.pics li a img').each(function() {
        $(this).click(function(){ 
        $('#tourc').fadeOut();
        var src = $(this).attr('src');
            $('.main-visual').fadeOut(function(){
                container.attr('src',src.replace('thumbs/',''));
                $('.main-visual').fadeIn();
            });
        });
    });
}

function toggleTour(tour) {
   var so = new SWFObject(ROOT_URL + "resources/swf/pano"+tour+".swf", "cpano1", "450", "340", "8");
   so.write("tour");
} 

function toggleMap(map) {
    var map = new SWFObject(ROOT_URL + "resources/swf/maps/" + map+".swf", "medlifeMap", "800", "800", "8");
    map.addParam("wmode", "transparent");
    map.write("mapcontainer");
}

function toggleBodyServices() {
        $(".wizard-section:not('.first')").hide();
        $(".servicebox").fadeIn();
}

function toggleBodySections() {
        $(".wizard-section").css({'display':'block'});
        $(".servicebox").css({'display':'none'});
}


// init
$(document).ready(function() {
    
       
    var context = $('body').attr('id');	
    if (context == 'clinic') {
        initDoctor();
        //initGallery();
    }
    //swf's
    
    //header phone
    var map = new SWFObject(ROOT_URL + "resources/swf/tel.swf", "medLifePhone", "206", "52", "8");
    map.addParam("wmode", "transparent");
    map.write("motto");
    
   /* $('#motto').flash({
        src: ROOT_URL + 'resources/swf/tel.swf',
        width: 206,
        height: 52,
        wmode: 'transparent'
    });*/
    
    
    //medlife slider hp
    $('#medlife-slider').flash({
        src: ROOT_URL + 'resources/swf/slider2/slider.swf',
        width: 1019,
        height: 191,
        wmode: 'transparent'
    });
    
    //medlife banner hp & promotions
    /*
    $('#medlife-banner').flash({
        src: ROOT_URL + 'resources/swf/medlife-banner.swf',
        width: 350,
        height: 165
    });
    */
    
    //ie7 hp tab fix & other fixes
    if ($.browser.msie && $.browser.version=="7.0") {
        $("#page #head #features").css("position","relative");
        $("#page #head #features").css("top","189px");
        // ---
        $(".main-visual").css("padding-right","20px");
        $("#page #main table.doctors").css("border","1px solid #CDEDFC !important");
        $("table.doctors tr td").css("background-color","#000");
    }
    
    //maps
    initMaps();
    //activate map lightbox
    $("a.maps").fancybox({
        'hideOnContentClick': false,
        'zoomOpacity': true,
        'zoomSpeedIn': 600,
        'zoomSpeedOut': 500,
        'easingIn': 'easeInExpo',
        'easingOut':'easeInBack',
        'centerOnScroll': false,
        'overlayShow': true,
        'overlayColor': 123,
        'overlayOpacity': 0.9,
        'frameWidth': 809,
        'frameHeight': 800
    });

    
    $("a.location-map").fancybox({
        'hideOnContentClick': false,
        'zoomOpacity': true,

        'centerOnScroll': false,
        'overlayShow': true,
        'overlayColor': 123,
        'overlayOpacity': 0.9,
        'frameWidth': 809,
        'frameHeight': 800
    });

    //timisoara
    $("a.map-timisoara").fancybox({
        'hideOnContentClick': false,
        'zoomOpacity': true,
        'zoomSpeedIn': 600,
        'zoomSpeedOut': 500,
        'easingIn': 'easeInExpo',
        'easingOut':'easeInBack',
        'centerOnScroll': false,
        'overlayShow': true,
        'overlayColor': 123,
        'overlayOpacity': 0.9,
        'frameWidth': 1046,
        'frameHeight': 839
    });
    initHeaders();
    initNav();
    initFeatures();
    parseLinks();
    initSlider();
 
    
    $('#secondary ul.submenu li').each(function(){
        $(this).hover(
            function(){
                $(this).addClass('hovered');
            },
            function(){
                $(this).removeClass('hovered');
            }
        );
    });
    
    if (context == 'homepage') {
        //$('ul.news').marquee();
        //$('marquee').marquee();
        /*
        $('marquee').marquee('pointer').mouseover(function () {
          $(this).trigger('stop');
        }).mouseout(function () {
          $(this).trigger('start');
        }).mousemove(function (event) {
          if ($(this).data('drag') == true) {
            this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
          }
        }).mousedown(function (event) {
          $(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
        }).mouseup(function () {
          $(this).data('drag', false);
        });
        */
    }
    
    if ((context == 'homepage')||(context == 'promotions')) {
        var b = new SWFObject(ROOT_URL + "resources/swf/medlife-banner.swf", "medLifePhone", "350", "165", "8");
        b.addVariable("url",ROOT_URL + "promotii/");
        b.write("medlife-banner");
    }
    
    $("a.iframe").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 800, 'overlayShow': true, 'overlayOpacity':0.9 ,'frameWidth':830, 'frameHeight':800 }); 
    $("body#clinic a.iframe").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 800, 'overlayShow': true, 'overlayOpacity':0.9 ,'frameWidth':450, 'frameHeight':350 }); 
    $("a.flashmap").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 800, 'overlayShow': false, 'frameWidth':800, 'frameHeight':850 }); 
    if ($('#date').length != 0)
    $("#date").datepicker({dateFormat: 'dd-mm-yy',dayNamesMin: ['D', 'L', 'M', 'M', 'J', 'V', 'S'],firstDay: 1,monthNames: ['Ianuarie','Februarie','Martie','Aprilie','Mai','Iunie','Iulie','August','Septembrie','Octombrie','Noiembrie','Decembrie'], minDate: 'Today'+1, maxDate: 'Today'+30});
	
    if ($('#inputDate').length != 0)
    $("#inputDate").datepicker({dateFormat: 'dd/mm/yy',dayNamesMin: ['D', 'L', 'M', 'M', 'J', 'V', 'S'],firstDay: 1,monthNames: ['Ianuarie','Februarie','Martie','Aprilie','Mai','Iunie','Iulie','August','Septembrie','Octombrie','Noiembrie','Decembrie']});
	
    if ($('#inputDateEnd').length != 0)
    $("#inputDateEnd").datepicker({dateFormat: 'dd/mm/yy',dayNamesMin: ['D', 'L', 'M', 'M', 'J', 'V', 'S'],firstDay: 1,monthNames: ['Ianuarie','Februarie','Martie','Aprilie','Mai','Iunie','Iulie','August','Septembrie','Octombrie','Noiembrie','Decembrie']});
	
    
    $("a[rel=image]").fancybox({
        'zoomOpacity': true,
        'zoomSpeedIn'        : 600,
        'zoomSpeedOut'        : 500,
        'easingIn'            : 'easeInExpo',
        'easingOut'            :'easeInBack',
        'centerOnScroll'    : true,
        'overlayShow'        : true,
        'overlayColor'      : 123,
        'overlayOpacity': 0.9
    });

    //satisfaction form star rating
    var bodyClass = $("body").attr("class");
    var myBodyClass = bodyClass.split(" ");
    if (myBodyClass[1]=="satisfaction") {
        $("#satisfactionForm fieldset .element").each(function(i) { 
            $(this).find(".rating-star").rating({ 
                focus: function(value, link) { 
                    var tip = $('body.satisfaction .input-title');
                    tip[0].data = tip[0].data || tip.html(); 
                    tip.eq(i).html(link.title || 'value: '+value); 
                }, 
                blur: function(value, link) { 
                    var tip = $('body.satisfaction .input-title'); 
                    tip.html("");
                }
            });
            
        });
        $("div.rating-cancel").remove();
    }
    
    
    $("body#companies #page #main #primary table td:not('.section'),body#labs #page #main #primary table.doctors-new td:not('.section')").css("border-right","1px solid #CDEDFC");
    
    //pngFix
    if ($.browser.msie && $.browser.version == "6.0") {
        $.ifixpng('/resources/img/bullets/pixel.gif');
        $("#logo, #head #features li .heading, body#about.corporate #page #main #primary div.articles ul.enum li .visual, body.corporate #page #main #primary, body.corporate #page #main #sidebar, #homepage #main .box, #SiteContainer #Content").ifixpng();
        //$("#motto").pngFix();
        DD_belatedPNG.fix('.overlayer');
        DD_belatedPNG.fix('#head #features li');
        $("table.doctors").css({"float":"left","display":"table"});
    }
    
    if ($.browser.mozilla) {
        $("#page #main table.doctors td").css("border-top","none");
    }
    
    //webkit & ie7,8
    if ($.browser.safari || ($.browser.msie && ($.browser.version == "7.0" || $.browser.version == "8.0"))) {
        $("body#companies.subscriptions #page #main #primary table thead tr td, body#companies.subscriptions #page #main #primary table thead tr").css("height","28px");
    }
    
    
    $("body#body #page #main .wide .wizard .box .section-body .services-list").each(function(i) {
        if ($("body#body #page #main .wide .wizard .box .section-body .services-list:eq("+i+") ul li").length<13) {
            $("body#body #page #main .wide .wizard .box .section-body .services-list:eq("+i+")").addClass("fit-height");
        }
    });

    
    //sidebar icons onClick handle
	$("#page #main #sidebar div.section").each(function(i) {
		$(this).click(function(e) {
			var link = $(this).children(".section-holder").children(".section-body").children(".more").children("a").attr("href");
			window.location = link;
			e.preventDefault();
		});
	});

	$('.medlife-centers ul li').each(function(){
		$(this).hover(
			function(){
			$(this).addClass('small');
			},
			function(){
			$(this).removeClass('small');
			}
		);
	});
    
    $('.clinic-services ul li').each(function(){
        $(this).hover(
            function(){
            $(this).addClass('small');
            },
            function(){
            $(this).removeClass('small');
            }
        );
    });
	

	$("body:not('#homepage') #head .flash-replaced").css({"position":"absolute","top":"155px","left":"0px"});
});
