// ISOTOPE
//Masonry Functions to be able to set the logo as corner-stamp
$.Isotope.prototype._masonryResizeChanged = function() { 
  return true;
};
$.Isotope.prototype._masonryReset = function() {
  // layout-specific props
  this.masonry = {};
  this._getSegments();
  var i = this.masonry.cols;
  this.masonry.colYs = [];
  while (i--) {
    this.masonry.colYs.push( 0 );
  }
  if ( this.options.masonry.cornerStampSelector ) {
    var $cornerStamp = this.element.find( this.options.masonry.cornerStampSelector ),
	stampWidth = $cornerStamp.outerWidth(true) - ( this.element.width() % this.masonry.columnWidth ),
	cornerCols = Math.ceil( stampWidth / this.masonry.columnWidth ),
	cornerStampHeight = $cornerStamp.outerHeight(true);
    for ( i = Math.max( this.masonry.cols - cornerCols, cornerCols ); i < this.masonry.cols; i++ ) {
      this.masonry.colYs[i] = cornerStampHeight;
    }
  }
};

// Isotope Initialization
$(window).bind("load", function() { //Wait till everything loads
 var $container = $('#content');
    $container.isotope({ 
	itemSelector: '.item',
        filter: '.cat0',
        animationOptions: {
            duration: 750,
            easing: 'linear',
            queue: false
        },
	masonry: {
            columnWidth: 10,
	     cornerStampSelector: '.banner_big'
	},
	getSortData : {
	    location : function ( $elem ) {
	      return parseInt($elem.attr('location'));
	    }
	},
	sortBy : 'location'
    });
});

$(document).ready(function () {
    var $container = $('#content');
    $("#nav").find('[data-filter=".cat0"]').attr("class", "active");
    
    /* Header Navigation */
    $('#nav a').click(function () {
        var selector = $(this).attr('data-filter');
	$("#nav li a").removeClass("active");
	$(this).attr("class", "active");
	
        $container.isotope({
            filter: selector,
            animationOptions: {
                duration: 750,
                easing: 'linear',
                queue: false
            },
	    sortBy : 'location'
        });
        return false;
    });

    /* Module Navigations */
    $('#post_arch').click(function () {
        var selector = $(this).attr('data-filter');
	$("#nav li a").removeClass("active");
	$("#nav").find('[data-filter="'+selector+'"]').attr("class", "active");
        $container.isotope({
            filter: selector,
            animationOptions: {
                duration: 750,
                easing: 'linear',
                queue: false
            },
	    sortBy : 'location'
        });
        return false;
    });

    /* increaseSize function */
    $.fn.increaseSize = function (columnnumber) {
	
        var $columnnumber = columnnumber;
        var $elementClicked = $(this[0]);
        var $box = $elementClicked.closest('div');

        if ($columnnumber === 2) {
            var togglebox = "doublebox";
        } else if ($columnnumber === 3) {
            var togglebox = "triplebox";
        } else if ($columnnumber === 4) {
            var togglebox = "quadruplebox";
        }

        $box.toggleClass(togglebox);
        $box.find('span.moretext').toggle();

        var $morelink = $box.find('span.more');
        var $lesslink = $box.find('span.less');
	var $dots = $box.find('#expand_dots');

        if ($morelink.length === 0) {
            $lesslink.text('More');
            $lesslink.removeClass('less');
            $lesslink.addClass('more');
	    $dots.show();
        } else {
            $morelink.text('Less');
            $morelink.removeClass('more');
            $morelink.addClass('less');
	    $dots.hide();
        }

        $container.isotope('reLayout');
    };

    /* moreText function */
    $.fn.moreText = function () {

        var $elementClicked = $(this[0]);
        var $box = $elementClicked.closest('div');

        var $morelink = $box.find('span.more');
        var $lesslink = $box.find('span.less');
	var $dots = $box.find('#expand_dots');

        $box.find('span.moretext').toggle();

        if ($morelink.length === 0) {
            $lesslink.text('More');
            $lesslink.removeClass('less');
            $lesslink.addClass('more');
	    $dots.show();
        } else {
            $morelink.text('Less');
            $morelink.removeClass('more');
            $morelink.addClass('less');
	    $dots.hide();
        }

        $container.isotope('reLayout');
    };
   $container.delegate('.moreText', 'click', function () {
       var boxdiv = $(this).parent().parent().parent()
       if(boxdiv.hasClass("hobbie") || boxdiv.hasClass("internship") || boxdiv.hasClass("experience")){
	  $(this).moreText();
       }
   });    

    /* Set any specific box to be able to be expanded by clicking on it - choose the number of columns in increaseSize(#columns). */
    $container.delegate('#hobbies.box', 'click', function (event) {
        if(!($(event.target).hasClass('edit_box_link')) && !($(event.target).hasClass('delete_box_link'))){
	    $(this).increaseSize(3);
	}
    });
    $container.delegate('#trip_ny.box', 'click', function (event) {
        if(!($(event.target).hasClass('edit_box_link')) && !($(event.target).hasClass('delete_box_link'))){
	    $(this).increaseSize(2);
	}
    });
    $container.delegate('#zuribox.box', 'click', function (event) {
	if(!($(event.target).hasClass('edit_box_link')) && !($(event.target).hasClass('delete_box_link'))){
	    $(this).increaseSize(2);
	}
    });
    
    $container.delegate('.post', 'click', function (event) {
        if(!($(event.target).hasClass('edit_box_link')) && !($(event.target).hasClass('delete_box_link'))){
	    $(this).increaseSize(2);
	}
    });
    
    // Show Email
    $(".show_email").live('click',function(){
      if(!$(".hidden_email").is(":visible")){
	$(".hidden_email").show();
	$(".show").hide();
      }
    });
    
    
    // PRELOADING 
    
    var myimg_link = $(".location_more").attr("href");
    if(myimg_link!= null && myimg_link!=""){
      //if($("#nav_admin").length !=0) var myurl = "admin/modal_large_map.php"; else
      var myurl = "modal_large_map.php";
      $.ajax({
	  type: "GET",
	  data: {img_link:myimg_link},
	  url: myurl ,
	  success: function (ajax_modal) {
	    if(ajax_modal!="false"){
	      $("#main").append(ajax_modal);
	    }
	  }   
      });
    }
    
    // CLICKABLE
    $('.location_more, #clickable_map').click(function (e) {
	$('#large_pic_3').modal({
	    position: [25],
            fixed: false,
	    containerCss:{
		width:660
	    }
	});
        return false;
    });
    
    // CLOSE SMALL GET TYBA WINDOW
    
    $(".close_get").click(function () {
      $("#get_tyba").hide("slow");
    });
    
    // CLOSE HEADER WINDOW
    
    $(".close_header").click(function () {
      $("#waiting_header").slideUp("slow");
    });

    
});

