var currentFeaturedToys;
var totalFeaturedToys;
var largeImagePreview;
var largeImg;
var imgL;


$(document).ready(function() {

    // rollovers
    PEPS.rollover.init();

    // PNG tranparency for IE6
    $('.png').pngfix();

    // set div links
    $(".link").click(function() {
        window.location = $(this).find("a").attr("href");
        return false;
    });
	
    // external links
    $("a[@rel='external']:not(#preview_img_link)").click(function() {
        window.open($(this).attr('href'));
        return false;
    });
	
	// disable current links
	$('a.current').click(function(){
		return false;						  
	});

    // Add end class to various lists
    $('ul li:last-child, ol li:last-child').addClass('end');
	
	// Search Field clear
	$('#search_text').focus(function(){
		if($(this).val() == "Search"){
			$(this).val('');
			return false;
		}
	});
	$('#search_text').blur(function(){
		if($(this).val() == ""){
			$(this).val('Search');
			return false;
		}
	});
	$('#search_button').click(function(){
		if($('#search_text').val() == "Search" || $('#search_text').val() == ""){
			return false;
		}
	});
	
	// Mailing List Form
	$('#list_name').focus(function(){
		$(this).parent().addClass('focused');
		if($(this).val() == "Full Name"){
			$(this).val('');	
		}
	});
	$('#list_name').blur(function(){
		$(this).parent().removeClass('focused');
		if($(this).val() == ""){
			$(this).val('Full Name');	
		}
	});
	$('#list_email').focus(function(){
		$(this).parent().addClass('focused');
		if($(this).val() == "Email Address"){
			$(this).val('');	
		}
	});
	$('#list_email').blur(function(){
		$(this).parent().removeClass('focused');
		if($(this).val() == ""){
			$(this).val('Email Address');	
		}
	});
	$('#list_submit').click(function(){
		if($('#list_name').val() == "Full Name" || $('#list_name').val() == "" || $('#list_email').val() == "Email Address" || $('#list_email').val() == ""){
			return false;
		}
	});
	
	// Set Highlighting and Links for Featured Items
	featuredItems();
	
	// Featured Toys Nav
	if($('#featured').length > 0){
		currentFeaturedToys = $('#ftcp').text();
		totalFeaturedToys = $('#ftpt').text();
		$('#featured_left').click(function(){
			getNewFeaturedToys('back');
			return false;
		});
		$('#featured_right').click(function(){
			getNewFeaturedToys('fwd');
			return false;
		});
	}
	
	// Set Highlighting and Links for List Grid Items
	listGridItems();

	// List View dropdowns
	$('#per_page1, #per_page2').change(function(){
		$(this).parents('form').submit();
	});
	
	
	//Left Navigation
	if($('#sort_by').length > 0){
		$('#sort_by ul li ul').hide();
		$('#sort_by ul li.open ul').show();
		$('a.sorter').click(function(){
			if($(this).parent().children('ul').children().length==0) return true;
			$(this).parent().toggleClass('open');
			$(this).next().slideToggle('fast'); 
			return false;
		});
		$('#sort_by ul ul li.open').parent().show();
	}
	
	// Larger Image on Detail View
	if($('#preview_img').length > 0){
		largeImg = $('a', '#preview_img').attr('href');
		imgL = new Image();
		imgL.src = largeImg;
		$('a', '#preview_img').click(function(){
			largeImagePreview = $('#preview_img');
			getLargerImage(largeImagePreview);
			return false;
		});
	}
	

});
// Highlighting and Links Function for Featured Items
function featuredItems(){
	$('#featured > li').hover(function(){
		$(this).addClass('over');
	},	function(){
		$(this).removeClass('over');
	});
	$('#featured > li').click(function() {
        window.location = $(this).find("a").attr("href");
        return false;
	});
}
// Highlighting and Links Function for Featured Items
function listGridItems(){
	$('#list_grid > li').hover(function(){
		$(this).addClass('over');
	},	function(){
		$(this).removeClass('over');
	});
	$('#featured > li').click(function() {
        window.location = $(this).find("a").attr("href");
        return false;
	});
}
// Featured Toys Nav Ajax Function
function getNewFeaturedToys(direction){
	if(direction == 'back'){
		if(currentFeaturedToys <= 1){
			currentFeaturedToys = totalFeaturedToys;	
		}else{
			currentFeaturedToys--;	
		}
	}
	if(direction == 'fwd'){
		if(currentFeaturedToys >= totalFeaturedToys){
			currentFeaturedToys = 1;	
		}else{
			currentFeaturedToys++;	
		}
	}
	var rnum=Math.floor(Math.random()*11);
	$.ajax({
		type: "GET",
		url: "products/featured_ajax/?p=" + currentFeaturedToys + "&r=" + rnum,
		dataType: "xml",
		success: function(xml) {
			$('#featured').empty();
			$(xml).find('item').each(function(){
				var image = $(this).find('image').text();
				var name = $(this).find('name').text();
				var url = $(this).find('url').text();
				var price = $(this).find('price').text();
				if(price!='Call for Pricing'){
					price='$'+price;	
				}
				$('#featured').append('<li><img src="'+ image + '" alt="preview" class="png" /><br /><a href="' + url + '">' + name + '</a><br />' + price + '</li>');
			});
			featuredItems();
			$('#ftcp').text(currentFeaturedToys);
		    $('.png').pngfix();

		},
		error:function (xhr, ajaxOptions, thrownError){
            	alert(xhr.status);
				alert(ajaxOptions);
				alert(thrownError);
		}
	});
}

// Large Image Function
function getLargerImage(preview){
	// Get center position for image to 'open' 
	var image = $('img', preview);
	var imgHeight = imgL.height;
	var imgWidth = imgL.width;
	var pos = image.position();
	var x = pos.left + Math.round(image.width()/2);
	var y = pos.top + Math.round(image.height()/2);
	$('body').append('<div id="largeImage" style="position:absolute; top:'+y+'px; left:'+x+'px; width:100px; height:100px; margin-top:-70px; margin-left:-70px; padding:10px; background: #fff url(css/img/ajax-loader.gif) no-repeat center center; border:10px solid #A3DEF1; overflow:hidden;"><img src="' + largeImg + '" width="100%" height="100%" /></div>');
	$('#largeImage').animate({
		width: imgWidth + 'px',
		height: imgHeight + 'px',
		marginTop: '-' + Math.round((imgHeight + 40)/2) + 'px',
		marginLeft: '-' + Math.round((imgWidth + 40)/2) + 'px'
	}, 'fast');
	$('#largeImage').append('<div style="position:absolute; width:57px; height:18px; right:0px; bottom:0px; text-align:right; color:#008faf; font-size:11px; cursor:pointer; background: #fff url('+SITE_URL+'css/img/button_large_img_close.png) no-repeat 0 0;"><!-- --></div>');
	$('#largeImage div').click(function(){
	$('#largeImage').animate({
		width: '100px',
		height: '100px',
		marginTop: '-70px',
		marginLeft: '-70px'
	}, 'fast', function(){
		$('#largeImage').remove();	
	});
	});
	
}
