 $(document).ready(function(){
     if(!preview){
       move();
       if(document.location.protocol == 'https:')
           $('table[id^=s_]').remove();
       window.onscroll =  function(){
           var scrol = $(document).scrollTop();
           var dh    = $(document).height();
           var wh    = $(window).height();
           var padding = 30;
           var an    = 200;
           

           $('.right-top').each(function(index){
               if(index > 0){
                   var ph = 0;
                   for(var i = 0; i < index; ++i){
                       var prev = $('.right-top')[i];
                       ph += $(prev).height();
                   }
                   var sc = scrol + padding + ph;
               } else {
                   var sc = scrol + padding;
               }
               sc += 'px';
               $(this).stop().animate({top: sc}, 200);
           });

           $('.left-top').each(function(index){
               if(index > 0){
                   var ph = 0;
                   for(var i = 0; i < index; ++i){
                       var prev = $('.left-top')[i];
                       ph += $(prev).height();
                   }
                   var sc = scrol + padding + ph;
               } else {
                   var sc = scrol + padding;
               }
               sc += 'px';
               $(this).stop().animate({top: sc}, 200);
           });
           $('.cent').each(function(index, elem){
               if(index > 0){
                   var ph = 0;
                   for(var i = 0; i < index; ++i){
                       var prev = $('.cent')[i];
                       ph += $(prev).height();
                   }
                   var sc = scrol + padding + ph;
               } else {
                   var sc = scrol + padding;
               }
               sc += 'px';
               $(this).stop().animate({top: sc}, 200);
           });

           $('.right-bottom').each(function(index, elem){
               if(index > 0){
                   var ph = 0;
                   for(var i = 0; i < index; ++i){
                       var prev = $('.right-bottom')[i];
                       ph += $(prev).height();
                   }
                   var sc = -scrol + padding + ph;
               } else {
                   var sc = -scrol + padding;
               }
               sc += 'px';
               $(this).stop().animate({bottom: sc}, 200);
           });

           $('.left-bottom').each(function(index, elem){
               if(index > 0){
                   var ph = 0;
                   for(var i = 0; i < index; ++i){
                       var prev = $('.left-bottom')[i];
                       ph += $(prev).height();
                   }
                   var sc = -scrol + padding + ph;
               } else {
                   var sc = -scrol + padding;
               }
               sc += 'px';
               $(this).stop().animate({bottom: sc}, 200);
           });

      };
       $('.survey-table-border').css('position', 'absolute');

//multi vote
           $('.survey-submitkey').click(MultiVote);


//close survey
           $('.survey-close').click(CloseSurvey);
     }
   });

    function MultiVote(){
        var ids = new Array();
        $('input.surv:checked').each(function(){
            var id = $(this).attr("id");
            id = id.split('_')[1];
            ids.push(id);
            var redir = $('#redir_'+id).val();
            if(redir != null && redir != '')
                window.open(redir,'_blank');
        });
        if (ids.length==0){

            return false;
        }
        var sur_id = $(this).attr('id'); 
        sur_id = sur_id.split('_');
        $('#s_'+sur_id[1]).remove();
        
            var params = {};
            params.secure = true;var _location = document.location.href; 
            if(_location.indexOf('dxloo') > 0){
                params.domain = ".dms9.dxloo.com";
            } else if(_location.indexOf('ixloo') > 0){
                params.domain = ".ixloo.com";
            }
            $.cookie('secure_'+sur_id.join('_'), true, params);
       
        
        $.cookie(sur_id.join('_'), true);

        $.ajax({
                url:        '/ajax',
                datatype:   'json',
                type:       'POST',
                data:        {oper: 'vote', id: ids}
            });
    }

   function CloseSurvey(){
          var cl = $(this).parent().parent().parent().parent().parent().parent().parent().remove().attr('class');
          if(cl.indexOf('passive') > 0){
             var id = $(this).parent().parent().parent().parent().parent().parent().parent().attr('id');
             $.cookie(id, true);
             
                var params = {};
                   params.secure = true;var _location = document.location.href;
                if(_location.indexOf('dxloo') > 0){
                    params.domain = ".dms9.dxloo.com";
                } else if(_location.indexOf('ixloo') > 0){
                    params.domain = ".ixloo.com";
                }
                $.cookie('secure_'+id, true, params);
            
          }
          $('#'+id).remove();
          //$(this).parent().parent().parent().parent().parent().parent().parent().remove();
   }


   function move(){
	   $('.survey-table-border').not('.right-top, .right-top-fixed, .left-top, .left-top-fixed, .cent, .cent-fixed, .left-bottom, .left-bottom-fixed, .right-bottom, .right-bottom-fixed').remove();
           var rt = $('.right-top, .right-top-fixed');
           if($(rt).size() > 0){
               $(rt).each(function(index, elem){
                   if(index > 0){
                       var ph = 0;
                       for(var i = 0; i < index; ++i){
                           var prev = $(rt)[i];
                           ph += $(prev).height();
                       }
                       var right = $(elem).css('top');
                       var top = new Number(right.split('px')[0]);
                       top += ph;
                       top += 'px';
                       $(elem).css('top', top);
                   }

               });
           }

           var lt = $('.left-top, .left-top-fixed');
           if($(lt).size() > 0){
               $(lt).each(function(index, elem){
                   if(index > 0){
                       var ph = 0;
                       for(var i = 0; i < index; ++i){
                           var prev = $(lt)[i];
                           ph += $(prev).height();
                       }
                       var right = $(elem).css('top');
                       var top = new Number(right.split('px')[0]);
                       top += ph;
                       top += 'px';
                       $(elem).css('top', top);
                   }

               });
           }

            var cent = $('.cent, .cent-fixed');
            if($(cent).size() > 0){
               $(cent).each(function(index, elem){
                   if(index > 0){
                       var ph = 0;
                       for(var i = 0; i < index; ++i){
                           var prev = $(cent)[i];
                           ph += $(prev).height();
                       }
                       var right = $(elem).css('top');
                       var top = new Number(right.split('px')[0]);
                       top += ph;
                       top += 'px';
                       $(elem).css('top', top);
                   }

               });
            }
            var lb = $('.left-bottom, .left-bottom-fixed');
            if($(lb).size() > 0){
               $(lb).each(function(index, elem){
                   if(index > 0){
                       var ph = 0;
                       for(var i = 0; i < index; ++i){
                           var prev = $(lb)[i];
                           ph -= $(prev).height(); 
                       }
                       var right = $(elem).css('bottom');
                       var top = new Number(right.split('px')[0]); 
                       top += ph;
                       top += 'px'; 
                       $(elem).css('bottom', top);
                   }

               });
           }

           var rb = $('.right-bottom, .right-bottom-fixed');
            if($(rb).size() > 0){
               $(rb).each(function(index, elem){
                   if(index > 0){
                       var ph = 0;
                       for(var i = 0; i < index; ++i){
                           var prev = $(rb)[i];
                           ph -= $(prev).height(); 
                       }
                       var bottom = $(elem).css('bottom');
                       var bot = new Number(bottom.split('px')[0]);
                       bot += ph;
                       bot += 'px';
                       $(elem).css('bottom', bot);
                   }

               });
           }
           $('.right-top, .right-top-fixed, .left-top, .left-top-fixed, .cent, .cent-fixed, .left-bottom, .left-bottom-fixed, .right-bottom, .right-bottom-fixed').show();
   }


   function addSurvey(place, id, mode, beh){
        var sur = $('#'+id).hide();
        var padding = '30px';

        if(beh == 1)
            $(sur).addClass('passive');

        $(sur).css('z-index', 1100);

       switch(place){
            case  "0":
                if(mode == "0")
                    $(sur).addClass('left-top-fixed');
                else
                    $(sur).addClass('left-top');

                    $(sur).css('left', padding).css('top', padding);

                break;

            case "1":
                if(mode == "0")
                    $(sur).addClass('right-top-fixed');
                else
                    $(sur).addClass('right-top');
                $(sur).css('right', padding).css('top', padding);
                break;

            case "2":
                var w = $(sur).width();
                var h = $(sur).height();
                var ww = $(window).width();
                var wh = $(window).height();
                var left = (ww / 2) - (w / 2);
                var top  = (wh / 2) - (h / 2);
                $(sur).css('left', left+'px').css('top', top+'px');

                if(mode == "0")
                    $(sur).addClass('cent-fixed');
                else
                    $(sur).addClass('cent');
                break;

            case "3":
                $(sur).css('left', padding).css('bottom', padding);
                if(mode == "0")
                    $(sur).addClass('left-bottom-fixed');
                else
                    $(sur).addClass('left-bottom');
                break;

            case "4":
                $(sur).css('right', padding).css('bottom', padding);
                if(mode == "0")
                    $(sur).addClass('right-bottom-fixed');
                else
                    $(sur).addClass('right-bottom');
                break;

            default:
                break;
       }
   }
