// Copyright (c) 2010 Evan Sharp / Pinterest
// All rights reserved. 
//--------------------------------------------------------------------------//

Array.max = function( array ){
    return Math.max.apply( Math, array );
};

Array.min = function( array ){
    return Math.min.apply( Math, array );
};

String.prototype.trim = function()
{
  return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str)}

String.prototype.endsWith = function(str) 
{return (this.match(str+"$")==str)}

// Text Input, Default Value Swap
function removeDefaultValue(inputToSwap, defaultValue) {
	var inputValue = $('#' + inputToSwap).val();
	if (inputValue == defaultValue) {
		$('#' + inputToSwap).val("");
	}
}
function replaceDefaultValue(inputToSwap, defaultValue) {
	var inputValue = $('#' + inputToSwap).val();
	if (inputValue == "") {
		$('#' + inputToSwap).val(defaultValue);
	}
}

// Textarea, Default Value Swap
function removeDefaultTextareaValue(textareaToSwap, defaultValue) {
	var textareaText = $('#' + textareaToSwap).text();
	if (textareaText == defaultValue) {
		$('#' + textareaToSwap).text("");
	}
}
function replaceDefaultTextareaValue(textareaToSwap, defaultValue) {
	var textareaText = $('#' + textareaToSwap).text();
	if (textareaText) {
		$('#' + textareaToSwap).val(defaultValue);
	}
}

// Board Grid
var boardLayout = {
    columnCount : 3, // Minimum column number.
    columnWidth : 193, // Fixed width of all columns.
    columnMargin : 16, // Margin (in pixel) between columns/pins.
    columnPadding : 14, // Padding within all pins
    flowPins : function() {
        var self = this;

        // Fixing .indexOf for IE
        if(!Array.indexOf){
            Array.prototype.indexOf = function(obj){
                for(var i=0; i<this.length; i++){
                    if(this[i]==obj){
                        return i;
                    }
                }
                return -1;
            }
        }

        var columns = Math.max(this.columnCount, parseInt($('#article').innerWidth() / ((this.columnWidth + (this.columnPadding * 2) + this.columnMargin))));

        // Set the length of the array to the number of columns
        var pinArray = new Array (columns);

        // Set all values in the array to 0 (to start)
        var x = 0;
        for (x = 0; x < columns; x++) {
            pinArray[x] = 0;
        }

        var allpins = $('#article .pin');

        // Set the widths of .columnContainer and #boardIntroduction, based on var 'columns'
        var columnContainerWidth = ((columns * (this.columnWidth + this.columnMargin + (2 * this.columnPadding))) - 4);
        $('#article .columnContainer').css('width', (columnContainerWidth - 13)  + 'px');
        $("#header").css("width", (columnContainerWidth - 12) + "px");
        $('#boardIntroduction, .profilebox_board').css('width', (columnContainerWidth - 40)  + 'px');

        // Set the width and padding of each .pin
        allpins.css('position', 'absolute').css('width', this.columnWidth  + 'px').css('padding', this.columnPadding  + 'px');

        // If Profile Page
        if ($('#ProfileWrapper').length > 0) {
          var profileWidth = Math.max(this.columnCount, parseInt($('#profile').innerWidth() / ((this.columnWidth + (this.columnPadding * 2) + this.columnMargin))));
          var profileWrapperWidth = ((profileWidth * (this.columnWidth + this.columnMargin + (2 * this.columnPadding))) -4);
          $('#ProfileWrapper, #header').css('width', (profileWrapperWidth - 13 - 193)  + 'px');
        }

        // If Board Page
        if ($('#boardMetadata').length > 0) {
          // Set the absolute positioning of #boardMetadata based on the number of columns
          var boardMetadataLeft = ((columns - 1) * ((this.columnWidth + (this.columnPadding * 2) + this.columnMargin)));
          $('#boardMetadata').css('left', boardMetadataLeft + 'px').css('padding', this.columnPadding + 'px');
          pinArray[(columns - 1)] = ($('#boardMetadata').height() + (this.columnPadding * 2) + this.columnMargin);
        }

        // Get the number of pins
        var numberOfPins = allpins.length;

        // Iterate through each pin, placing it in the shortest column
        allpins.each(function() {  // SLOW
            var shortestColumnValue = Math.min.apply(Math, pinArray);
            var shortestColumn = pinArray.indexOf(shortestColumnValue);

            var tempLeft = (shortestColumn * (self.columnWidth + (self.columnPadding * 2) + self.columnMargin));
            $(this).css('left', tempLeft + 'px');

            var tempTop = 0;
            if (pinArray[shortestColumn]) { tempTop = pinArray[shortestColumn]; }
            $(this).css('top', tempTop + 'px');

            pinArray[shortestColumn] = tempTop + $(this).outerHeight() + self.columnMargin;

        });

        // Set the height of #article, to pull down the bgcolor
        var tallestColumnValue = Math.max.apply(Math, pinArray);
        var articleHeight = Math.max(($(window).height() - 150), tallestColumnValue);
        $('#article').css('min-height', articleHeight + 'px');
    },
    profile_flowPins : function() {
        var self = this;

        // Fixing .indexOf for IE
        if(!Array.indexOf){
            Array.prototype.indexOf = function(obj){
                for(var i=0; i<this.length; i++){
                    if(this[i]==obj){
                        return i;
                    }
                }
                return -1;
            }
        }
        var columns = Math.max(this.columnCount, parseInt($('#profile').innerWidth() / ((this.columnWidth + (this.columnPadding * 2) + this.columnMargin))));
        // Set the length of the array to the number of columns
        var pinArray = new Array (columns);

        // Set all values in the array to 0 (to start)
        var x = 0;
        for (x = 0; x < columns; x++) {
            pinArray[x] = 0;
        }

        var allpins = $('#article .pin');

        // Set the widths of .columnContainer and #boardIntroduction, based on var 'columns'
        var columnContainerWidth = ((columns * (this.columnWidth + this.columnMargin + (2 * this.columnPadding))) - 4);
        $('#article .columnContainer').css('width', (columnContainerWidth - 13)  + 'px');
        $("#header").css("width", (columnContainerWidth - 12) + "px");
        $('#boardIntroduction').css('width', (columnContainerWidth - 40 - 237)  + 'px');

        // Set the width and padding of each .pin
        allpins.css('position', 'absolute').css('width', this.columnWidth  + 'px').css('padding', this.columnPadding  + 'px');

        // If Profile Page
        if ($('#ProfileWrapper').length > 0) {
          var profileWidth = Math.max(this.columnCount, parseInt($('#profile').innerWidth() / ((this.columnWidth + (this.columnPadding * 2) + this.columnMargin))));
          var profileWrapperWidth = ((profileWidth * (this.columnWidth + this.columnMargin + (2 * this.columnPadding))) -4);
          $('#ProfileWrapper, #header').css('width', (profileWrapperWidth - 13)  + 'px');
        }

        // Get the number of pins
        var numberOfPins = allpins.length;

        // Iterate through each pin, placing it in the shortest column
        allpins.each(function() {  // SLOW
            var shortestColumnValue = Math.min.apply(Math, pinArray);
            var shortestColumn = pinArray.indexOf(shortestColumnValue);

            var tempLeft = (shortestColumn * (self.columnWidth + (self.columnPadding * 2) + self.columnMargin));
            $(this).css('left', tempLeft + 'px');

            var tempTop = 0;
            if (pinArray[shortestColumn]) { tempTop = pinArray[shortestColumn]; }
            $(this).css('top', tempTop + 'px');

            pinArray[shortestColumn] = tempTop + $(this).outerHeight() + self.columnMargin;

        });

        // Set the height of #article, to pull down the bgcolor
        var tallestColumnValue = Math.max.apply(Math, pinArray);
        var articleHeight = Math.max(($(window).height() - 150), tallestColumnValue);
        $('#article').css('min-height', articleHeight + 'px');
    }
};

// grab variable from get parameters
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});