﻿// JavaScript Document

var viewContentW;
var bindElementContent;
var sbmtAns;
var sbmtStatus;
var labelcheck;
var newupload;
var controls;
//Funciones globales
var loadEventStar;
var loadStar;

var close_item;

function loaderMoreItems(){
      $.ajax( {
                method:     "GET",
                url:        ipb_var_base_url + "act=publicelements",
                data:       {'CODE' : 'reload', 'level' : $('#total-item').text()},
                beforeSend: function() {
                            },
                complete:   function() {
                            },
                error:      function(obj, wtf, oobj){
                                alert("Ocurrio un error al hacer la peticion: "+wtf);
                },
                success:     function(data) {
					            $('#more').remove();$('#total-item').remove();$('#last-item').remove();
                                $('#content-last').append(data);
								close_item();
                             }
      });
}

$().ready(function() {
      setInterval(function() {
                     var lastelement = $('#content-last div:eq(0)').attr('id'); //Es el numero de elementos que se tienen en el escritorio por id
					 lastelement=lastelement.substr(2, lastelement.length);
                     $.ajax( {
                     method:    "GET",
                     url:       ipb_var_base_url + "act=publicelements",
                     data:      {'CODE' : 'newobject', 'lastitem' : lastelement },
                     beforeSend:function() {
                                },
                     complete:  function() {
                                },
                     error:     function(obj, wtf, oobj){
                     },
                     success:  function(data) {
                                   $(data).insertBefore('#content-last div:eq(0)').hide();
								   $('#content-last div:eq(0)').show(800);
                               }
                     });
                 }, 3000);
});

$().ready( function() {
     $('#selectCategory_warzone').change( function() {
         $.ajax( {
                   method:     "get",
                   url:        ipb_var_base_url+"act=warzone",
                   data:       {'CODE' : 'warlist_ajax_view', 'level' : $('select[name="selectCategory_options"]').val()},
                   beforeSend: function() {
                                   $('#elementLoad').show();
                               },
                   complete:   function() {
                                   $('#elementLoad').hide();
                               },
                   error:      function(obj, wtf, oobj){
                                   alert("Ocurrio un error al hacer la peticion: "+wtf);
                   },
                   success:     function(data) {
                                   $('#pzone').html(data);
                                   viewContentW();
                                 }
         });
         $('#pzone').hide();
         $('#pzone').fadeIn(1000);
     } );
} );

$(document).ready( function() {
     viewContentW = function() {
         $('#pzone a[id^="wid"]').click( function(event) {
		     if(navigator.appName!='Netscape') { $top = event.pageY; }else { $top = event.clientY }
             var widthWindow=screen.width;
             $('#elementLoad').css( {'bottom' : parseInt($top), 'left' : widthWindow/2-100 } );
             $('#elementWarzone').css( {'overflow' : 'auto', 'padding' : '10px' } );
             $('#elementWarzone').empty();
             var $this=this.id;
             $.ajax( {
             method:    "get",
             url:       ipb_var_base_url+"act=warzone",
             data:      {'CODE' : 'warlist_ajax_content', 'level' : this.id.substr(3, this.id.length)},
             beforeSend:function() {
                            $('#elementLoad').show();
                        },
             complete:  function() {
                            $('#elementLoad').hide();
                        },
             error:     function(obj, wtf, oobj){
                            alert("Ocurrio un error al hacer la peticion: "+wtf);
             },
             success:  function(data) {
                            $('#elementWarzone').html(data);
                            $('#idlayer').show();
                            $('#contentElement').show();
                            bindElementContent();
                            loadStar($this.substr(3, $this.length));
                            sbmtAns($this.substr(3, $this.length));
                       }
             });
             event.preventDefault();
         });
     }
     viewContentW();
});


$(document).ready(function() {
     sbmtAns = function(level) {
         $('form[name^="inpBox"]').submit(function(event) {
             if($('#statusAns').is (':hidden')) {
                 $.ajax( {
                         method:    "get",
                         url:       ipb_var_base_url+'act=warzone',
//                         data:      {'CODE' : 'war_validate_answer', 'level' : level, 'answer' : $('form[name^="inpBox"] input:eq(0)').val()},
                         data: {'CODE' : 'war_validate_answer', 'level' : level, 'answer' : unescape($('form[name^="inpBox"] input:eq(0)').val())}, 
                         beforeSend:function() {
                         },
                         complete:  function() {
                         },
                         error:     function(obj, wtf, oobj){
                         },
                         success:  function(data) {
                              $('#statusAns').html("<p align='center'>Procesando su respuesta...</p>"+data);
                              if(data.substring(0,11)=="Felicidades") {
                                     $('#img'+level).attr('src', '/style_images/ip.boardpr/aff_tick.gif');
                              }
                         }
                 });
                 $('#statusAns').slideToggle("slow");
                 setTimeout(function(){ $('#statusAns:visible').slideToggle("slow") }, 10000);
              }
              event.preventDefault();
         });
         $('#statusAns').click(function() {
                $('#statusAns').slideToggle("slow");
         });
     }
     bindElementContent=function() {
           $('#contentElement div').click( function(e) {
                e.stopPropagation();
           });
           $('#contentElement').click( function() {
                 $('#contentElement').css('display', 'none');
                 $('#idlayer').hide(0);
           } );
		   $('#contentElement .viewwz a').attr({target:'_blank'});
     }
     bindElementContent();

});

//Votaciones

$(document).ready(function() {
     //Funcion que será llamada cuando cargue contenido en pantalla
     loadStar=function(level) {//Reto que se esta realizando
            $('#contentStar').empty();
            var a_str=new Array("Creatividad", "Presentaci&oacute;n", "Dificultad", "Ense&ntilde;anza");
            for(var i=1; i<=4; i++) { //5 areas de votacion
                 //Contenedor local
                 $('#contentStar').append("<cite>"+a_str[i-1]+":</cite> <div id='startPoll"+i+"'></div>");
                 $('#startPoll'+i).append("<ul></ul>");
                 for(var j=1; j<=10; j++) { //Puntos que serán asignados al reto
                      $('#startPoll'+i+' ul').append("<li id='valuel"+i+"v"+j+"'><img src='http://static.ak.fbcdn.net/rsrc.php/z12E0/hash/8q2anwu7.gif' width=13 height=12 /></li>");
                      loadEventStar(i, j, level); //i estrella activada, level nivel del reto
                 }
                 $('#contentStar').append("<div id='show"+i+"'>10</div><br />");
            }
            $('#contentStar').append('<span id="total" style="float:right;margin-top:-15px;color:#CCC555;font-weight:bold;">Total: 10</span>');
            //Estilos
            $('div [id^="startPoll"]').addClass("startPoll");
            $('div [id^="value"] img').addClass("imgBg");
            $('div [id^="show"]').addClass("showValue");
     }
     loadEventStar=function($i, $j, level) {
            var $this = $('#valuel'+$i+'v'+$j); //Referencia del objeto
            $this.mousemove(function() {
                 $('#startPoll'+$i+' ul li:gt('+($j-1)+') img').css('background', 'url(/style_images/ip.boardpr/star_selected.gif)');
                 $('#startPoll'+$i+' ul li:lt('+($j)+') img').css('background', 'url(/style_images/ip.boardpr/star_filled.gif)');
                 var $k=$j<10 ? "0"+$j : $j;
                 $('#show'+$i).html($k);
            });
            $this.click( function() {
                $.ajax( {
                method:    "get",
                url:       ipb_var_base_url+"act=warzone",
                data:      {'CODE' : 'war_ajax_update', 'level' : level, 'cv' : $i, 'lv' : $j },
                beforeSend:function() {
                               $('#elementLoad').show();
                           },
                complete:  function() {
                               $('#elementLoad').hide();
                           },
                error:     function(obj, wtf, oobj){
                               alert("Ocurrio un error al hacer la peticion: "+wtf);
                },
                success:  function(data) {
                               $('#total').html("Total: "+data);
                          }
                 });
           });
     }
});

//Portal
$().ready(function() {
     labelcheck=function() {
          $('#changeStatus label').click(function() {
                $('#changeStatus').html("<form name='report_Status' action='' method='GET' enctype='multipart/form-data'>\
                                             <input type='text' name='textStatus' size='100' value='"+$(this).text()+"' class='reportStatus' />\
                                         </form>");
                $('#changeStatus input:eq(0)').focus();
                sbmtStatus();
          }).mousemove(function() {
               $(this).addClass("colorTextcS");
          }).mouseout(function() {
               $(this).removeClass("colorTextcS");
          });
     }
     labelcheck();
     sbmtStatus=function(){
           $('#changeStatus form[name^="report_Status"]').submit(function(e) {
              var nicknew=$('#changeStatus input:eq(0)').val();
              $('#changeStatus').empty().append("<label style='display:none;'></label>");
              $('#changeStatus label').load(ipb_var_base_url + 'act=publicelements', {'CODE' : 'changenick', 'nicknew' : nicknew} );
              $('#changeStatus label').fadeIn('slow');
              labelcheck();
              e.preventDefault();
           });
           $('#changeStatus form[name^="report_Status"] input[name^="textStatus"]').blur(function() {
                $('#changeStatus form[name^="report_Status"]').submit();
           });
      }
});

$().ready( function(){
 close_item=function() {
        $('.ui-icon-squaresmall-minus').click(function() {
              id=this.id.substr(8, this.id.length);
              if($('#action'+id).is (':visible')){
                  $('#action'+id).hide();
                  $('#minimize'+id).removeClass('ui-icon-squaresmall-minus').addClass('ui-icon-squaresmall-plus');
              }else{
                  $('#action'+id).show();
                  $('#minimize'+id).removeClass('ui-icon-squaresmall-plus').addClass('ui-icon-squaresmall-minus');
              }
        });

        $('.ui-icon-squaresmall-close').click(function() {
                  var answer=confirm("Estas seguro de querer eliminar esta actualizacion, una vez realizado el cambio no se podra deshacer");
                  if(answer==false) {
                        return;
                  }
                  id=this.id.substr(5, this.id.length);
                  $.ajax( {
                     method:    "get",
                     url:       ipb_var_base_url+"act=publicelements",
                     data:      {'CODE' : 'deleteupdate', 'level' : id},
                     beforeSend:function() {
                                },
                     complete:  function() {
                                },
                     error:     function(obj, wtf, oobj){
                                },
                     success:  function(data) {
                                   $('#id'+id).hide();
                                }
                  });
        });
 }
 close_item();
});

$().ready(function () {
  controls=function() {
     $('#featured-content').mousemove(function() {
         $('#controls').show();
     });
     $('#featured-content').mouseout(function() {
         $('#controls').hide();
     });
  }
  controls();
});

function return_important(tid) {
                  $.ajax( {
                     method:    "get",
                     url:       ipb_var_base_url+"act=publicelements",
                     data:      {'CODE' : 'update_post', 'level' : tid},
                     beforeSend:function() {
                                },
                     complete:  function() {
                                },
                     error:     function(obj, wtf, oobj){
                                },
                     success:  function(data) {
                                   if(data!="no.content") {
                                      $('#update_post').html(data);
                                      controls();
                                   }
                                }
                  });
}

$().ready(function() {
				   
			$("select.gTranslate").change(function() {
				  //apply at class
				  $id= parseInt(this.id.substr(2, this.id.length));
                  $.ajax( {
                     method:    "get",
                     url:       ipb_var_base_url+"act=publicelements",
                     data:      {'CODE' : 'translate', 'language' : this.value,'text' : Base64.encode($('#post-main-'+$id).text())},
                     beforeSend:function() {
                                },
                     complete:  function() {
                                },
                     error:     function(obj, wtf, oobj){
						                  alert("Failed translate, retry of new");
                                },
                     success:  function(data) {
                                   $('#post-main-'+$id).html(data);
								   $('#gT' + $id).attr("disabled", "true");
                                }
                  });
		    });
			
});