// JavaScript Document
$(document).ready(function(){
	$('.box').click(function(evt){
		evt.preventDefault();
		if($(this).hasClass('expanded')) {
			//$('.box').removeClass('expanded');	
		} else {
			$('.box').removeClass('expanded');
			$(this).addClass('expanded');
			doLayout();
		}
	});
	$('.box-expanded').each(function(){
		$(this).prepend('<a href="#close" class="close-btn">close</a>');
	});
	
	$('a.close-btn').click(function(){
		$('.box').removeClass('expanded');
		doLayout();
		return false;
	});
	
	$('.box a[href^=http://]').click(function(){
		var link = $(this).attr('href');
		window.open(link,'mywindow');
	});
	
	$('input[type="image"]').click(function(){
		$('#contactform').submit();
	});
	
	// GALLERIES
	$('#gal-rubios').cycle({
		fx:     'fade',
		speed:   300,
		timeout: 0,
		next: '#links-rubios .next, #gal-rubios img',
		prev: '#links-rubios .prev',
		pause:   1,
		after: onAfter
	});
	$('#gal-mmsd').cycle({
		fx:     'fade',
		speed:   300,
		timeout: 0,
		next: '#links-mmsd .next, #gal-mmsd img',
		prev: '#links-mmsd .prev',
		pause:   1,
		after: onAfter
	});
	$('#gal-miltons').cycle({
		fx:     'fade',
		speed:   300,
		timeout: 0,
		next: '#links-miltons .next, #gal-miltons img',
		prev: '#links-miltons .prev',
		pause:   1,
		after: onAfter
	});
	$('#gal-project').cycle({
		fx:     'fade',
		speed:   300,
		timeout: 0,
		next: '#links-project .next, #gal-project img',
		prev: '#links-project .prev',
		pause:   1,
		after: onAfter
	});
	$('#gal-crider').cycle({
		fx:     'fade',
		speed:   300,
		timeout: 0,
		next: '#links-crider .next, #gal-crider img',
		prev: '#links-crider .prev',
		pause:   1,
		after: onAfter
	});
	$('#gal-ucsd').cycle({
		fx:     'fade',
		speed:   300,
		timeout: 0,
		next: '#links-ucsd .next, #gal-ucsd img',
		prev: '#links-ucsd .prev',
		pause:   1,
		after: onAfter
	});
	$('#gal-zodiac').cycle({
		fx:     'fade',
		speed:   300,
		timeout: 0,
		next: '#links-zodiac .next, #gal-zodiac img',
		prev: '#links-zodiac .prev',
		pause:   1,
		after: onAfter
	});
	$('#gal-barney').cycle({
		fx:     'fade',
		speed:   300,
		timeout: 0,
		next: '#links-barney .next, #gal-barney img',
		prev: '#links-barney .prev',
		pause:   1,
		after: onAfter
	});
	$('#gal-turbotax').cycle({
		fx:     'fade',
		speed:   300,
		timeout: 0,
		next: '#links-turbotax .next, #gal-turbotax img',
		prev: '#links-turbotax .prev',
		pause:   1,
		after: onAfter
	});
	$('#gal-saladstyle').cycle({
		fx:     'fade',
		speed:   300,
		timeout: 0,
		next: '#links-saladstyle .next, #gal-saladstyle img',
		prev: '#links-saladstyle .prev',
		pause:   1,
		after: onAfter
	});
	$('#gal-pets').cycle({
		fx:     'fade',
		speed:   300,
		timeout: 0,
		next: '#links-pets .next, #gal-pets img',
		prev: '#links-pets .prev',
		pause:   1,
		after: onAfter
	});
	$('#gal-time').cycle({
		fx:     'fade',
		speed:   300,
		timeout: 0,
		next: '#links-time .next, #gal-time img',
		prev: '#links-time .prev',
		pause:   1,
		after: onAfter
	});
	
	// FLASH
	$('#gal-benetrac').flash({
		src: 'BT_160x600_36k.swf',
		width: 160,
		height: 600,
		flashvars: { wmode: 'transparent' }
	});
	$('#gal-rubiossocial').flash({
		src: 'player.swf',
		width: 512,
		height: 288,
		flashvars: { wmode: 'transparent',file:'video.flv' }
	});
	$('#gal-cox1').flash({
		src: 'COX_HD_728x315.swf',
		width: 728,
		height: 315,
		flashvars: { wmode: 'transparent' }
	});
	$('#gal-cox2').flash({
		src: 'CCOM-007_advanced_tv_728x315.swf',
		width: 720,
		height: 315,
		flashvars: { 'wmode': 'transparent' }
	});
	
	// INPUTS -> CLEAR TEXT ON FOCUS, RESTORE DEFAULT ON BLUR IF EMPTY
    $('input, textarea').focus(function() {
        if (($(this).val() == this.defaultValue)) {
            $(this).val('');
        }
    }).blur(function() {
        if ($(this).val() == '') {
            $(this).val(this.defaultValue);
        }
    });
	doLayout();
});

function onAfter(curr,next,opts) {
	var caption = '(' + (opts.currSlide + 1) + ' of ' + opts.slideCount + ')';
	var counter = opts.$cont.parent().parent().attr('id') + ' .counter';
	$('#'+counter).html(caption);
	//$().html(caption);
}

$(window).resize(function() {
	doLayout();
});

function doLayout () {
	var boxwidth = 220;
	var boxheight = 200;
	var boxmargin = 12;
	var mincolumns = 4;
	var totalboxes = $('.box').length;
	var columns = parseInt($('#container').innerWidth() / (boxwidth + boxmargin));
	if(columns < mincolumns) {
		columns = mincolumns;
	}
	var rows = Math.ceil(totalboxes/columns);
	var column = 0;
	var row = 0;
	var hash = new Array(rows+6);
	for(var r = 0; r <= (rows+6); r++) {
		hash[r] = new Array(columns);
		for(var c=0;c<columns;c++) {
			hash[r][c] = false;
		}
	}
	$('.box').each(function(){
		var box = $(this);
		var thisid = box.attr('id');
		var checkcol = 0;
		var checkrow = 0;
		for(var i = 0;i<=totalboxes+20;i++) {
			var current = hash[checkrow][checkcol];
			if(current) {
				checkcol++;
				if(checkcol == columns) {
					checkrow++;
					checkcol = 0;
				}
			} else {
				if((box.hasClass('expanded'))&&(checkcol >= (columns - 1))) {
					checkcol = 0;
					checkrow++;
				} else if ((box.hasClass('expanded'))&&((box.hasClass('box-3x1'))||(box.hasClass('box-3x2'))||(box.hasClass('box-3x3'))||(box.hasClass('box-3x4'))||(box.hasClass('box-3x5')))&&(checkcol >= (columns - 2))) {
					checkcol = 0;
					checkrow++;
				} else if ((box.hasClass('expanded'))&&((box.hasClass('box-4x2')))&&(checkcol >= (columns - 3))) {
					checkcol = 0;
					checkrow++;
				}
				column = checkcol;
				row = checkrow;
				break;
			}
		}
		tempLeft = (column * (boxwidth + boxmargin));
		tempTop = (row * (boxheight + boxmargin));
		if((box.hasClass('box-2x2'))&&(box.hasClass('expanded'))){
			hash[row][column+1] = true; // next column, same row (right)
			hash[row+1][column] = true; // same column, next row (below)
			hash[row+1][column+1] = true; // next column, next row (down-right)
		}
		if((box.hasClass('box-2x3'))&&(box.hasClass('expanded'))){
			hash[row][column+1] = true;
			hash[row+1][column] = true;
			hash[row+1][column+1] = true;
			hash[row+2][column] = true;
			hash[row+2][column+1] = true;
		}
		if((box.hasClass('box-2x4'))&&(box.hasClass('expanded'))){
			hash[row][column+1] = true;
			hash[row+1][column] = true;
			hash[row+1][column+1] = true;
			hash[row+2][column] = true;
			hash[row+2][column+1] = true;
			hash[row+3][column] = true;
			hash[row+3][column+1] = true;
		}
		if((box.hasClass('box-3x1'))&&(box.hasClass('expanded'))){
			hash[row][column+1] = true;
			hash[row][column+2] = true;
		}
		if((box.hasClass('box-3x2'))&&(box.hasClass('expanded'))){
			hash[row][column+1] = true;
			hash[row][column+2] = true;
			hash[row+1][column] = true;
			hash[row+1][column+1] = true;
			hash[row+1][column+2] = true;
		}
		if((box.hasClass('box-3x3'))&&(box.hasClass('expanded'))){
			hash[row][column+1] = true;
			hash[row][column+2] = true;
			hash[row+1][column] = true;
			hash[row+1][column+1] = true;
			hash[row+1][column+2] = true;
			hash[row+2][column] = true;
			hash[row+2][column+1] = true;
			hash[row+2][column+2] = true;
		}
		if((box.hasClass('box-3x4'))&&(box.hasClass('expanded'))){
			hash[row][column+1] = true;
			hash[row][column+2] = true;
			hash[row+1][column] = true;
			hash[row+1][column+1] = true;
			hash[row+1][column+2] = true;
			hash[row+2][column] = true;
			hash[row+2][column+1] = true;
			hash[row+2][column+2] = true;
			hash[row+3][column] = true;
			hash[row+3][column+1] = true;
			hash[row+3][column+2] = true;
		}
		if((box.hasClass('box-3x5'))&&(box.hasClass('expanded'))){
			hash[row][column+1] = true;
			hash[row][column+2] = true;
			hash[row+1][column] = true;
			hash[row+1][column+1] = true;
			hash[row+1][column+2] = true;
			hash[row+2][column] = true;
			hash[row+2][column+1] = true;
			hash[row+2][column+2] = true;
			hash[row+3][column] = true;
			hash[row+3][column+1] = true;
			hash[row+3][column+2] = true;
			hash[row+4][column] = true;
			hash[row+4][column+1] = true;
			hash[row+4][column+2] = true;
		}
		if((box.hasClass('box-4x2'))&&(box.hasClass('expanded'))){
			hash[row][column+1] = true;
			hash[row][column+2] = true;
			hash[row][column+3] = true;
			hash[row+1][column] = true;
			hash[row+1][column+1] = true;
			hash[row+1][column+2] = true;
			hash[row+1][column+3] = true;
		}
		hash[row][column] = true;
		box.css({
			'left' : tempLeft + 'px',
			'top'  : tempTop + 'px'
		});
		if(column == columns) {
			row++;
			column = 0;
		}
	});
	
	// determine footer position
	var checkrow = 0;
	for(var i = 0;i<=rows+6;i++) {
		var row = hash[checkrow][0];
		if(row) {
			checkrow++;
		} else {
			var goodrow = true;
			for(var j=1;j<columns;j++) {
				if(hash[checkrow][j]) {
					goodrow = false;
				}
			}
			if(goodrow) {
				row = checkrow;
				break;
			} else {
				checkrow++;
			}
		}
	}
	tempTop = (row * (boxheight + boxmargin));
	if(tempTop < (window.innerHeight - 224)) {
		tempTop = window.innerHeight - 224;
	}
	$('footer').css({
		'top' : (tempTop + 144) + 'px'
	}).show();
	
	// determine contact info/ social chiclets position
	var endright = (columns - 1) * (boxwidth + boxmargin) + (24 + boxmargin);
	$('#links, #social').css({
		'left' : endright + 'px'
	});
}
