Array.prototype.shuffle = function() {
	var s = [];
	while (this.length) s.push(this.splice(Math.random() * this.length, 1));
	while (s.length) this.push(s.pop());
	return this;
}

$(document).ready(function(){
	init();
	heroPhoto();
	sideBar();
	categorySeparator();
	gallery();
	shopGrid();
	relatedProducts();
});

function init() {
	var thisPage = $('body').attr('id');

    if ( thisPage == 'home') {
		Cufon.replace('#slide_links a', {
			hover: true
		});
		$('#slide_links a:last-child').css('border-bottom', 'none');
		$('#home_columns div.home_column:last').css('margin-right','0px');
		$('#home_columns div.article:last-child').css('background-image','none').css('padding-bottom','0px');
		$('.item_image').insertAfter('.item_name');
    }
	
	Cufon.replace('#page_col_left h2', {
		hover: true
	});
	
	if (thisPage == 'contact') {
		Cufon.replace('.contact_sidebar .secondary_content h2:not(#locations_title)');
		$('#locations_title').next('h2').css('padding-top','4px');
		$(".feedback_form").validate({
			rules: {
				email: {
					required: true,
					email: true
				}
			}
		});
	}
	
	if (thisPage == 'becomeaphoenixvip') {
		$(".feedback_form").validate({
			rules: {
				email: {
					required: true,
					email: true
				}
			}
		});
	}
	
	$('#footer .soc:last').css('margin-right','0px');
}

function heroPhoto() {
	$('#hero_photo_img').hide();
	var photos = ['hero_photo_01.png', 'hero_photo_02.png', 'hero_photo_03.png', 'hero_photo_04.png', 'hero_photo_05.png', 'hero_photo_06.png', 'hero_photo_07.png', 'hero_photo_08.png', 'hero_photo_09.png', 'hero_photo_10.png'];
	photos.shuffle();
	$('#hero_photo_img').attr('src', templateUrl + '/images/' + photos[0]);
	//if (jQuery.support.leadingWhitespace) {
		//$('#hero_photo_img').fadeIn(4000);
	//} else {
		$('#hero_photo_img').show();
	//}
}

function sideBar() {
	$('<div class="inner"></div>').insertAfter('.secondary_content h3');
	$('.secondary_content .inner').each(function(index) {
		$(this).nextAll().appendTo($(this));
	});

	$('<div class="box_sep"></div>').insertAfter('.box:not(:last-child)');
	$('#right_side_bar li').each(function(index) {
		var li = $.trim($(this).html());
		if (li == "") {
			$(this).remove();
		}
	});
	$('<div class="box_sep"></div>').insertAfter('#right_side_bar li:not(:last-child)');
}

function categorySeparator() {
	$('<div class="box_sep"></div>').insertAfter('#category .entry:not(:last-child)');
}

function gallery() {
	var g = $('#gallery_page');
	if (g.length) {
		$('#gallery_page p:first-child').css('margin-top', '0px');
		$('.ngg-gallery-thumbnail').each(function(index) {
			$(this).css('width','177px').css('height','177px').css('padding-bottom','20px');
			$(this).find('img:first-child').css('border','none').css('background-color','none').css('padding','0px').css('margin','0px');
			var img_w = $(this).find('img:first-child').width();
			var img_h = $(this).find('img:first-child').height();
			if (img_w > img_h) {
				$(this).wrapInner('<div class="horiz"></div>');
				var t = (177 - img_h) / 2;
				$(this).find('img:first-child').css('position','relative').css('top',t).css('left','26px');
			} else {
				$(this).wrapInner('<div class="vert"></div>');
				var l = (177 - img_w) / 2;
				$(this).find('img:first-child').css('position','relative').css('top','25px').css('left', l);
			}
  		});
	}
}

function shopGrid() {
	if ($('#grid_view_products_page_container')){
		$('.product_grid_item').css('margin','0 20px 0 0');
		$('.grid_view_clearboth').prev().css('margin-right','0px');
		$('.entry p:first').css('margin','0px 0px 15px 0px');
	}
}

function relatedProducts() {
	$('#related_products li img').each(function(index) {
    	var src = $(this).attr('src');
		src = src.replace("-78x78.png", "-145x145.png");
		$(this).attr('src',src);
	});
	$('#related_products li img').width(145);
	$('#related_products li img').height(145);
	var img_pre = '<img class="title_brackets" src="/wp-content/themes/phoenix/images/title_pre.png" />';
	var img_post = '<img class="title_brackets" src="/wp-content/themes/phoenix/images/title_post.png" />';
	$('#related_products h3').prepend(img_pre);
	$('#related_products h3').append(img_post);
}

// SLIDESHOW CODE

function slideSwitch() {
	// This version of slideSwitch has been adapted to include the 
	// feature to change the text in the accompanying mirror
	
    var $active = $('#slideshow IMG.active');
    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

	var $activeText = $('#slide_links a.active');
	if ( $activeText.length == 0 ) $activeText = $('#slide_links a:last');
	
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');
		
	var $nextText = $('#slide_links a.active').next('a');
	if ( $nextText.length == 0 ) $nextText = $('#slide_links a:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        	$activeText.removeClass('active');
			$nextText.addClass('active');
			Cufon.refresh();
		});
}

$(function() {
	var thisPage = $('body').attr('id');
	if ( thisPage == 'home') {
	    setInterval( "slideSwitch()", 4000 );
	};
});

// END SLIDESHOW
