$(document).ready(function(){




   // box ticker

   var _off = $("#frontpageItems").offset();
   var _fromLeft = _off.left+'px';
   var _fromTop  = _off.top+'px';

   var numFrontpageItems = 0;
   var z = 6;

   $("#frontpageItems").children(".frontpageItem").each(function(i){
     $(this).css('z-index', (z-i) );
     $(this).css('left', _fromLeft );
     $(this).css('top', _fromTop );
     numFrontpageItems = numFrontpageItems+1;
     //$(this).children(".promo > .more").css('left', _fromLeft);
   });



   /* GLOBALS */
   var row2_height = 0;

  /* SIFR */
   // siirretty sifr config fileen

   /* etusivun news ticker */
   $("#ticker").newsticker();
   var temp1 = $(".ticker_title");
   temp1.not(":eq(0)").hide();
   //$(".ticker_title:not(eq(0))").hide();


   // dialogup
   $(".openDialogUp").click(function(){
     $(".dialog").hide();
     var _offset = $(this).offset();
     var atop = parseInt(_offset.top);
     var aleft = parseInt(_offset.left);

     var _target = $(this).attr('href');
     $(_target).show();
     var h = $(_target).height();

     var ypos = atop-h-10 + 'px';
     $(_target).css('top', ypos );

     var xpos = aleft + 'px';
     $(_target).css('left', xpos );

     return false;
   });

   // dialogDown
   $(".openDialogDown").click(function(){
     $(".dialog").hide();
     var _offset = $(this).offset();
     var atop = parseInt(_offset.top);
     var aleft = parseInt(_offset.left);

     var _target = $(this).attr('href');
     $(_target).show();

     var ypos = atop+22 + 'px';
     $(_target).css('top', ypos );

     var xpos = aleft + 'px';
     $(_target).css('left', xpos );

     return false;
   });


   // competition froms
   $(".openCompetition").click(function(){
     var _offset = $(this).offset();
     var atop = parseInt(_offset.top);
     var aleft = parseInt(_offset.left);

     var _target = $(this).attr('href');
     $(_target).show();
     var h = $(_target).height();

     var ypos = atop-h-10 + 'px';
     $(_target).css('top', ypos );

     var xpos = aleft + 'px';
     $(_target).css('left', xpos );

     return false;

   });

   $(".competitionFrom").submit(function(){
     var _self = $(this);
     var _height = _self.height();
     var _width = _self.width();

     _self.css('height', _height);
     _self.css('width', _width);

     var temp = $(this).serialize();

     $.post("/ajax/comptetitionsubmissions", temp , function(data){
       _self.html( data );
       setTimeout( function(){ _self.fadeOut(3000); } ,4000 );
     });

     return false;
   });


   $("#registerForm").submit(function(){
     var _self = $(this);
     var _height = _self.height();
     var _width = _self.width();

     _self.css('height', _height);
     _self.css('width', _width);

     var temp = $(this).serialize();

     $.post("/ajax/register", temp , function(data){
       _self.html( data );
       setTimeout( function(){ _self.fadeOut(3000); } , 4000 );
     });

     return false;
   });

   $("#resendForm").submit(function(){
     var _self = $(this);
     var _height = _self.height();
     var _width = _self.width();

     _self.css('height', _height);
     _self.css('width', _width);

     var temp = $(this).serialize();

     $.post("/ajax/forgot_password", temp , function(data){
       _self.html( data );
       setTimeout( function(){ _self.fadeOut(3000); } , 4000 );
     });

     return false;
   });

 $("#feedbackForm").submit(function(){
     var _self = $(this);
     var _height = _self.height();
     var _width = _self.width();

     _self.css('height', _height);
     _self.css('width', _width);

     var temp = $(this).serialize();

     $.post("/ajax/feedback", temp , function(data){
       _self.html( data );
       setTimeout( function(){ _self.fadeOut(3000); } , 4000 );
     });

     return false;
   });

 $("#abuseForm").submit(function(){
     var _self = $(this);
     var _height = _self.height();
     var _width = _self.width();

     _self.css('height', _height);
     _self.css('width', _width);

     var temp = $(this).serialize();

     $.post("/ajax/abuse", temp , function(data){
       _self.html( data );
       setTimeout( function(){ _self.fadeOut(3000); } , 4000 );
     });

     return false;
   });

   $("#askquestion").submit(function(){
     var _self = $(this);
     var _height = _self.height();
     var _width = _self.width();

     _self.css('height', _height);
     _self.css('width', _width);

     var temp = $(this).serialize();

     $.post("/ajax/ask_tutor", temp , function(data){
       _self.html( data );
       setTimeout( function(){ _self.fadeOut(3000); } , 4000 );
     });

     return false;
   });


   // dialogs
   $(".dialog").hide();

   $(".dialog_close").click(function(){
     $(this).parent(".dialog").hide();
     return false;
   });


   // galleria

   $("#galleryList").children('li').each(function(i){
       $(this).hover(
        function () {
          $(this).addClass('active');
        },
        function () {
          $(this).removeClass('active');
        }
      );
   });


   // kommentointi
   $("#commentForm").children("img.selectPoints").each(function(i){
      $(this).bind("click", function(e){
        var _self = $(this);
        _self.parent().children('#points').val( _self.attr('alt') );
        _self.siblings().each(function(i){
          $(this).attr('src', '/images/tutoricon_'+$(this).attr('alt')+'_inactive.gif');
        });
        _self.attr('src', '/images/tutoricon_'+_self.attr('alt')+'.gif');
      });
   });







   // answerlist
   $(".contentText > dl").children("dt").children('a').each(function(){
     $(this).click(function(){
       $(this).parent('dt').next('dd').slideToggle('fast');
       $(this).toggleClass('open');
       return false;
     });
     $(this).parent('dt').next("dd").hide();
   });






    // contentText kuvan siirto
    $(".contentText > p > img").each(function(){
      _self = $(this);
      var src = _self.attr('src');
      var title = _self.attr('title');

      var _split = $(this).parent().parent('.contentText').parent('.content_split');
      var _target = _split.prev();

      _clone = $(document.createElement('img')).attr('src', src);
      _clone.attr('title', title);
      _clone.addClass('image_from_content_text');

      _target.append( _clone );

      _self.remove();
    });




    // kuvamuotoisen navigaation hover-efektit
    $("#nav1img > li > a").hover(
    //over
    function() {
      $(this).addClass("hover");
      return false;
    },
    function() {
      $(this).removeClass("hover");
      return false;
    });




// MITTAUS TIMEOUT

setTimeout(
   function(){
/*
         //muuttujat documentready
         var row1_height = 0;
         var row2_height = 0;
         var height = 0;

         // etusivun nostoboksien korkeus (kaikki yhtä korkeita kuin suurin)
         $(".js_row1").each(function(i){
           var h = parseInt( $(this).height() ) + 0;
           if( h > row1_height ){
             row1_height = h;
           }
         });
         height = row1_height + 'px';
         $(".js_row1").children(".promo").css( 'height', height );

         // .. ja toka rivi
         $(".js_row2").each(function(i){
             var h = parseInt( $(this).height() ) + 0;
               if( h > row2_height ){
                 row2_height = h;
             }
         });

         height = row2_height + 'px';
         $(".js_row2").children(".promo").css( 'height', height );

*/

/*
         // tutor commentit paikoilleen         
         $(".tutorlist_comment").each(function(){

            var _self = $(this);
            var _offset = _self.offset();
            var atop = parseInt(_offset.top)-50+'px';
            var aleft = parseInt(_offset.left)+'px';
            var awidth = _self.width()+'px';
            var aheight = _self.height()+'px';
            var atext = _self.text();
            _self.hide();

            var _new = $(document.createElement('div')).addClass('newComment');
            var _text  =   $(document.createElement('span')).addClass('commenttext');

            _new.css('position', 'absolute');
            _new.css('z-index', '10');
            _new.css('top', atop);
            _new.css('left', aleft );
            _new.css('width', awidth );
            _new.css('height', aheight );
            _text.text( atext );

            _new.append( _text );
            $('body').append( _new );
            _new.show();

         });
*/

          // sama ajatus kun etusivulla kopioitina taustajoukkojen etsivulle (level1. level2)

           var level1_height = 0;
           var level2_height = 0;
           var height = 0;

           $(".levelone").each(function(i){
             //var h = parseInt( $(this).height() ) + 0;
             var h = 0;
             $(this).children().each(function(){
               h = h + parseInt( $(this).height() );
             });
             if( h > level1_height ){
               level1_height = h;
             }
             //alert('rivin '+i+' korkeus '+h);
           });

           height = level1_height + 16 + 'px';
           //alert( 'lopullinen korkeus '+height );
           $(".levelone").css( 'height', height );

           $(".leveltwo").each(function(i){
             //var h = parseInt( $(this).height() ) + 0;
             var h = 0;
             $(this).children().each(function(){
               h = h + parseInt( $(this).height() );
             });
             if( h > level2_height ){
               level2_height = h;
             }
             //alert('rivin '+i+' korkeus '+h);
           });

           height = level2_height + 30 + 'px';
           //alert( 'lopullinen korkeus '+height );
           $(".leveltwo").css( 'height', height );


           // sitten vielä levelin 1 kuvat alas
           $(".levelone").children('img').each( function(){
             var _me = $(this);
             var _myoffset = _me.offset();
             var mytop = parseInt(_myoffset.top);
             var myheight = parseInt(_me.height());
             var mybottom = mytop+myheight;

             var _parent = _me.parent();
             var _parentoffset = _parent.offset();
             var parenttop = parseInt(_parentoffset.top);
             var parentheight = parseInt(_parent.height());
             var parentbottom = parenttop+parentheight;

             var diff = parentbottom-mybottom;
             if( diff > 0 ){
               mt = diff+'px';
               _me.css('margin-top', mt);
             }

           });


            // etusivun boksit
            // more bottonit
            /*
             $("div.frontpageItem > .promo > div").children("a.more").each(function(){

                the_height = parseInt( $(this).parent().height() );
                if( parseInt($(this).parent().next().height()) > the_height ){
                  the_height = parseInt($(this).parent().next().height());
                }

                var prev_height = 0;
                $(this).prevAll(":not('.out')").each(function(j){
                  prev_height = prev_height + parseInt( $(this).height() );
                });

                var margintop = the_height-prev_height-85 + "px";
                $(this).css("margin-top", margintop);

             });
*/




   



   },1600);

// MITTAUS TIMEOUT END

setTimeout(
   function(){

         // ETUSIVN  more buttonien asemointi
         /*
         $(".promo").children(".more").each(function(i){
              a = $(this).parent().offset(); //promoboksin kohta sivulla
              b = 163; //$(this).parent().height(); //promoboksin korkeus
              c = parseInt( a.top + b ); //edellisten yhteissumma

              d = 0;
              $(this).prevAll(":not('.out')").each(function(j){
                d = d + parseInt( $(this).height() ); //more-nappulan korkeus
              });

              var margintop = b-d-36 + "px"; //promoboksin korkeus - more-nappulan korkeus

              $(this).css("margin-top", margintop); //asetetaan more-nappulalle ylämarginaali

              // tupla chekkaus
              var f = $(this).offset(); //more-nappulan kohta sivulla
              if( f.top > (c-36) ){
               var temp = f.top - (c-36);
               margin = parseInt( $(this).css("margin-top") ) - temp;
               margintop = margin+"px";
               $(this).css("margin-top", margintop);
              }

          });

            */




   },2000);

// vielä ie6 check

setTimeout(
   function(){
     if($.browser.msie && jQuery.browser.version.substr(0,3) == "6.0" ){

     // ei täs mitään


     }
  },3000);

if( parseInt(numFrontpageItems) > 1 ){
     $("#frontpageItems").frontpageTicker(4500);
   }
   
});





$(window).resize(function(){

   var _off = $("#frontpageItems").offset();
   var _fromLeft = _off.left+'px';
   var _fromTop  = _off.top+'px';

   $("#frontpageItems").children(".frontpageItem").each(function(i){
     $(this).css('left', _fromLeft );
   });


});


