$(function(){
	$('.thumb_view .featimg').mouseover(function(){
		$('.entry-title',$(this)).css('opacity','1');
	}).mouseleave(function(){
		$('.entry-title',$(this)).css('opacity','0');
	});
});



		$(function(){
			
			var rtime = new Date(1, 1, 2000, 12,00,00);
			var timeout = false;
			var delta = 200;
			$(window).resize(function() {
			    rtime = new Date();
			    if (timeout === false) {
			        timeout = true;
			        setTimeout(resizeend, delta);
			    }
			});

			function resizeend() {
			    if (new Date() - rtime < delta) {
			        setTimeout(resizeend, delta);
			    } else {
			        timeout = false;
			        $('#main').each(function() {
					    var p = $(this).parent();
					    $(this).height((p.height() - ($(this).offset().top - p.offset().top)));
					});
			    }               
			}

			$('#main').each(function() {
			    var p = $(this).parent();
			    $(this).height((p.height() - ($(this).offset().top - p.offset().top)));
			});

		});

		$(function(){
			var more_elem;

			$('.featimg-hover').hover(function(){
				$(this).css('opacity','1');
			}).mouseleave( function(){
				$(this).css('opacity','0');
			});

		});

		
function moveTabs(elemId){


	var currentActiveMenuIndex = jQuery('#nav ul li a.active').parent().index();
	var nameOfId = elemId.replace('#', '#ctn_');
 	var clickedMenuIndex = jQuery('#menu_'+nameOfId.replace('#ctn_', '')).parent().index();
 	var clickedMenuId = jQuery(elemId);


 	// Add active class to selected container
 	jQuery('.container-wrapper > div').removeClass('active');
 	jQuery('.container-wrapper').find(clickedMenuId).addClass('active');

 	// Change background depending on the tab
    jQuery('.bgr').find('li').fadeOut(500);
    jQuery('.bgr').find('li[data-id-bg="'+nameOfId+'"]').fadeIn(1000);

    // Verify which way menu has to go
 	var diffCurrentActive = clickedMenuIndex - currentActiveMenuIndex;

 	goto(nameOfId, this);
 	setTimeout(function() {
 		elementsForFree(nameOfId);
	}, 1100);
}

function randsort(c) {
    var o = new Array();
    for (var i = 1; i <= c; i++) {
	    o.push(i);
	}
    return o;
}
function Shuffle(o) {
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
}

function elementsForFree(parent){
	//Global vars
	var randomElements = true;


	jQuery('.thumb_view > div > article').animate({opacity: 0}, 100, 'easeInOutCubic');
	jQuery('.list_view > div > article').animate({opacity: 0}, 100, 'easeInOutCubic');
	jQuery('.grid_view > div > article').animate({opacity: 0}, 100, 'easeInOutCubic');

	// Check if div contains THUMBS VIEW
	if(jQuery(parent).find("> div.row").hasClass('thumb_view')){
		var numberOfElements = jQuery(parent + ' > .thumb_view > div').size();
		var arr = randsort(numberOfElements);
		if(randomElements === true){
			var r = Shuffle(arr);
		}else{
			var r = arr;
		}
		jQuery(parent).find("> div.row > div > article").each(function(i){
			jQuery(this).delay(r[i]*200).animate({opacity: 1}, 400, 'easeInOutCubic');
		})
	}
	// Check if div contains GRID VIEW
	if(jQuery(parent).find("> div.row").hasClass('grid_view')){
		var numberOfElements = jQuery(parent + ' > .grid_view > div').size();
		var arr = randsort(numberOfElements);
		if(randomElements === true){
			var r = Shuffle(arr);
		}else{
			var r = arr;
		}
		jQuery(parent).find("> div.row > div > article").each(function(i){
			jQuery(this).delay(r[i]*200).animate({opacity: 1}, 400, 'easeInOutCubic');
		})
	}
	// Check if div contains LIST VIEW
	if(jQuery(parent).find("> div.row").hasClass('list_view')){
		var numberOfElements = jQuery(parent + ' > .list_view > div').size();
		var arr = randsort(numberOfElements);
		if(randomElements === true){
			var r = Shuffle(arr);
		}else{
			var r = arr;
		}
		jQuery(parent).find("> div.row > div > article").each(function(i){
			jQuery(this).delay(r[i]*200).animate({opacity: 1}, 400, 'easeInOutCubic');
		})
	}


}

    $(function () {

    	// Toggle the single window
    	$(".single-clicker").click(function() {
			$('.single-container').animate({'bottom': '0%', opacity: 1}, 1000, 'easeInOutCubic');
			return false;
		});
		$(".single-close").click(function() {
			$('.single-container').animate({'bottom': '-100%', opacity: 0}, 500, 'easeInOutCubic');
			return false;
		});

       

        

         $('#nav ul li a').click(function (e) {
         	var menuId = $(this).attr('href');
         	var theNameId = menuId.replace('ctn_','');
			;
			//$(this).addClass('active');
			var currentActiveMenuIndex = $('#nav ul li a.active').parent().index();
			
			// prevent default behaviour
		    e.preventDefault();
		    // cache href attribute
		    location.hash = menuId.replace('#', '');
         });

    });
    function goto(id, t){
		//animate to the div id.
		$(".container-wrapper").animate({"top": -($(id).position().top)}, 1500, 'easeInOutCubic');
		
	}
	jQuery(document).ready(function() {
		jQuery('#nav ul.home_menu li a').click(function (e) {
	     	var menuId = jQuery(this).attr('href');
	     	var theNameId = menuId;
			//jQuery(this).addClass('active');
			var currentActiveMenuIndex = jQuery('#nav ul li a.active').parent().index();
			
			// prevent default behaviour
		    e.preventDefault();
		    // cache href attribute
		    location.hash = menuId;
	     });
		var numberOfMenus = jQuery("#nav > ul#menuid > li").size();
	    var widthOfElements = 100/numberOfMenus;
	    jQuery(".container-wrapper").css('height', numberOfMenus * 100 + '%');
	    jQuery(".container-wrapper > div").css('height', widthOfElements + '%');
	});
	
	

/* History thing */

jQuery(function(){

	/* ##################### THIS IS USED FOR THEME PRODUCTION ####################################
	if(location_hash.triger_hash  == '1'){
		if(location.hash != ""){
			var tHash = location.hash;
			moveTabs(tHash, null);
			var hash = location.hash.replace('#','');
			jQuery('#nav ul li a').removeClass('active');
			jQuery('#menu_' + hash).addClass('active');
			isHash = true;
		}else{
			isHash = false;
			//location.hash = '#ctn_tag_1_portfolio';
			location.hash = location_hash.initial_hash;
		}
	}
	*/
	if(location.hash != ""){
			var tHash = location.hash;
			moveTabs(tHash, null);
			var hash = location.hash.replace('#','');
			$('#nav ul li a').removeClass('active')
			$('#menu_' + hash).addClass('active');
			isHash = true;
		}else{
			isHash = false;
			location.hash = '#media';
		}

	if ("onhashchange" in window){
	    jQuery(window).bind('hashchange', function(e){
	        e.preventDefault();
	    	var hash = location.hash;
	    	moveTabs(hash, null);
	        return false;
	    });
	}
	
  // Bind an event to window.onhashchange that, when the hash changes, gets the
  // hash and adds the class "selected" to any matching nav link.
  jQuery(window).hashchange( function(event){
    var hash = location.hash;
    

    // Iterate over all nav links, setting the "selected" class as-appropriate.
    jQuery('#nav ul li a').each(function(){
      var that = jQuery(this);
      that[ that.attr( 'href' ) === hash ? 'addClass' : 'removeClass' ]( 'active' );
    });

  	event.preventDefault();
  })
  
  // Since the event is only triggered when the hash changes, we need to trigger
  // the event now, to handle the hash the page may have loaded with.
  
});
$(window).resize(function() {
        if(this.resizeTO) clearTimeout(this.resizeTO);
        this.resizeTO = setTimeout(function() {
            $(this).trigger('resizeEnd');
        }, 500);
    });
 $(window).bind('resizeEnd', function() {
    //do something, window hasn't changed size in 500ms
    goto('#ctn_' + location.hash.replace('#', ''));
});

