// AddTHIS configuration
var addthis_config = {
     ui_cobrand: "MIGZ festival",
     ui_header_color: "#ffffff",
     ui_header_background: "#24303c",
		services_compact: 'facebook, vk, twitter, myspace, delicious, blogger, linkedin, gmail, print, more'
}

$(function(){

// MIGZ RU
// custom jquery functions and event handlers

//JQUERY ADDRESS LOGIC
$.address.autoUpdate(false);

//ADDRESS ONCHANGE
$.address.change(function(event) {
	if($.address.parameter('v')!=undefined&&$.address.parameter('t')!=undefined) {
		pv = $.address.parameter('v');
		pt = $.address.parameter('t');

		$("a.stagefilter").removeClass("activefilter");
		$("a.typefilter").removeClass("activefilter");
		
		$("a[href*=#" + pv + "]").addClass("activefilter");
		$("a[href*=#" + pt + "]").addClass("activefilter");
		//alert(pv+'_'+pt);	
		
		if(pv=="allvenues") {pv=''};
		if(pt=="alltypes") {pt=''};
		
		//$.uiTableFilter( theTable, pv, "Stage" );
		
        filter_program(pv, pt);
        //$.uiTableFilter( theTable, pt+" "+pv);

        // ПРЯЧЕМ ЗАГОЛОВКИ-ЧИСЛА ТАБЛИЦ
        $.each($('.widecontainer table'), function(a, b) {
            if ($(b).find('tr.hidden').size() == $(b).find('tr').size())
            {
                $(b).parent().children('h3').addClass('hidden');
                $(b).css('marginBottom', '0px');
                //alert('hide '+$(b).parent().children('h3').html());
            }
        });
	}
	return false;
});


    function filter_program(venue, theme)
    {
        $('.widecontainer h3').removeClass('hidden');
        $('.widecontainer .tprogram').css('marginBottom', '60px');

        if (venue != '' && theme != '')
        {
            $('.tprogram tr').addClass('hidden');
            //$('.tprogram not(.scene-'+venue+')').hasClass('').addClass('hidden');
            //$('.tprogram tr:not(.theme-'+theme+')').addClass('hidden');
            //$('tr').hasClass('theme-'+theme).removeClass('hidden');
            $.each( $('.tprogram .scene-'+venue), function (i, o){
                if ($(o).hasClass('theme-'+theme)) $(o).removeClass('hidden');
            });
        }
        else if (venue != '' && theme == '')
        {
            $('.tprogram tr').addClass('hidden');
            $('.tprogram tr.scene-'+venue).removeClass('hidden');            
        }
        else if (theme != '' && venue == '')
        {
            $('.tprogram tr').addClass('hidden');
            $('.tprogram tr.theme-'+theme).removeClass('hidden');
        }
        else
        {
            $('.tprogram tr').removeClass('hidden');
        }

        if ($('.tprogram-second tr.hidden').size() == $('.tprogram-second tr').size())
            $('.events-second-day').addClass('hidden');
        if ($('.tprogram-first tr.hidden').size() == $('.tprogram-first tr').size())
            $('.events-first-day').addClass('hidden');

    }

//PROGRAM FILTERS
$("a.stagefilter").click(function(event, silent) {

    $('.events-second-day').removeClass('hidden');
    $('.events-first-day').removeClass('hidden');

    if (silent === undefined) {
        $.address.value('/');
		$.address.parameter('v', $(this).attr('href').replace(/^#/, ''), false);
		
        $('a.typefilter').each(function() {
			if($(this).hasClass("activefilter")) {
				$.address.parameter('t', $(this).attr('href').replace(/^#/, ''), false);
			}
        });
        $.address.update();
		
		checkTables();

        if ($('.tprogram-second tr.hidden').size() == $('.tprogram-second tr').size())
            $('.events-second-day').addClass('hidden');

        if ($('.tprogram-first tr.hidden').size() == $('.tprogram-first tr').size())
            $('.events-first-day').addClass('hidden');

        return false;
    }
});

$("a.typefilter").click(function(event, silent) {
    if (silent === undefined) {
        $.address.value('/');
		$.address.parameter('t', $(this).attr('href').replace(/^#/, ''), false);
		
        $('a.stagefilter').each(function() {
			if($(this).hasClass("activefilter")) {
				$.address.parameter('v', $(this).attr('href').replace(/^#/, ''), false);
			}
        });
		
        $.address.update();
        
		checkTables;
		
		return false;
    }
});

function checkTables() {
	// $("table.tprogram tbody").each(function() {
	// 	var trv = $(this).find("tr:visible");
	// 	
	// 	if (trv.length==0) {
	// 		$(this).css('backgroand', 'yellow');
	// 		//alert($(this));
	// 	}
	// 	
	// 	// else {
	// 	// 	$(this).show();
	// 	// }
	// 	
	// 	alert(trv.length);
	// 	});
}


//highlights - equalize divs floating
$("div.highlights.fp div.highlight:nth-child(2n)").addClass("hlrightside");
$("div.highlights.myevents div.highlight:nth-child(3n)").addClass("hlrightside");
//highlights -  assign clickable divs
$("div.highlight").click(function() { window.location = $(this).find("h2:first a:first").attr("href"); return false;});
$("div.highlight").hover(
  function() { $(this).addClass("hovering");}, function() {$(this).removeClass("hovering");
  }
);

//equalize heights of highlights and miniarticles
$("div.highlights div.highlight").equalHeights(180, 320);
$(".miniarticle").equalHeights();
$(".partner").equalHeights();

//set easing defaults
jQuery.easing.def = "easeOutQuint";
$.fn.hSlides.defaults.easing = 'easeOutQuint';

// slides constants
$('.accordion').hSlides({
	totalWidth: 960, 
	totalHeight: 360, 
	minPanelWidth: 100, 
	maxPanelWidth: 560,
	speed:600,
	panelPositioning: 'bottom',
	eventHandler: 'hover',
	panelSelector: 'span.slide',
	activeClass: 'active_click',
	sensitivity:15,
	interval:50,
	timeout:0,
	onEnter:enterSlide
});


function enterSlide() {
	
}

//news - clickable divs...
$("div.news div.item").hover(
  function() { $(this).addClass("hovering");}, function() {$(this).removeClass("hovering");
  }
);

$("div.news.l2 div.item").hover(
  function() { $(this).addClass("hovering");}, function() {$(this).removeClass("hovering");
  }
);




// openclose top
$(".dashboardlink-active").toggle(function(){
  $("#logo").animate({top: "-300"}, 400 );	
  $("#header").animate({top: "0"}, 400 );
  $("#headerbottom").animate({top: "236",height: "0"}, 400 );
  $("#contenttop").animate({height:"200"}, 400 );
  $("#footerwrapper").animate({bottom:"-300"}, 400 );
},
function(){
  $("#logo").animate({top: "0"}, 400 );
  $("#header").animate({top: "-200"}, 400 );
  $("#headerbottom").animate({top: "36",height: "6"}, 400 );
  $("#contenttop").animate({height:"0"}, 400 );
  $("#footerwrapper").animate({bottom:"0"}, 400 );
});


// animated scrolling function and # link interception
// $('a[href*=#]').click(function() {
// if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
//     && location.hostname == this.hostname) {
//         var $target = $(this.hash);
//         $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
//         if ($target.length) {
//             var targetOffset = $target.offset().top;
//             $('html,body').animate({scrollTop: targetOffset}, 500);
//             return false;
//         }
//     }
// });


// PROGRAM TABLE: filtering and tooltips

	//hide column 2
	//$('table.tprogram tr td:nth-child(2), table.tprogram tr th:nth-child(2)').hide();

	
	//add classes
	var artistdiv = $('table.tprogram tr td:nth-child(1)');
	$('table.tprogram tr td:nth-child(1)').addClass("e_artist");
	$('table.tprogram tr td:nth-child(2)').addClass("e_mymigz");
	$('table.tprogram tr td:nth-child(4)').addClass("e_venue");
	$('table.tprogram tr th:nth-child(5)').addClass("e_time");
	$('table.tprogram tr td:nth-child(5)').addClass("e_time");
	$(artistdiv).find("a:first").addClass("showdetail");



	// show/hide tooltip
	$('table.tprogram:not(.artists) tbody tr').mouseover(function(){
		$(this).addClass("hovering");
		
		$('ul.programcontrols').stop(true,false);
		$('ul.programcontrols').fadeTo(200, 0.33);

		var pos = $(this).offset();
		var top = pos.top - 6 + "px";
		var pic = "url(" + $(this).find("td a:first").attr('rel') + ")";
		
		$('#proghlpic').css("background-image", pic);
		$('#proginfobox').css( {top: top});
		var tt = $(this).find("td .hidden-desc").html();
		tt = tt.substr(0,256) + "...";
		$('#proginfobox p').html(tt);
		$('#proginfobox').show();
		//$('#proginfo').css(  );
	});
	$('table.tprogram:not(.artists) tbody tr').mouseout(function(){
		$(this).removeClass("hovering");
		$('ul.programcontrols').stop(true,false);
		$('ul.programcontrols').fadeTo(500, 1);
		// pos=0;
		// top=0;
		$('#proginfobox p').html('');
		$('#proginfobox').hide();
	});
	
	//mymigz button
	
	$('td.e_mymigz a').addClass("hide");
	
	/*
	$('td.e_mymigz a').click(function(){
		alert("Call to server — add/remove event from MyMIGZ");
		if ($(this).hasClass('hide')) {
			$(this).removeClass("hide");
			
			//this should be a callback after ajax call...
			$(this).addClass("show");
			$(this).text("In MyMIGZ");
		}
		else if ($(this).hasClass('show')) {
			$(this).removeClass("show");
			
			//this should be a callback after ajax call...
			$(this).addClass("hide");
			$(this).text("Mark event");
		}
	});
	*/
	
	

	var theTable = $('table.tprogram');
	var rowshidden = false;



	
	// $.address.change(function(event) {
	//         var params = event.parameters['a'];
	//         $('a.tablefilter').each(function() {
	//             var active = $(this).parent('li').attr('class').indexOf('active') != -1;
	//             if ($.inArray(
	//                     $(this).attr('href').replace(/^#/, ''), 
	//                     [].concat(params ? params : [])) != -1) {
	//                 if (!active) $(this).trigger('click', [true]);
	//             } else if (active) {
	//                 $(this).trigger('click', [true]);
	//             }
	//         });
	//     });
	
	

	//   	$("a.tablefilter").click(function() {
	// 	$("a.tablefilter, a#sortaz").removeClass("activefilter");
	// 	$(this).addClass("activefilter");
	// 	$.uiTableFilter( theTable, this.name, "Stage" );
	// 	positionFooter();
	// 	return false;
	// });
	// 
	// $("ul.filterx li").click(function() {
	// 	$("ul.filterx li").removeClass("active");
	// 	$(this).addClass("active");
	// 	$.uiTableFilter( theTable, this.name );
	// 	positionFooter();
	// 	return false;
	// });
	
  	$("a#sortaz").click(function() {
		$("a.tablefilter").removeClass("activefilter");
		$(this).addClass("activefilter");
	//	$.uiTableFilter( theTable, this.name );
		positionFooter();
		return false;
	});


//event page dim

$("div.sidebar div.swdim.on").mouseover(function(){
	$(this).removeClass("on");
});

$("div.sidebar div.swdim.on").mouseout(function(){
	$(this).addClass("on");
});



// sticky footer
	positionFooter();
	$(window)
	  .scroll(positionFooter)
	  .resize(positionFooter);

	function positionFooter() {
	  var docHeight = $(document.body).height() - $("#sticky-footer-push").height();
	  if(docHeight < $(window).height()){
	    var diff = $(window).height() - docHeight;
	    if (!$("#sticky-footer-push").length > 0) {
	      $("#footerwrapper").before('<div id="sticky-footer-push"></div>');
	    }
	    $("#sticky-footer-push").height(diff);
	  }  
	}
	


$('div.col:nth-child(4n)').addClass("right");


//venues
$('div.venue:odd').addClass("right");
$("div.venue").equalHeights(140, 200);


$('div.widecontainer > h3').wrapInner("<span></span>")


//font replacement
// Cufon.replace ('span.slide > h3, h1.pagetitle, div.widecontainer > h3');

//clearfix
$('.clearfix').parent().append('<div class="clear"></div>'); //apply jquery clearfix

});
