$.fn.reorder = function() {
 
  // found at: http://blog.rebeccamurphey.com
  // random array sort from
  // http://javascript.about.com/library/blsort2.htm
  function randOrd() { return(Math.round(Math.random())-0.5); }
  
	return($(this).each(function() {
    var $this = $(this);
    var $children = $this.children();
    var childCount = $children.length;
    
    var childName = new Array();
    childName = $('.homeModule').get();
  	console.log(childName);
    
        
	var cookieName = 'listOrder';
 
    if (childCount > 1) {
      $children.remove();

 	// is cookie present
	if($.cookie(cookieName)) {
	
	var cookie = $.cookie(cookieName);
	if (!cookie) return;
	// make array from saved order
	neworder = cookie.split(',');
	
/* 	$.each(neworder,function(j,k) { $this.append($children.eq(k)); }); */
	
	var indices = new Array();
	for (i=0;i<childCount;i++) { indices[indices.length] = i; }

	indices = neworder;
	console.log(indices + ' new indices');	
	$.each(indices,function(j,k) { $this.append($children.eq(k)); });


/* this works */	
/* 	$this.append($children.eq(2)); */
	
	} else {
 		
	var indices = new Array();
	for (i=0;i<childCount;i++) { indices[indices.length] = i; }
	
	indices = childName.sort(randOrd);
		
	$.each(indices,function(j,k) { $this.append($children.eq(k)); });
 
 	// set cookie with list order
 	$.cookie(cookieName, indices, { path: '/', expires: 1});
 	console.log($.cookie(cookieName) + ' <-- this is cookie');
 	
    }
}
  }));
}




$(document).ready(function() {



//hide email if there
if ($('span.email').length >0) {
$('span.email').hide();
};

/* $('div#pageHome').reorder(); */


// windows subnav placement
// pc thing have to this on FF as well
var PC = (navigator.platform.indexOf("Win")>=0) ? true : false;
if(PC) {
$('#subNav').css({'margin-left' : '77px'});
}


// NOT supporting ie6 one bit
var IE6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false;
if(IE6){

	$(function(){
		
		$("<div>")
			.css({
				'position': 'absolute',
				'top': '0px',
				'left': '0px',
				backgroundColor: 'black',
				'opacity': '0.75',
				'width': '100%',
				'height': $(window).height(),
				zIndex: 5000
			})
			.appendTo("body");
			
		$("<div><a href='http://www.mozilla.com/firefox/' target='_blank'><img src='_img/ie6_sucks.gif' /></a>")
			.css({
				backgroundColor: 'black',
				'top': '50%',
				'left': '50%',
				marginLeft: -210,
				marginTop: -100,
				width: 387,
				paddingRight: 0,
				height: 170,
				'position': 'absolute',
				zIndex: 6000
			})
			.appendTo("body");
	});		
}

// testing delete cookie
/*
$('a.mastHead').click(function() { // delete cookie
$.cookie('listOrder', null, { path: '/' });
console.log('cookie cleared');
});
*/


// home page images
$('div.homeModule').hover(function() {
var homeImages = $(this).find('img:first');

homeImages.animate({'opacity': '0.5'}, 100);
homeImages.animate({'left': '-216'}, 1);
homeImages.animate({'opacity': '1'}, 200);

/* jquery syntax, not css */
$(this).css('cursor','pointer');
window.status = $(this).attr('id') + ('.php');

// change text
$(this).find('h5').css({'color' : '#b0d75d', 'text-decoration' : 'underline'});

},

function() {
var homeImages = $(this).find('img:first');

homeImages.animate({'opacity': '0.5'}, 100);
homeImages.animate({'left': '0'}, 1);
homeImages.animate({'opacity': '1'}, 100);

$(this).find('h5').css({'color' : '#c5c5c5', 'text-decoration' : 'none'});

});
// END home page hover

$('div.homeModule').click(function() {
window.location = $(this).attr('id') + ('.php');
});
// END home page click


// merch section
$('#subNav a[href="#merch"]').click(function() {

$('span.email').hide();
$('#merch').fadeIn(400);
$('#mainImg').animate({'opacity': '0.3'}, 400);

return false;
});


// close merch overlay and error all other links
$('#subNav a[href!="#merch"]').click(function() {
$('span.error').hide();
$('span.email').hide();

$('#merch').fadeOut(400);
$('#mainImg').animate({'opacity': '1.0'}, 400);

if ('#subNav a.follow') {
return true;

} else {
return false;
}
});

// merch rollover
$('div.merchModule img.itemImg').hover(function() {
$(this).animate({'left': '-212'}, 1);

},

function() {
$(this).animate({'left': '0'}, 1);

});


// contact info
$('#subNav a[href="#contact"]').click(function() {
$('span.email').toggle();

return false;
});



// splash page tumblr
if (where == 'home') {
	

var noCache = new Date().getTime();

	$.getJSON("http://seventhsontattoo.tumblr.com/api/read/json?callback=?", {'noCache': noCache },
	  function(data) { 
	    //console.log(data);
	    
	    var firstPost = data.posts[0]['photo-url-500'];
	    
	    var firstImg = firstPost;
	    
		    if(firstImg == null) {
		    	firstImg = 'http://seventhsontattoo.com/_img/fp-tumblr.jpg';
		    }
	    
	    $('.col2').css('background-image', 'url(' + firstImg + ')');
	});


	
	$('.col2').hover(function() {
		$(this).css('cursor','pointer');
		window.status = 'http://seventhsontattoo.tumblr.com';
		
		$('.col4').find('h4 a').css({'color' : '#b0d75d', 'text-decoration' : 'underline'});
		
		},
		function(){
		window.status = '';
		$('.col4').find('h4 a').css({'color' : '#D7AC5D', 'text-decoration' : 'none'});
	});
	
	
	$('.col2').click(function() {
		window.location = 'http://seventhsontattoo.tumblr.com';
	})
	
	$('.col1').hover(function() {
		$(this).css('cursor','pointer');
		window.status = 'http://seventhsontattoo.com/home.php';
		
		$('.col3').find('h4 a').css({'color' : '#b0d75d', 'text-decoration' : 'underline'});
		
		},
		function(){
		window.status = '';
		$('.col3').find('h4 a').css({'color' : '#D7AC5D', 'text-decoration' : 'none'});
	});
	
	$('.col1').click(function() {
		window.location = 'http://seventhsontattoo.com/home.php';
	})
}



});
// END dom ready



