sfHover = function() {
	var sfEls = document.getElementById("post_links").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" hover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" hover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);



$(document).ready(function(){ 

	$(".over a").hover(
			function() {
				if (!$(this).parent().hasClass('on')) {
					$(this).css('color', '#000');
				}
			},
			function() {
				if (!$(this).parent().hasClass('on')) {
					$(this).css('color', '#fff');
				}
			});

	$(".over ul").hover(
			function() {
				if (!$(this).parent().hasClass('on')) {
					$(this).parent().children('a').addClass('hover');
					$(this).parent().children('a').css('color', '#000');
				}
			},
			function () {
				if (!$(this).parent().hasClass('on')) {
					$(this).parent().children('a').removeClass('hover');
					$(this).parent().children('a').css('color', '#fff');
				}
	});
		   /////////////////////////// TOP PANEL ANIMATE	
	
    $("#topPanel").animate({ 
    	height: "0px",
    	opacity: 0
    }, 0 );
    
    $("div#content").show();
    $("div#preloader").hide();
    
	
	
    $(".topPanelContent").fadeOut();
    //$(".topPanelContent").css({"display":"block"});
	
	$(".topPanelLink").click(function(){
		var targetPane = $(this).attr('href');
		
		//alert(topPanelHidden);
		
		if(topPanelHidden != true){
			//$('.topPanelLink').removeClass('selected');
      		$("#topPanel").animate({ 
				height: "0px",
				opacity: 0
      		}, 500, function(){
      			//setContainerHeight();	
      		});
      		$(".topPanelContent").fadeOut(500);
      		hasClosed = true;
		}
			
			
		if(topPanelShowing != targetPane || topPanelHidden == true) {
			topPanelShowing = targetPane;
			//$(this).addClass('selected');
			//$('div' + targetPane).fadeIn(500);
			//alert($('div' + targetPane).height());
			$('div' + targetPane).animate({ 
				opacity: 1
			}, 500, function(){	
				$('div' + targetPane).fadeIn(500);
      			//setContainerHeight();	
			});

			
		 	//$('body').scrollTo( { top:0 , left:0}, 1000, {easing:'easeInOutCubic'});
			$("#topPanel").animate({ 
				opacity: 1
			}, 0 );
			int_height=$('div' + targetPane).height();
      		$("#topPanel").animate({ 
				//height: $('div' + targetPane).height(),
				height: int_height+"px",
				//height: "300px",
				opacity: 1
      		}, 500, function(){
    			//setContainerHeight();
      		});
      		hasClosed = false;
		}
		
		
		topPanelHidden = hasClosed;
		
		return false; 
    });
	
	
	$("#posts").vgrid({
		easeing: "easeOutQuint",
		time: 400,
		delay: 20
	});
	
	$(".post-image-transparent a img").fadeTo(0, .4);
	$(".post-image a img").fadeTo(0, .4);
	$(".post div.links").fadeTo(0, .0);
	fnc_adjust();
	
	  //ADJUST HEIGHT
	$('.post').each(function(i) {
		var id=$(this).attr('id');
        $("div#"+id+" div.links").css("bottom","-12px");
		$("div#"+id+" div.links").height($("div#"+id).height()-$("div#"+id+" img").height());
		//ADJUST DROPUPs
		int_listen_adjust=$("div#"+id+" div.links ul#post_links ul.li_listen").height()+9;
		$("div#"+id+" div.links ul#post_links ul.li_listen").css("top",-int_listen_adjust+"px");
		int_buy_adjust=$("div#"+id+" div.links ul#post_links ul.li_buy").height()+9;
		$("div#"+id+" div.links ul#post_links ul.li_buy").css("top",-int_buy_adjust+"px");
	});
	
	$("#container").width($("#posts").width()-30);
	$("#container").height($("#posts").height());
	$("#container").svg();
	$(".post").hoverIntent(
                 function() {
                         roll(this.id);
				    line("draw",this.id);
                 },
                 function() {
                         roll(this.id);
				    line("clear");
                 }
         );
});




function roll(id){
	$("div#"+id+" img").opacityToggle("fast");
	$("div#"+id+" div.links").fadeToggle("fast");
}

jQuery.fn.opacityToggle = function(speed, easing, callback) {
  var op = $(this).css('opacity');
  return this.stop().animate({opacity: op != .4 ? .4 : 1.0}, speed, easing, callback);
};
jQuery.fn.fadeToggle = function(speed, easing, callback) {
  var op = $(this).css('opacity');
  return this.stop().animate({opacity: op != .0 ? .0 : 1.0}, speed, easing, callback);  
};


function line(action,id) {
	fnc_adjust();
	//svg.circle(75, 75, 50, {fill: 'none', stroke: 'red', 'stroke-width': 3});
	var svg = $('#container').svg('get');
	if(action=="draw"){
	var g = svg.group({stroke: 'red', 'stroke-width': 1});
	var b = svg.group({stroke: '#FFF', 'stroke-width': 1.5});
	var arr_class=$('#'+id).attr('class').split(' ');
	$.each(arr_class,function(n,val){
		//alert(val);
	});
	
	int_y=$('#container').offset().top;
	int_x=$('#container').offset().left;
	var x1 = $('#'+id).offset().left+($('#'+id).width()/2)-int_x;
	var y1 = $('#'+id).offset().top+($('#'+id).height()/2)-int_y;
	$('.post').each(function(i) {
		//alert($(this).attr('id'));
		//alert($(this).attr('class'));
		var arr_this_class=$(this).attr('class').split(' ');
		bln_match=-1;
		if($(this).attr('id')!=id){
			$.each(arr_class,function(n,val){
					var over_class=val;
					if(over_class!="post" && over_class!="content" && over_class!="small"){
						//alert(over_class);
						$.each(arr_this_class,function(num,value){
						var match_class=value;
						 if(over_class==match_class){bln_match=1;}
						});
					}
			});
		 if(bln_match==1){
			this_id=$(this).attr('id');
			var x2 = $("#"+this_id).offset().left+($("#"+this_id).width()/2)-int_x;
			var y2 = $("#"+this_id).offset().top+($("#"+this_id).height()/2)-int_y;	
			svg.line(b, x1, y1, x2, y2);
		}
		}
	});
	//var x1 = $("#post-2").offset().left+($("#post-2").width()/2);
	//var y1 = $("#post-2").offset().top+($("#post-2").height()/2);
	//var x3 = $("#post-7").offset().left+($("#post-7").width()/2);
	//var y3 = $("#post-7").offset().top+($("#post-7").height()/2);
	//var x2 = $("#post-10").offset().left+($("#post-10").width()/2);
	//var y2 = $("#post-10").offset().top+($("#post-10").height()/2);
	//alert('post-2: '+x1+','+y1+' - post-10: '+x2+','+y2);
	//svg.line(b, x1, y1, x2, y2);
	//svg.line(b, x1, y1, x3, y3);
	//svg.line(g, 75, 15, 500, 500);
	}else if(action=="clear"){
		svg.clear();
	}
}

function fnc_adjust(){
	$("#container").children().width($("#posts").width()-33);
	$("#container").children().height($("#posts").height());
	$("#container").width($("#posts").width()-33);
	$("#container").height($("#posts").height());
}

window.onresize=function(){
	fnc_adjust();
}

function setOpen(str){
var targetPane = str;
topPanelShowing = targetPane;
//$(this).addClass('selected');
//$('div' + targetPane).fadeIn(500);
//alert($('div' + targetPane).height());
$('div' + targetPane).animate({ 
opacity: 1
}, 500, function(){
$('div' + targetPane).fadeIn(500);
       //setContainerHeight();
	   fnc_adjust();
});

//$('body').scrollTo( { top:0 , left:0}, 1000, {easing:'easeInOutCubic'});
$("#topPanel").animate({ 
opacity: 1
}, 0 );
int_height=$('div' + targetPane).height();
       $("#topPanel").animate({ 
//height: $('div' + targetPane).height(),
height: int_height+"px",
//height: "300px",
opacity: 1
       }, 500, function(){
     //setContainerHeight();
	 	fnc_adjust();
       });
       hasClosed = false;
topPanelHidden = hasClosed;
}

