var fade_time = 1000;
var text_show_time = 5000;
var total_time = text_show_time+fade_time;

$(function(){

    $( "input:submit" ).button();
    $( "input:button" ).button();

    
    $('.inputinputsearch').val("  Search")
    //focus event on username
    $('.inputinputsearch').focus(function() {
        if($.trim($('.inputinputsearch').val()).toLowerCase() == "search") {
            $('.inputinputsearch').val("");
        }
    });
   //set blur event on username
    $('.inputinputsearch').blur(function() {
        if($('.inputinputsearch').val() == "") {
            $('.inputinputsearch').val("  Search");
        }
    });

    //remove border bottom from last child element
    $('.moduletable_menu .menu li ul li:last-child').css("border","none");

    //special function to top menu about us
    $('.moduletable_menu .menu .item2 ul li').hover(function() {
      //$('.moduletable_menu .menu li').css('background-color','#d8d9da');
      //$(this).parent().parent().css('background-color','#d8d9da');
      $(this).parent().parent().css('border-top-left-radius','10px');
      $(this).parent().parent().css('-moz-border-radius-topleft','10px');
      $(this).parent().parent().css('-webkit-border-top-left-radius','10px');
      //$(this).parent().parent().css('border','solid');

      //alert($(this));
   }, function()
   {
      $(this).parent().parent().css('border-top-left-radius','0px');
      $(this).parent().parent().css('-moz-border-radius-topleft','0px');
      $(this).parent().parent().css('-webkit-border-top-left-radius','0px');

      //alert("out");
   });

    if(navigator.userAgent.indexOf('MSIE 7') < 0) {
       //use normal background
        //$('.header .moduletable_menu .menu ').css('background','#FFF url(../templates/soc3/images/menuBg.png) no-repeat bottom left');
        //special function to top menu
        $('.header .moduletable_menu .menu li ul li').hover(function() {
          //$('.moduletable_menu .menu li').css('background-color','#d8d9da');
          $(this).parent().parent().css('background-color','#d8d9da');

          //alert($(this).parent().parent());
       }, function()
       {
          $(this).parent().parent().css('background','none');
          //alert("out");
       });
       
    }else {
        //$('.header .moduletable_menu .menu ').css('background','#FFF url(../templates/soc3/images/menuBgie7.png) no-repeat bottom left');
    }

    //hide video players in divs
    $('.videoplayer-common').hide();


    $("#base").hide();
    $("#text").hide();
    $("#tv").hide();
    $("#scrolling").hide();
    $("#scrolling_glow").hide();
    $("#scrolling_text").hide();
    $("#scrolling_text_right").hide();    
    $("#rotating_image").hide();
    $("#slideshow_logo").hide();

    $("#loading-image").fadeIn(fade_time);
    $("#loading-text").fadeIn(fade_time);


    //preload the content
    preloadAnimationContent();
    
    //if different of explorer, normal loading
    if(navigator.userAgent.indexOf('MSIE') < 0) {
    
        $.preload([
                    '../templates/soc3/images/logo.png',
                    '../templates/soc3/images/slideshow_img1.png',
                    '../templates/soc3/images/1_soc_mood.png',
                    '../templates/soc3/images/2_soc_mood.png',
                    '../templates/soc3/images/3_soc_mood.png',
                    '../templates/soc3/images/4_soc_mood.png',
                    '../templates/soc3/images/slideshow_img4.png',
                    '../templates/soc3/images/slideshow_tv_shadow.png',
                    '../templates/soc3/images/slideshow_tv1.png',
                    '../templates/soc3/images/slideshow_tv2.png',
                    '../templates/soc3/images/slideshow_tv3.png',
                    '../templates/soc3/images/slideshow_tv4.png',
                    '../templates/soc3/images/slideshow_scrolling_glow.png',
                    '../templates/soc3/images/slideshow_scrolling_right.png'
            ], {
                    init: function(loaded, total) {
                            $("#loading-text").html("Loading "+parseInt(loaded/total*100)+"%");
                    },
                    loaded: function(img, loaded, total) {
                            $("#loading-text").html("Loading "+parseInt(loaded/total*100)+"%");
                            //$("#full-screen").append(img);
                    },
                    loaded_all: function(loaded, total) {
                            $("#loading-text").html("Loading "+parseInt(loaded/total*100)+"%");
                            $("#loading-image").fadeOut(fade_time);
                            $("#loading-text").fadeOut(fade_time);

                            //start the animation
                            $("#base").show();
                            $("#text").show();

                            //After load the images and the content, start the first animation
                            //and the scroller
                            simulateLoading();
                    }
            });
    }else {
        //simulate loading, give extra seconds to load all the images
        setTimeout(simulateLoading,text_show_time);

    }
    
    
   
});


function preloadAnimationContent() {
    //load all backgrounds

    $(".bg").css("display","none");
    //load all tv images

    $(".tv-image").css("display","none");
    //load all text
    //show text
    //if using browser different from internet explorer
    //use text

    $("#text").css("display","none");
    if(navigator.userAgent.indexOf('MSIE') < 0) {
        $("#text").html("");
        $("#text").append("<div id='text-animation0' class='text-animation'>"+
                        "CHANGING EMERGENCY <br/>"+
                        "MEDICINE &#150; ENDING THE <br/>"+
                        "STRUGGLE TO FIND <br/>"+
                        "EFFECTIVE ON-CALL <br/>"+
                        "COVERAGE SOLUTIONS</div>");
        $("#text-animation0").css({"left":"220px"});
        $("#text").append("<div id='text-animation1' class='text-animation'>"+
                        "EXCEPTIONAL, AFFORDABLE ON-CALL <br/>"+
                        "COVERAGE FROM BOARD CERTIFIED <br/>"+
                        "SPECIALISTS DAY AND NIGHT</div>");
        $("#text-animation1").css({"left":"100px","color":"#86922e"});
        $("#text").append("<div id='text-animation2' class='text-animation'>"+
                        "UNMATCHED CLINICAL AND <br/>"+
                        "FINANCIAL OUTCOMES FOR SUBURBAN, <br/>"+
                        "URBAN AND RURAL HOSPITALS</div>");
        $("#text-animation2").css({"left":"80px","color":"#002c77"});
        $("#text").append("<div id='text-animation3' class='text-animation'>"+
                        "NATION'S LEADING PROVIDER OF<br/>"+
                        "EMERGENCY SPECIALTY<br/>"+
                        "CONSULTATIONS &ndash; OVER 30,000<br/>"+
						"CONSULTATIONS DELIVERED,<br/>"+
                        "COUNTLESS LIVES IMPACTED</div>");
        $("#text-animation3").css({"left":"130px","color":"#869338"});
        $(".text-animation").css("display","none");

    }else {
        //if using internet explorer
        $("#text-animation0").css({"left":"220px"});
        $("#text-animation1").css({"left":"100px","color":"#86922e"});
        $("#text-animation2").css({"left":"80px","color":"#002c77"});
        $("#text-animation3").css({"left":"130px","color":"#869338"});
        $(".text-animation").css("display","none");
    }



}

function simulateLoading() {
    $("#loading-image").fadeOut(fade_time);
    $("#loading-text").fadeOut(fade_time);

    //start the animation
    $("#base").show();
    $("#text").show();

    //After load the images and the content, start the first animation
    //and the scroller
    //start animation
    startAnimation1();
    startAnimation2();
    startAnimation3();
    startAnimation4();
    //start the scroll with a little time difference
    setTimeout(startScroller,fade_time*2);	
}


function startAnimation1() {
    //animation 1

    //show background image
   
    $("#baseimg0").fadeIn(fade_time);
    $('#text').show();

    //show tv base image
    $("#scrolling_glow").show();
    //$("#tvimg0").delay(fade_time).fadeIn(fade_time);
    $("#scrolling").delay(fade_time).fadeIn(fade_time/2);
    $("#scrolling_text").delay(fade_time*2).fadeIn(fade_time/2);
    $("#scrolling_text_right").delay(fade_time).fadeIn(fade_time/2);
    
    $("#tv").delay(fade_time).fadeIn(fade_time);
    $("#rotating_image").show();
    $("#tvimg0").delay(fade_time*2).fadeIn(fade_time);

    //$("#text-animation0").delay(fade_time*2).effect("scale",fade_time);
    $("#text-animation0").delay(fade_time*2).fadeIn(fade_time*2);
    //hide text
    //$("#text-animation0").delay(text_show_time).fadeOut(fade_time);
    $("#text-animation0").delay(text_show_time-fade_time).effect("puff",fade_time/3);
    //$("#text").delay(text_show_time).fadeOut(fade_time);
    //hide tv image
    $("#tvimg0").delay(text_show_time).fadeOut(fade_time);
    //hide background image
    //$("#baseimg0").delay(total_time+fade_time*2).fadeOut(fade_time);

    //end of animation 1
    //begin of animation 2
    //console.log("fin 1");
    //setTimeout(startAnimation2,total_time*1.5);
    
}

function startAnimation2() {
    //console.log("inicio 2");
    //animation 2
    //show background image
    //animate several backgrounds
    //base like water animation
    $("#baseimg10").delay(total_time+fade_time*2).fadeIn(fade_time);
    $("#baseimg11").delay(total_time+fade_time*4).fadeIn(fade_time);
    $("#baseimg12").delay(total_time+fade_time*6).fadeIn(fade_time);
    $("#baseimg13").delay(total_time+fade_time*8).fadeIn(fade_time);

    //base like water animation
    $("#baseimg13").delay(total_time).fadeOut(fade_time);
    $("#baseimg12").delay(total_time+fade_time*2).fadeOut(fade_time);
    $("#baseimg11").delay(total_time+fade_time*4).fadeOut(fade_time);
    

    //show logo
    $("#slideshow_logo").delay(total_time+fade_time*2).fadeIn(fade_time);
    //show tv image 2
    //$("#rotating_image").html("<img src='templates/soc3/images/slideshow_tv2.png' alt='Slideshow image' />");
    $("#tvimg1").delay(total_time+fade_time*3).fadeIn(fade_time);
    //show text
    //change text position
    $("#text-animation1").delay(total_time+fade_time*3).fadeIn(fade_time*2);
    //hide text
    //$("#text-animation1").delay(total_time+fade_time*2+text_show_time).effect("puff",{},fade_time/2);
    //$("#text-animation1").delay(text_show_time).fadeOut(fade_time);
    $("#text-animation1").delay(text_show_time-fade_time).effect('puff',fade_time/3);
    //hide tv image
    $("#tvimg1").delay(text_show_time).fadeOut(fade_time);
    //hide background image
    //$("#base").delay(total_time+fade_time*2).fadeOut(fade_time);

    //end of animation 2
    //begin of animation 3
    //console.log("fin 2");
    //setTimeout(startAnimation3,total_time*1.6);
    
}

function startAnimation3() {
    //console.log("inicio 3");
    //animation 3

    //show tv image 3

    $("#tvimg2").delay(total_time*2.15+fade_time*4).fadeIn(fade_time);
    //show text
    //change text position
    $("#text-animation2").delay(total_time*2.15+fade_time*4).fadeIn(fade_time*2);
    //hide text
    //$("#text-animation2").delay(total_time*2+text_show_time).effect("puff",{},fade_time/2);
    //$("#text-animation2").delay(text_show_time).fadeOut(fade_time);
    $("#text-animation2").delay(text_show_time).effect('puff',fade_time/3);
    //hide tv image
    $("#tvimg2").delay(text_show_time+fade_time).fadeOut(fade_time);
    //hide logo
    //$("#slideshow_logo").delay(total_time*4 +fade_time*2+text_show_time).fadeOut(fade_time);

    //hide background image
    //$("#baseimg13").delay(total_time*3+text_show_time+fade_time).fadeOut(fade_time);

    //end of animation 3
    //begin of animation 4
    //setTimeout(startAnimation4,total_time*3.6/2);
    
}

function startAnimation4() {
    //animation 4
    //show background image
    //hide background image
    $("#baseimg3").delay(total_time*3+text_show_time+fade_time).fadeIn(fade_time);
    //show logo
    //$("#slideshow_logo").delay(total_time*4+fade_time*2+text_show_time).fadeIn(fade_time);


    //show tv image 4
    $("#tvimg3").delay(total_time*3.7+fade_time*3).fadeIn(fade_time);
    //show text
    //change text position
    $("#text-animation3").delay(total_time*3.7+fade_time*3).fadeIn(fade_time*2);

    //hide text
    //$("#text").delay(text_show_time).effect("puff",{},fade_time/2);

    //hide tv image
    //$("#rotating_image").delay(text_show_time).fadeOut(fade_time);
    //hide background image
    //$("#base").delay(total_time+fade_time*2).fadeOut(fade_time);
    
}

function startScroller () {
      // Add Scroller Object
      $jScroller.add("#scroller_container","#scroller","left",5);

      // Start Autoscroller
      $jScroller.start();
}

function hideBlog() {
	//$('.moduletable_menu .menu .item11').css("display","none");
}

function openContactus(index){
    $("#overlay-contactus"+index).dialog({width: 750});
//    //alert("Hola");
}

function openVideo(index){
    //on x add 27px more for the modal margins
    //on y add 43px more for the modal margins
    //to place the video at 450x300px
    $('#video'+index).dialog({width: 477,height:343});
}

