//<![CDATA[

var ICONS = [];
var side_bar_html = "";
var ttl_videos = 0;
var gmarkers = [];
var map;

function load() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("mapdiv"));
    var myGeographicCoordinates = new GLatLng(20.0000, 0.0000);
    map.setCenter(myGeographicCoordinates, 2);
    map.setMapType(G_SATELLITE_MAP);
    map.addMapType(G_SATELLITE_3D_MAP);
        
    map.addControl(new GHierarchicalMapTypeControl());
    map.addControl(new GLargeMapControl());
    map.addControl(new GScaleControl());
    map.addControl(new GMapTypeControl());
    //map.addControl(new GOverviewMapControl());
    reLoadMarkers();
  }
}

function reLoadMarkers(){
  GDownloadUrl("ls_get/main_pageload.php", function(data) {
      if (data=="") return false;
      map.clearOverlays();
      stepcarousel.setup(null, 1);
      side_bar_html='';
      ttl_videos = 0;
      var xml = GXml.parse(data);
      var markers = xml.documentElement.getElementsByTagName("marker");
      //clearTodayTicker();
      for (var i = 0; i < markers.length; i++) {
        var id = markers[i].getAttribute("id");
        var name = markers[i].getAttribute("name");
        var descr = markers[i].getAttribute("descr");
        var img = markers[i].getAttribute("img");
        var imgt = markers[i].getAttribute("imgt");
        var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), 
                                parseFloat(markers[i].getAttribute("lng")));
        var watchc = markers[i].getAttribute("watchc");
        var publisher = markers[i].getAttribute("publisher");
        var userpic = markers[i].getAttribute("userpic");
        var uid = markers[i].getAttribute("uid");
        var vfile = markers[i].getAttribute("vfile");
        var vcat = markers[i].getAttribute("vcat");
        var views = markers[i].getAttribute("views");
        var avg = markers[i].getAttribute("avg");
        var marker = createMarker(id, name, descr, point, img, watchc, imgt, publisher, userpic, uid, vfile, vcat, views, avg);
        map.addOverlay(marker);
        //fillTodayTicker(name,watchc);
      }
      stepcarousel.setup({
      	galleryid: 'galleryB',
      	beltclass: 'belt',
      	panelclass: 'panel',
      	panelbehavior: {speed:500, wraparound:true, persist:false},
      	defaultbuttons: {enable: false, moveby: 1, leftnav: ['arrowl.gif', -10, 100], rightnav: ['arrowr.gif', -10, 100]},
      	statusvars: ['reportA', 'reportB', 'reportC'],
      	contenttype: ['own'],
      	content: side_bar_html
      }, 0);
      document.getElementById("ttl_videos").innerHTML=ttl_videos;
    });
}
    
function getCatrgoryIcon(vcat) {
  if (!ICONS[vcat]) {
    var icon = new GIcon();
    icon.image = "images/mapico/" + vcat + ".png";
    icon.iconAnchor = new GPoint(11, 34);
    icon.infoWindowAnchor = new GPoint(11, 0);
    icon.iconSize = new GSize(22, 34);
    icon.shadow = "images/mapico/shadow.png";
    icon.shadowSize = new GSize(35, 34);
    ICONS[vcat] = icon;
  }
  return ICONS[vcat];
}
    
function createMarker(id, name, descr, point, img, watchc, imgt, publisher, userpic, uid, vfile, vcat, views, avg) {
  var marker = new GMarker(point, { icon: getCatrgoryIcon(vcat) });
  var html = "<div id=mapm><div id=title>" + name + "</div>"+
  "<div id=video class='popvideo'>";
    if (vcat==2) html += "<div class='popwarn' id='pwarn_"+id+"'><div class='popwarncont'><b>WARNING</b><br>This is adult video and it may contain explicit material. You can watch it only if you are at least 18 years old or older."+
       " Are you sure you want to watch this video?<br /><br />"+
       "<u><a onclick='watch_hideAdultWarnPop("+id+");'>SHOW</a></u> | <u><a onclick='parent.map.closeInfoWindow();'>CLOSE</a></u></div></div>";
    html += "<input type='hidden' id='pvid_"+id+"' value="+vfile+">"+
    "<div class='vidp' id='pcont_"+id+"'><a href='watch.php?v="+ watchc +"'><img height=150 src="+ img +" class=dblock></a></div>"+
    "<div class='vidc'><a href='javascript:popPlayVideo("+id+");'><img src='images/mpop-c-play.gif' class=dblock></a><a href='watch.php?v="+ watchc +"'><img src='images/mpop-c-open.gif' class=dblock></a></div>"+
  "</div>"+
  "<div id=descr>" + descr + "</div>"+
  "<div id=covotes><div class=divviews>"+ views +" views</div><div class=divrate>"+ mpBuildAvgMark(avg) +"</div><div class=clear></div></div>"+
  "<div id=info><div class='infp'><img src='/ls_content/av/"+ userpic +"' class='popupic'></div>"+
  "<div class='infc'>Posted by <a href='user.php?u="+ uid +"'>"+ publisher +"</a><br>"+
  "<div class='plinks'><a href='user.php?u="+ uid +"'>Watch all "+ publisher +"'s videos</a><br><a href='watch.php?v="+ watchc +"'>Open this video in full format page</a></div>"+
  "</div></div>"+
  "</div>";
  GEvent.addListener(marker, 'click', function() {
    marker.openInfoWindowHtml(html);  
  });
  
  // save the info we need to use later for the side_bar
  gmarkers.push(marker);
  ttl_videos++;
  // add a line to the side_bar html
  side_bar_html += '<div class="panel"><a href="javascript:myclick(' + (gmarkers.length-1) + ')"><img src="'+ imgt +'" height=75></a></div>\n';
  return marker;
}

function watch_hideAdultWarnPop(id){
  document.getElementById('pwarn_'+id).style.display="none";
  return false;
}

function mpBuildAvgMark(avg){
  var width=0;
  if (avg>0) width = Math.round(16 * avg);
  return '<div id="fake-stars-off" class="stars-off" style="width:160px"><div id="fake-stars-on" class="stars-on" style="width:'+ width +'px"></div></div>';
}

function myclick(i) {
  GEvent.trigger(gmarkers[i], "click");
}


function popPlayVideo(element){
  var ni = document.getElementById("pcont_"+element);
  ni.innerHTML='';
  var newdiv = document.createElement('div');
  newdiv.setAttribute('id','pplayer_cont');
  newdiv.setAttribute('style','');
  ni.appendChild(newdiv);

  var popplay = new SWFObject("ls_player/player.swf","ply","200","150","9","#FFFFFF");
  var videofile = document.getElementById("pvid_"+element).value;
	popplay.addParam("allowscriptaccess","always");
	popplay.addParam("flashvars","file=/ls_content/vt/"+videofile+".flv&autostart=true&config=/ls_player/configs.php&controlbar=none");
	popplay.write("pplayer_cont");
}
    
function setCatSelection(do_check)  {
    var the_form = "cat_list";
    var elts      = document.forms[the_form].elements['selected_cat[]'];
    var elts_cnt  = (typeof(elts.length) != 'undefined')
                  ? elts.length
                  : 0;

    if (elts_cnt) {
        for (var i = 0; i < elts_cnt; i++) {
            elts[i].checked = do_check;
        }
    } else {
        elts.checked        = do_check;
    }
    setCheckboxes();
    return true;
}

function setCheckboxes() {
    var the_form = "cat_list";
    var selectedboxes = [];
    var elts      = document.forms[the_form].elements['selected_cat[]'];
    var elts_cnt  = (typeof(elts.length) != 'undefined')
                  ? elts.length
                  : 0;
    if (elts_cnt) {
        for (var i = 0; i < elts_cnt; i++) {
            if (elts[i].checked == true) {
              selectedboxes.push(elts[i].value);
            }
        }
    } else {
        if (elts.checked == true) {
          selectedboxes.push(elts.value);
        }
    }
    
    $.post("ls_post/categories-check.php", "categories="+selectedboxes,
    function(data){
    }, "script");
    return true;
}

// function to set and reset additional filters
function mpSwitchCarouselOptions(opt){
  document.getElementById("caropt").value=opt;
  $.post("ls_post/categories-filter.php", "catfilter="+opt,
    function(data){
  }, "script");
  if (opt==1) {
    document.getElementById("caropt1").className="sel";
    document.getElementById("caropt2").className="desel";
    document.getElementById("caropt3").className="desel";
    document.getElementById("caropt4").className="desel";
  } else if (opt==2) {
    document.getElementById("caropt1").className="desel";
    document.getElementById("caropt2").className="sel";
    document.getElementById("caropt3").className="desel";
    document.getElementById("caropt4").className="desel";
  } else if (opt==3){
    document.getElementById("caropt1").className="desel";
    document.getElementById("caropt2").className="desel";
    document.getElementById("caropt3").className="sel";
    document.getElementById("caropt4").className="desel";
  } else {
    document.getElementById("caropt1").className="desel";
    document.getElementById("caropt2").className="desel";
    document.getElementById("caropt3").className="desel";
    document.getElementById("caropt4").className="sel";
  }
}

function mpGrabLiveStreams(){
  var ssup_id = document.getElementById("ssup_id").value;
  $.post("ls_post/stream-currently.php", "ssup_id="+ssup_id, function(data){
  }, "script");
}

function mpApplyAdditionalFilter(){
  var f_datefrom = document.getElementById("datefrom").value;
  var f_dateto = document.getElementById("dateto").value;
  var f_scoopname = escape(document.getElementById("scoopname").value);
  if (f_datefrom!='' && f_dateto=='') alert("Please fill in both date fields or reset filter");
  if (f_datefrom=='' && f_dateto!='') alert("Please fill in both date fields or reset filter");
  if (f_datefrom=='' && f_dateto=='' && f_scoopname=='') return;
  
  $.post("ls_post/categories-addfilter.php", "datefrom="+f_datefrom+"&dateto="+f_dateto+"&scooper="+f_scoopname , function(data){
  }, "script");
}

function mpResetAdditionalFilter(){
  document.getElementById("datefrom").value='';
  document.getElementById("dateto").value='';
  document.getElementById("scoopname").value='';
  
  $.post("ls_post/categories-addfilter.php", "act=reset", function(data){
  }, "script");
}