/*	PGS jquery
*/

function related_products_vertical_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function video_thumbs_box_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

var $j = jQuery.noConflict();

$j(function(){ 
 
    $j(document).ready(function() {
	
	 
	 $j("#comment-login").click(function() {
		$j.scrollTo(0);
		return false;
		});
	 
	 
	  var submitcomment = false;
	 

	var subfreeze = null;
	

      function addMega(){ 
			var choice = $j(this).attr("rel");
			$j("#"+choice).animate({ "height": "show", "opacity": "show" }, "slow" ); 
			 $j(this).removeClass("mega"); 
			 $j(this).addClass("topcathover");  
        }

      function removeMega(){
	  
									var choice = $j(this).attr("rel");
									var thisid = $j(this).attr("id");
									if (subfreeze != choice){
											//alert(subfreeze+"| choice: "+thisid);   	
											$j("#"+choice).animate({"height": "hide", "opacity": "hide"}, "slow"); 
											 $j(this).removeClass("topcathover"); 
											  $j(this).addClass("mega"); 
											} 
					}
		 
      function showSub(){ 
						var parent = $j(this).attr("rel");
						var choice = $j(this).attr("id");
						 subfreeze = choice;
					 //$j("#" + parent).addClass("ul#top-cat-navmenu li.mega a:hover"); 
						$j("#" + parent).removeClass("mega"); 
						$j("#" + parent).addClass("topcathover");  


					}

      function hideSub(){
					  subfreeze = null;
					  var parent = $j(this).attr("rel");
					  var choice = $j(this).attr("id");
					  $j("#"+choice).animate({"height": "hide", "opacity": "hide"}, "slow"); 
					  $j("#" + parent).addClass("mega"); 
					   $j("#" + parent ).removeClass("topcathover"); 
					}

    var megaConfig = {
         interval: 30,
         sensitivity: 1,
         over: addMega,
         timeout: 600,
         out: removeMega
    };
	
	    var subConfig = {
         interval: 20,
         sensitivity: 2,
         over: showSub,
         timeout: 800,
         out: hideSub
    };

	
	
	//BOF Xsell jquery stuff
	
	  //Get our elements for faster access and set overlay width
  var div = $j('div.sc_menu'),
               ul = $j('ul.sc_menu'),
               // unordered list's left margin
               ulPadding = 15;

  //Get menu width
  var divWidth = div.width();

  //Remove scrollbars
  div.css({overflow: 'hidden'});

  //Find last image container
  var lastLi = ul.find('li:last-child');

  //When user move mouse over menu
  div.mousemove(function(e){

    //As images are loaded ul width increases,
    //so we recalculate it each time
    var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

    var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
    div.scrollLeft(left);
  });
  
	$j('#commentform').hide();
	$j('#comment-submit-button-area-loader').hide(); 
	$j('#comment-form-mssg').hide();
	
	
	 	$j('#cancel-comment-reply-link').click(function() { 
		//alert('ding');
				$j('#comment_parent').val(null);
				$j('#cancel-comment-reply-link').hide('fast');
				 return false;
					});
	
	 	$j('#pgs-product-comments-form-switch').click(
						function() { openCommentForm(null);
							return false;
							}
						);

	 openCommentForm = function(parentID) {
				if(submitcomment==false){
					if(parentID){
							$j('#cancel-comment-reply-link').show('fast');
							$j.scrollTo('#respond');
							}else{
							$j('#cancel-comment-reply-link').hide('fast');
							}
		
					$j('#comment_parent').val(parentID); 
					$j('#submit-comment').show('fast');
					$j('#commentform').slideDown("fast");
					$j('#comment-form-comment-box textarea').fadeTo("fast", 1);
					$j("#pgs-product-charlimitinfo").html('You have 800 characters left.');
				return false;
				}
				}
			
				
				  $j('#submit-comment').click(function() {
						submitcomment = true;
						var comment_post_ID = $j("#comment_post_ID").val();
						var comment_parent = $j("#comment_parent").val();
						var wp_unfiltered_html_comment = $j("#_wp_unfiltered_html_comment").val();
						var author = $j("#author").val();
						var email = $j("#email").val();
						var comment = $j("#comment").val();

						var commentDataString = 'comment_post_ID='+ comment_post_ID + '&comment_parent=' + comment_parent + '&_wp_unfiltered_html_comment=' + wp_unfiltered_html_comment + '&author=' + author + '&email=' + email + '&comment=' + comment;
						
						//alert(commentDataString);

				 //if (comment!='') {
				$j('#comment-form-comment-box textarea').fadeTo("fast", 0.01);
						$j('#submit-comment').hide();
						$j('#comment-submit-button-area-loader').show();
						$j('#cancel-comment-reply-link').hide('fast');						
						//begin ajax
						$j.ajax({
						type: "POST",
						url: "/wp-comments-post.php",
						data: commentDataString,
						
								success: function(){ 
										$j("#comment").val('');
										$j("#pgs-product-charlimitinfo").html('');
										 $j('#comment-submit-button-area-loader').hide('fast',function(){
														
																$j('#comment-form-mssg').show(100, function(){
																		$j('#comment-form-mssg').animate({opacity: 1.0}, 2500).hide(100, function(){
																		$j('#commentform').slideUp("fast"); });
																	});
															
											}); 
											
											submitcomment = false;
								 
								}
						
						});//end ajax
				 //}

				return false;
				});
				
	$j('#comment').keyup(function(){ 
 		limitChars('comment', 800, 'pgs-product-charlimitinfo');
 	}) 
	
	function limitChars(textid, limit, infodiv)
		{
			var text = $j('#'+textid).val();	
			var textlength = text.length;
			if(textlength > limit)
			{
				$j('#' + infodiv).html('You cannot write more then '+limit+' characters!');
				$j('#'+textid).val(text.substr(0,limit));
				return false;
			}
			else
			{
				$j('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
				return true;
			}
		}
	
	if ($j('#thumbs-vertical-scroll').length){
		    $j('#thumbs-vertical-scroll').jcarousel({
				animation: 'fast',
		        vertical: true,
		        scroll: 4,
				visible: 4 
		    });
	};
	
	if ($j('#related-products-vertical-scroll').length){
		    $j('#related-products-vertical-scroll').jcarousel({
			start: 0,
			animation: 'fast',
	        vertical: true,
	        scroll: 2,
			visible: 15,
			auto: 2,
			wrap: 'last',
	        initCallback: related_products_vertical_initCallback
	    });
	};
		




											var pagination_options = {
											  num_edge_entries: 2,
											  num_display_entries: 5,
											  callback: pageselectCallback,
											  items_per_page: 5
											}


											function pageselectCallback(page_index, jq){
												var items_per_page = pagination_options.items_per_page;
												var offset = page_index * items_per_page;
												var new_content = $j('#commentlistresult li.comment').slice(offset, offset + items_per_page).clone(); 
												 $j('#commentslist').empty().append(new_content);
												return false;
											}



											function initPagination() {
											
												var num_entries = $j('#commentlistresult li.comment').length;
														$j("#Pagination").pagination(num_entries, pagination_options); 
												}
												
										  
    });
			
			}); 
			
 
