var last_id = '';

/** toggle (show/hide) the element referenced by the given id.
 * @param id
 */
function toggle(id) {
	$j('#'+ id).toggle();
}

/** toggle (show/hide) the element referenced by the given id, but only if the customer is logged in.
 * If the customer is not logged in, display the login form with the given title.
 * @param id
 * @param title
 */
function toggle_login(id, title, anchor) {
	if (get_cookie('customerid')) {
		$j('#'+ id).toggle();
	}
	else {
		var a = $j(anchor).eq(0);
		var pos = a.position();
		$j('#login_title').html(title);
		$j('#login_cont').css({
			top: $j('#reviews_cont .current_reviews_cont').position().top + 20,
			left: pos.left + a.innerWidth() + 10
		});
		if (!(last_id != id && $j('#login_cont').css('display') == 'block')) {
			$j('#login_cont').toggle('normal');
		}
		$j('#login_link_id').val(id);
	}
	last_id = id;
}

/** check the quantity to add to cart before adding to cart.
 * @param anchor
 */
function check_quantity(anchor) {
	var quantity = $j(anchor).parent().parent().find(':input').val();
	if (isNaN(quantity)) {
		quantity = 1;
	}
	if (quantity <= 0) {
		quantity = 1;
	}
	$j(anchor).attr('href', $j(anchor).attr('href') +'&q='+ quantity);
}

/** attach event functions to various elements on page load.
 */
$j(function() {
	/** add a mouseover event to see the rating descriptions and to set the rating value.
	 */
	$j('.ratings').each(function () {
		var parent = this;
		$j(this).find('img').each(function (i) {
			// graphically show the different ratings when hovering over
			$j(this).mouseover(function () {
				$j(parent).find('.rating_description').html($j('#rating_'+ i).html());
				var images = $j(parent).find('img');
				for (var x=0; x<5; x++) {
					if (x <= i) images.eq(x).attr('src', 'http://master.sissel.com/images/new/star_full.jpg');
					else images.eq(x).attr('src', 'http://master.sissel.com/images/new/star_off.jpg');
				}
			});
			// actually set the rating
			$j(this).click(function () {
				$j(parent).find('.product_rating').val(i + 1);
			});
			// set it to the rating when leaving
			$j(this).mouseout(function () {
				var rating = parseInt($j(parent).find('.product_rating').val());
				var description = '';
				if (rating > 0) description = $j('#rating_'+ (rating - 1)).html();
				$j(parent).find('.rating_description').html(description);
				var images = $j(parent).find('img');
				for (var x=1; x<=5; x++) {
					if (x <= rating) images.eq(x - 1).attr('src', 'http://master.sissel.com/images/new/star_full.jpg');
					else images.eq(x - 1).attr('src', 'http://master.sissel.com/images/new/star_off.jpg');
				}
			});
		});
	});

	/** clear username from the login input
	 */
	$j('#username').focus(function () {
		this.select();
	});
	$j('#write_review_form').submit(function () {
		review_product($j('#write_review_form a:first'));
		return false;
	});
	$j('#more_reviews_link').click(function (){
		$j('#more_reviews').toggle('normal');
		if ($j('#more_reviews_link span').html() == '&gt;') $j('#more_reviews_link span').html('&lt;');
		else $j('#more_reviews_link span').html('&gt;');
		return false;
	});
	// check for the enter keypress and 'submit' the form if enter was pressed
	$j('#model_login_form input').keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
    		user_login($j('#model_login_form a:first'));
            return true;
        }
    });
	// set the submit function of the user login form, just in case the above doesn't work
	$j('#model_login_form').submit(function () {
		user_login($j('#model_login_form a:first'));
		return false;
	});

	$j('#model_img_main img').click(function () {
		if ($j('#zoom_img_link').attr('href') != '') {
			showLightbox($j('#zoom_img_link')[0]);
		}
	});
	// set up the prev and next buttons for the thumbnail viewer
	$j('#image_nav_prev').click(function() {
		slide_thumbs('prev');
	});
	$j('#image_nav_next').click(function() {
		slide_thumbs('next');
	});
	// setup the click for each thumbnail to view the large image
	$j('#thumbs_viewer img').each(function (i) {
		$j(this).css('cursor', 'pointer');
		$j(this).click(function () {
			if (images[i] != '') {
				$j('#model_img_main .image').show();
				$j('#model_img_main .video').hide();
				$j('#model_img_normal').attr('src', images[i]);
				$j('#zoom_img_link').attr('href', zoom_images[i]);
				if (zoom_images[i] == '') {
					$j('#model_img_zoom').hide();
					$j('#model_img_main img').css('cursor', 'default');
				}
				else {
					$j('#model_img_zoom').show();
					$j('#model_img_main img').css('cursor', 'pointer');
				}
				$j('#model_img_normal').attr('width', width[i]).attr('height', height[i]);
			}
			else {
				$j('#model_img_main .video').html('<div id="model_img_video"></div>');
				var params = { allowScriptAccess: "always", autostart: 'false', allowfullscreen: 'true', movie: videos[i]+"?rel=0&enablejsapi=1&playerapiid=video_"+i };
			    var atts = { id: "video_"+i };
			    swfobject.embedSWF(videos[i]+"?rel=0&enablejsapi=1&playerapiid=video_"+i, "model_img_video", "300", "240", "8", null, null, params, atts);
				$j('#model_img_main .video').show();
				$j('#model_img_main .image').hide();
			}
		})
	});
	image_position = 0;
});

/** slide the thumbnail viewer based on the direction button pressed
 * negative values for left slide the thumbs more left
 * @param direction
 */
function slide_thumbs(direction) {
	if (num_hidden == 0) return;
	var enable_prev = true;
	var enable_next = true;
	if (direction == 'prev') {
		if (image_position > 0) {
			image_position--;
			$j('#thumbs_viewer').scrollTo('img:eq('+ image_position +')', 500);
		}
	}
	else {
		if (image_position < num_hidden) {
			image_position++;
			$j('#thumbs_viewer').scrollTo('img:eq('+ image_position +')', 500);
		}
	}
	if (image_position == 0) enable_prev = false;
	else if (image_position == num_hidden) enable_next = false;

	if (enable_prev) $j('#image_nav_prev').attr('src', 'http://master.sissel.com/images/new/nav_arrow_prev_on.gif').css('cursor', 'pointer');
	else $j('#image_nav_prev').attr('src', 'http://master.sissel.com/images/new/nav_arrow_prev.gif').css('cursor', 'default');

	if (enable_next) $j('#image_nav_next').attr('src', 'http://master.sissel.com/images/new/nav_arrow_next_on.gif').css('cursor', 'pointer');
	else $j('#image_nav_next').attr('src', 'http://master.sissel.com/images/new/nav_arrow_next.gif').css('cursor', 'default');
}

/** attempt a user login.
 */
function user_login(anchor) {
	var parent = $j(anchor).parent();
	var has_error = false;
	$j('#username').removeClass('error');
	$j('#username').removeClass('error');

	if ($j('#username').val() == '') {
		$j('#username').addClass('error');
		has_error = true;
	}
	if ($j('#password').val() == '') {
		$j('#password').addClass('error');
		has_error = true;
	}
	if (has_error) {
		$j('#login_required').show('normal');
		return false;
	}

	$j.ajax({
		type: "POST",
		url: "/review.php?action=login",
		dataType: "json",
		data: "username="+ $j('#username').val() +"&password="+ $j('#password').val(),
		success: function(data) {
			if (data.message) {
				$j('#return_message').html(data.message).fadeIn('normal');
				//hide login form
				$j('#login_cont').fadeOut(1000);
				$j(parent).find('#username').val('');
				$j(parent).find('#password').val('');
				var link_id = $j('#login_link_id').val();
				if (link_id != '') {
					var link_class = link_id.substr(0, link_id.length - 4) + "link";
					$j('.'+ link_class +':first').click();
				}
			}
			else {
				$j('#return_message').fadeIn('normal');
				$j('#return_message')[0].innerHTML = data.error;
			}
			set_message_timeout();
		}
	});
}

/** submit the rate this product form.
 */
function rate_product(anchor) {
	var parent = $j(anchor).parent();

	if ($j(parent).find('.product_rating').val() == 0) {
		$j('#rate_product_cont').toggle('normal');
		return false;
	}

	$j.ajax({
		type: "POST",
		url: "/review.php?action=rate",
		dataType: "json",
		data: "model_id="+ model_id +"&rating="+ $j(parent).find('.product_rating').val(),
		success: function(data){
			if (data.message) {
				$j('#return_message').html(data.message).fadeIn('normal');
				//update rating overview
				$j('.review_rating').replaceWith(data.rating);
				// hide buttons/form
				$j('#rate_product_cont').fadeOut(1000);
				$j('.rate_product_link').hide('normal');
				// hide the rating portion of the review
				$j('#review_rating_cont').hide('fast', function () {
					$j('#review_rating_cont').html('');
					$j('#review_rating_cont').css('margin', 0);
				});
			}
			else {
				$j('#return_message').html(data.error).fadeIn('normal');
			}
			set_message_timeout();
		}
	});
}

/** submit the review this product form.
 */
function review_product(anchor) {
	var parent = $j(anchor).parent();
	var has_error = false;

	$j('#review_required').hide();
	$j('#title').removeClass('error');
	$j('#review').removeClass('error');
	parent.find('label[for=model_review_rating]').removeClass('error');

	if (parent.find('.product_rating').length > 0) {
		if (parent.find('.product_rating').val() == '0') {
			parent.find('label[for=model_review_rating]').addClass('error');
		}
	}
	if ($j('#title').val() == '') {
		$j('#title').addClass('error');
		has_error = true;
	}
	if ($j('#review').val() == '') {
		$j('#review').addClass('error');
		has_error = true;
	}
	if (has_error) {
		$j('#review_required').show('normal');
		return false;
	}
	$j.ajax({
		type: "POST",
		url: "/review.php?action=review",
		dataType: "json",
		data: "model_id="+ model_id +"&rating="+ parent.find('.product_rating').val() +"&title="+ $j('#title').val() +"&review="+ $j('#review').val() +"&age_verify="+ $j('#title').val(),
		success: function(data) {
			if (data.message) {
				$j('#return_message').html(data.message).fadeIn('normal');
				//update rating overview
				$j('.review_rating').replaceWith(data.rating);
				// hide review buttons and form
				$j('.review_link_cont').hide('normal');
				$j('#write_review_cont').fadeOut(1500);
				// hide rating buttons and form
				$j('#rate_product_cont').fadeOut(1500);
				$j('.rate_product_link').hide('normal');
			}
			else {
				$j('#return_message').html(data.error).fadeIn('normal');
			}
			set_message_timeout();
		}
	});
}

/** check if a customer can rate this model
 */
function check_can_rate() {
	if (get_cookie('customerid')) {
		$j.ajax({
			type: "POST",
			url: "/review.php?action=can_rate",
			dataType: "json",
			data: "model_id="+ model_id,
			success: function(data) {
				// if we get a response, it will be the text for 'already rated'
				if (data.message != '') {
					$j('#return_message').html(data.message).fadeIn('normal');
					set_message_timeout();
					// hide and clear all rating elements
					$j('#rate_product_cont').fadeOut(1500, function () {
						$j('#rate_product_cont').html('');
					});
					$j('.rate_product_link').hide('normal', function () {
						$j('.rate_product_link').html('');
					});
				}
			}
		});
	}
}

/* check if the customer can review this model
 */
function check_can_review() {
	if (get_cookie('customerid')) {
		$j.ajax({
			type: "POST",
			url: "/review.php?action=can_review",
			dataType: "json",
			data: "model_id="+ model_id,
			success: function(data) {
				// if we get a response, it will be the text for 'already reviewed'
				if (data.message != '') {
					$j('#return_message').html(data.message).fadeIn('normal');
					set_message_timeout();
					// hide and clear all rating and review elements
					$j('.review_link_cont').hide().html('');
					$j('#write_review_cont').hide().html('');
				}
			}
		});
	}
}

/** rates a review if it was helpful or not.
 * If you are not logged, will show the login form.
 */
function rate_review(title, anchor, helpful) {
	if (get_cookie('customerid')) {
		// submit if it was helpful
		$j.ajax({
			type: "POST",
			url: "/review.php?action=review_helpful",
			dataType: "json",
			data: "review_id="+ $j(anchor).parent().find(':input').val() +"&helpful="+ helpful,
			success: function(data) {
				if (data.message) {
					$j('#return_message').html(data.message).fadeIn('normal');
					//update the found helpful #s if we need to
					if (data.helpful) $j(anchor).parent().parent().find('.found_helpful span').html(data.helpful);
					//hide helpful buttons
					$j(anchor).parent().fadeOut(1000);
				}
				else {
					$j('#return_message').html(data.error).fadeIn('normal');
				}
				set_message_timeout();
			}
		});
	}
	else {
		var a = $j(anchor);
		var pos = a.position();
		$j('#login_title').html(title);
		$j('#login_cont').css({
			top: pos.top - 10,
			left: pos.left + a.innerWidth() + 10
		}).toggle('normal');
		$j('#login_link_id').val('');
	}
}

/** sets a timeout to close the message box.
 */
function set_message_timeout() {
	$j('#return_message').oneTime(4500, function () {
		$j(this).fadeOut('slow');
		$j(this).stopTime();
	});
}

