/* javascript function to update form field
 *  field		form field that is being counted
 *  count		form field that will show characters left
 *  maxchars 	maximum number of characters
*/
function characterCount(field, count, maxchars) {
	if (field.value.length > maxchars) {
		field.value = field.value.substring(0, maxchars);
		alert("Error:\n\n- You are only allowed to enter up to "+maxchars+" characters.");
	} else {
		count.value = maxchars - field.value.length;
	}
}


$(document).ready(function() {
	
	 $('#reviewAddLink').click(function() {
    $(document).find('#reviewsContainer').hide();
		$(document).find('#reviewsWriteContainer').show();
		return false;
	});
	$('#reviewsListBoxLink').click(function() {
    $(document).find('#reviewsContainer').show();
		$(document).find('#reviewsWriteContainer').hide();
		return false;
	});   
  
  /*$('#infoListBox > ul').tabs(); */
  var $tabs = $('#infoListBox > ul').tabs({
	  select: function(event,ui) {
	  	var y = $('#logoWrapper').outerHeight()+$('#detailsBox').outerHeight()+$('#nameBox').outerHeight()+$('#categoryPathBox').outerHeight()+10;
		window.scrollTo(0,y);
	  }
  }); // first tab selected
  if($("#reviews_text").hasClass("inputErr") ||
     $("#reviews_rating_span").hasClass("textErr") ||
     $("#customers_name").hasClass("inputErr") ){
      var reviewsNumber = -1;
       var amount = $('ul#infoList li ').length;
       if (amount > 0 ) {
        var i = 0;
        while (i < amount && reviewsNumber == -1) {
         if ($('ul#infoList li:nth-child('+(i+1)+') a').attr('href') == '#reviewsListBox') {
          reviewsNumber = i;
         } else {
          i += 1;
         }
        }
       }    
    $tabs.tabs('select',reviewsNumber);
    $('#reviewAddLink').click();
  } 
  
	
	if ($('#allPhotoBox a').size() <= 0) {
		$('#principalPhotoBox a').lightbox();
	} else {
		$('#allPhotoBox a').lightbox();
	
		$('#principalPhotoBox a').click(function() {
			var href = $(this).attr('href');
			$('#allPhotoBox a').each(function() {
				if ($(this).attr('href') == href) {
					$(this).click();
					return;
				}	
			});
			return false;
		});
	}
	

	$('.tooltip_desc').each(function() {
		$(this).simpletip({
			content: $(this).find('.tooltip_text').text(),
			position: 'right',
			offset: [5,0]
		});
	});
	
	$('#technicaldesc').click(function() {
	  if($('#techdesc').text() == ''){
        $('.loaderImg').show("fast"); 
        var id = $('#orderBox input[name="products_id"]').val();
    		$.ajax({
    				type: 'POST',
    				data :'ajax_command=getTechnicalDescription&products_id='+id,
    				//url : 'index.php?main_page=product_info&products_id='+id,
    				url : 'ajax.php',
    				dataType : 'text',
    				success : function(xmlData) {
    					if ($(xmlData).find('result').text() != 'err') {
    						//if ($(xmlData).find('code')) {
    							$('#techdesc').html($(xmlData).find('result').text());
    						//}
    							$('.tooltip_desc').each(function() {
                		$(this).simpletip({
                			content: $(this).find('.tooltip_text').text(),
                			position: 'right',
                			offset: [5,0]
                		});
                	});
    					}
    				},
    				complete : function() {
              $('.loaderImg').hide(); 
              $('#techdesc').show();
            }
    			});
      }
	});	
	
});
