
var currentItemIndex;

var bodyScroll

$(document).ready(function() {
	initGallery();
	panelCounter();
	checkEmpty();
	initHeader();
	dynamicStyling();
	removeEmptyContent("ul");
	addGetScrollPosition();
	headingCheck();
	sizeAndColourSpacing();
	dynamicStyling();
	logout();
	orderHistory();
	deptNav();
	IE6Hover();
});

function sizeAndColourSpacing() {
	if($.trim($(".sizeAndColour").text()) == "") {
	  $(".sizeAndColour").remove();
	}
	$('.sizeAndColour select:first').css('margin-right','15px');
}

function addGetScrollPosition() {
	$(".sizeAndColour select").change(function() {
		var bodyScroll = $("html").scrollTop();
		$.cookie('bodyScroll', bodyScroll);
	});
}

function scrollFromTop() {
	$("html").scrollTop();	
}

function checkEmpty() {
	if($.trim($("div.rightCol").text()) == "") {
	  $("div.rightCol").remove();
	}
}

function initGallery() {
	if ($("div.galleryItem").length > 0) {
		var numberOfItems = $("div.galleryItem").length;
		$("span.of").html(numberOfItems);
		$("a.prev").click(function() {
			currentItemIndex--;
			showGallery();	
			panelCounter();
		});
		$("a.next").click(function() {
			currentItemIndex++;
			showGallery();	
			panelCounter();
		});
		$("div.galleryItem:first a.prev, div.galleryItem:last a.next").css('visibility','hidden');
		$("div.galleryNav").show();
		currentItemIndex = 0;
		showGallery();
	}
	if ($("div.galleryItem").length < 0) {
		$("div#productGallery").hide();
		$("div.galleryNav").hide();
	}
}

function showGallery() {
	$("div.galleryItem").hide();
	$("div.galleryItem div.galleryNav").show();
	$("a.showAll").show();
	$("a.showGallery").hide();
	$("div.galleryItem:eq(" + currentItemIndex + ")").show();
}

function showAllItems() {
	$("div.galleryItem div.galleryNav").hide();
	$("a.showAll, a.showGallery").toggle();
	$("div.galleryItem").show();
}

function panelCounter() {
	if ($("span.amount").length > 0) { // Check the span exists
		$("span.amount").html(currentItemIndex + 1);
		showGallery();	
	}
}

function initHeader() {
	var head_top = $('#collapsibleArea');
    var head_bottom = $('#head-wrapper');
    var toggle_link = $('#header-toggle');
	var newheader = $.cookie('newheader');
	
	if (newheader == 'hide') {
		toggle_link.removeClass('hide').addClass('show');
		head_top.removeClass('show').addClass('hide');
		head_top.css('display','none');
	}
		
    toggle_link.click( function() {
        if(head_top.hasClass('show')) {
            toggle_link.removeClass('hide').addClass('show');
            head_top.slideUp('slow').removeClass('show').addClass('hide');
			var newheader = 'hide';
			$.cookie('newheader', 'hide', { path:'/' } );
        }
        else {
            toggle_link.removeClass('show').addClass('hide');
            head_top.slideDown('slow').removeClass('hide').addClass('show');
			var newheader = 'show';
			$.cookie('newheader', 'show', { path:'/' } );
        }
        return false;
    });
}

function dynamicStyling() {
	$('.rightColFooter ul li:last').addClass('last');
	$('div.relatedLinks:last').css('margin-bottom','0');					   
							   
	if ($('.featuredItem').length > 0) {
		$('.featuredItem').css('margin-bottom','0');
	};
}

function removeEmptyContent(selector) {
	var emptyContent = new Array();
	emptyContent.push("");
	emptyContent.push("<ul></ul>");
	emptyContent.push("<ul>&nbsp;</ul>");
	$(selector).each(function() {
		var htmlStr = $(this).html().toLowerCase();
		var htmlStr = jQuery.trim(htmlStr);
		for (var i in emptyContent) {
			if (htmlStr == emptyContent[i]) {
				$(this).replaceWith("");
				break;
			}
		}
	});
}

function headingCheck() {
	if ($('h3').length > 0) {
		$('h2').css('margin-bottom','0');
	};
}

function logout() {
	if($.trim($("div#navArea ul li:last a").text()) == "Logout") {
	  $('h2#logoutTitle').html('Logout');
	  $('h3#logoutTitleTwo').html('Thank you for using Makepiece');
	}
}
function orderHistory() {
	if($.trim($("div.orderHistoryWrap p:last").text()) == "You have no jobs in your job history.") {
	  $('p.problems').hide();
	}
}

function deptNav() {
	$('.subNavItems_lh li:even').css('margin-right','9px');	
}

function IE6Hover() {
	$(".subNavItems_lh li span").hover(function() {
		$('.subNavItems_lh li span.text').addClass("hover");
		$('.subNavItems_lh li span.clickHere').addClass("clickHereHover");
	},
	  function () {
		$('.subNavItems_lh li span.text').removeClass("hover");
		$('.subNavItems_lh li span.clickHere').removeClass("clickHereHover");
	});	
}


	
	


