$(document).ready(function(){

	var category = $('#category').attr('rel');
	

	makeLinks();
	loadVideo();
	
	/* need to pass the some extra url stuff for viewing channels */
	if ($('#category').length) {
		var extra = 'tipologie/'+category+'/';
	} else {
		var extra = '';
	}
	//console.log(extra);
	loadFromUrl(extra);	

	loadMore(); //code for loading in next set of videos  	
  	
  	/* PAGINA WHERE GALLERY */
  	$('.jimgMenu ul').kwicks({max: 700, duration: 300, easing: 'easeOutQuad'});
  	
  	
  	/* Lightbox */
	$("a[rel=group], a[rel^=lightbox]").fancybox({
		'overlayColor': '#000',
		'overlayOpacity': '0.7'	
	});
	
	/* bordi in prefooter*/
	var leftheight = $("#prefooter .left").css("height");
	var	rightheight = $("#prefooter .right").css("height");
		
	if (leftheight > rightheight) {
		$("#prefooter .right").css("height", leftheight);
	} else if (leftheight < rightheight) {
		$("#prefooter .left").css("height", rightheight);
	}
	
	/* polymaps */
	if ($('#map').length) {  
		var po = org.polymaps;
		var radius = 10, tips = {};
		var offset = $("#map").offset();
		    
		var map = po.map()
    	.container(document.getElementById("map").appendChild(po.svg("svg")))
    	.center({lon: 9.619178, lat: 45.695539})
    	.zoom(15)
    	.add(po.interact())
    	.on("move", move)
    	.on("resize", move);
    	
		map.add(po.image()
		    .url(po.url("http://{S}tile.cloudmade.com"
		    + "/218509fdcc9a487d99873b56911f4ba8" // http://cloudmade.com/register
		    + "/27169/256/{Z}/{X}/{Y}.png")
		    .hosts(["a.", "b.", "c.", ""])));
		
		map.add(po.compass()
		    .pan("none"));
		
		map.add(po.geoJson()
    	.on("load", load)
    	.on("show", show)
    	.features([
			{
			  "id": "341",
			  "properties": {
			    "html": "<img src='http://341production.com/341production.png' width='80' height='48' alt='341PRODUCTION' /><br /><p>Via Trento 24, Curno (BG)</p>"
			  },
			  "geometry": {
			    "coordinates": [9.619178, 45.695539],
			    "type": "Point"
			  }
			}
    	]));
    	
    } /* end if */

	    
	/* POLYMAPS FUNCTIONS */
function load(e) {
  for (var i = 0; i < e.features.length; i++) {
    var f = e.features[i];
    f.element.setAttribute("r", radius);
    f.element.addEventListener("mousedown", toggle(f.data), false);
    f.element.addEventListener("dblclick", cancel, false);
  }
}

function show(e) {
  for (var i = 0; i < e.features.length; i++) {
    var f = e.features[i], tip = tips[f.data.id];
    tip.feature = f.data;
    tip.location = {
      lat: f.data.geometry.coordinates[1],
      lon: f.data.geometry.coordinates[0]
    };
    update(tip);
  }
}

function move() {
  for (var id in tips) {
    update(tips[id]);
  }
}

function cancel(e) {
  e.stopPropagation();
  e.preventDefault();
}

function update(tip) {
  if (!tip.visible) return; // ignore
  var p = map.locationPoint(tip.location);

  tip.anchor.style.left = offset.left + p.x - radius + "px";
  tip.anchor.style.top = offset.top + p.y - radius + "px";

  $(tip.anchor).tipsy("show");
}

function toggle(f) {
  var tip = tips[f.id];
  if (!tip) {
    tip = tips[f.id] = {
      anchor: document.body.appendChild(document.createElement("a")),
      visible: false,
      toggle: function(e) {
        tip.visible = !tip.visible;
        update(tip);
        $(tip.anchor).tipsy(tip.visible ? "show" : "hide");
        cancel(e);
      }
    };
    tip.anchor.style.position = "absolute";
    tip.anchor.style.visibility = "hidden";
    tip.anchor.style.width = radius * 2 + "px";
    tip.anchor.style.height = radius * 2 + "px";
    $(tip.anchor).tipsy({
      html: true,
      fallback: f.properties.html,
      gravity: $.fn.tipsy.autoNS,
      trigger: "manual"
    });
  }
  return tip.toggle;
}
	
});


