/*
 *
 *
 *	Review Expand Link Creator
 *	Description: This JS function is used to create the short lines of stuff.
 *
 *
 *
 */
function makeQuip() 
{

	//var reviewText = jQuery('#review-results .review-contents');
	var QUIP_LENGTH = 80;
	jQuery('#review-results .review-contents').each(function() {
		if(jQuery.trim(jQuery(this).text()).length > QUIP_LENGTH)
		{
			var newContent = document.createElement('div');
		
			var newText = document.createElement('p');
			jQuery(newText).addClass('quip');
			
			var expandLink = document.createElement('a');
			jQuery(expandLink).addClass('expand');
			jQuery(expandLink).text('[ Expand ]');
			
			var expandedText = document.createElement('div');
			jQuery(expandedText).addClass('review-expanded');
			jQuery(expandedText).html(jQuery(this).html());
			
			var shortText = jQuery.trim(jQuery(this).text()).substring(0,QUIP_LENGTH);
			
			jQuery(newText).append(shortText);
			jQuery(newText).append('... ');
			jQuery(newText).append(expandLink);
			
			jQuery(expandLink).click(function() {
			
				jQuery(newText).hide();
				jQuery(expandedText).show('slow');
			
			});
			
			jQuery(newContent).append(newText);
			jQuery(newContent).append(expandedText);
			
			jQuery(this).replaceWith(newContent);
			
			jQuery(expandedText).hide();
		}
	
	});

}


/**
 *
 *
 *  Various functions and such
 *
 *
 *
 */

 
ZeroClipboard.setMoviePath( 'http://tee-junction.com/teedirectory/wp-content/themes/teedirectory/js/ZeroClipboard.swf' );
var clip, clipButton;  

jQuery(document).ready(function() {
	jQuery('#cs_expiry').datepicker({minDate: 0, maxDate: '+12M +10D'});
	jQuery('#ds_expiry').datepicker({minDate: 0, maxDate: '+12M +10D'});
});


/*
 *
 *
 *	Pagination Implementation
 *
 *
 *
 *
 */
function reviewpageselectCallback(page_index, jq){
	var items_per_page = 5;
	var max_elem = Math.min((page_index + 1) * items_per_page, jQuery('div#reviews-list ol.commentlist li.comment').length);
	var newcontent = '<ol class="commentlist">';
	
	// Iterate through a selection of the content and build an HTML string
	for(var i = (page_index * items_per_page); i < max_elem; i++)
	{
		newcontent += '<li class="comment">' + jQuery('div#reviews-list ol.commentlist li.comment:eq('+i+')').html() + '</li>';
	}
	
	newcontent += '</ol>';
	
	// Replace old content with new content
	jQuery('#review-results').empty().append(newcontent);
	
	makeQuip();
	
	// Prevent click eventpropagation
	return false;
}

function dealpageselectCallback(page_index, jq){
	var items_per_page = 4;
	var max_elem = Math.min((page_index + 1) * items_per_page, jQuery('div#deals-list ol.commentlist li.comment').length);
	var newcontent = '<ol class="commentlist">';
	
	// Iterate through a selection of the content and build an HTML string
	for(var i = (page_index * items_per_page); i < max_elem; i++)
	{
		newcontent += '<li class="comment">' + jQuery('div#deals-list ol.commentlist li.comment:eq('+i+')').html() + '</li>';
	}
	
	newcontent += '</ol>';
	
	// Replace old content with new content
	jQuery('#deals-results').empty().append(newcontent);
	
	jQuery('#deals-results a.new-window').each(function(){
		jQuery(this).click(function(e){
			e.preventDefault();
			window.open(this.href);
		});
	});
	
	// Prevent click eventpropagation
	return false;
}

function couponpageselectCallback(page_index, jq){
	var items_per_page = 5;
	var max_elem = Math.min((page_index + 1) * items_per_page, jQuery('div#coupons-list ol.commentlist li.comment').length);
	var newcontent = '<ol class="commentlist">';
	
	// Iterate through a selection of the content and build an HTML string
	for(var i = (page_index * items_per_page); i < max_elem; i++)
	{
		newcontent += '<li class="comment">' + jQuery('div#coupons-list ol.commentlist li.comment:eq('+i+')').html() + '</li>';
	}
	
	newcontent += '</ol>';
	
	// Replace old content with new content
	jQuery('#coupons-results').empty().append(newcontent);
	
	clipify();
	
	// Prevent click eventpropagation
	return false;
}

function clipify()
{	
	jQuery('#coupons-results').find('.coupon-name').each(function() {
		var coupon = jQuery(this).find('strong');
		var couponCode = jQuery(coupon).text();
		var couponToolTip = document.createElement('div');
		var link = jQuery('.store-link a').attr('href');
		
		if(window.hidden)
		{
			var showButton = document.createElement('div');
			jQuery(showButton).addClass('show-button');
			jQuery(showButton).html('Show Code &amp; Open Site');
			jQuery(this).before(showButton);
			jQuery(showButton).mouseover(function() {
				if(clipButton) {
					clipButton.destroy();
				}
				clipButton = new ZeroClipboard.Client();
				clipButton.setText(couponCode);
				clipButton.setHandCursor(true);
				clipButton.glue(showButton);
				
				clipButton.addEventListener('onComplete', function() {
					clipOnComplete(coupon, couponCode, couponToolTip, link);	
				});
			
				clipButton.addEventListener('mouseOver', function() {
					clipMouseOver(coupon, couponCode, couponToolTip, link);
				});
				
				clipButton.addEventListener('mouseOut', function() { 
					jQuery(couponToolTip).fadeOut(200);
				});
			});
		}
		
		jQuery(this).mouseover(function() {
			
			if(clip) {
				clip.destroy();
			}
			
			clip = new ZeroClipboard.Client();
			
			clip.setText(couponCode);
			clip.setHandCursor(true);
			clip.glue(this);
			
			clip.addEventListener('onComplete', function() {
				clipOnComplete(coupon, couponCode, couponToolTip, link);	
			});
		
			clip.addEventListener('mouseOver', function() {
				clipMouseOver(coupon, couponCode, couponToolTip, link);
			});
			
			clip.addEventListener('mouseOut', function() { 
				jQuery(couponToolTip).fadeOut(200);
			});
			
		});
	
	});
	
	coverAll();
}

function clipOnComplete(coupon, couponCode, couponToolTip, link) {
	jQuery(couponToolTip).css({
		fontSize: 	'12px',
		postition: 	'absolute',
		top: 		'26px',
		left: 		'0px',
		fontWeight: 'normal'
	});
	jQuery(couponToolTip).text('This code has been copied, and the store has opened in a new window behind this one!');
	coupon.after(couponToolTip);
	jQuery(couponToolTip).addClass('coupon-tool-tip');
	jQuery(couponToolTip).show();
	revealAll();
	var merchantWindow = window.open(link, '_blank', 'toolbar=1,location=1,directories=1,scrollbars=1,resizable=1,status=1,menubar=1');
	if (typeof merchantWindow === "object")
		merchantWindow.blur();
}

function clipMouseOver(coupon, couponCode, couponToolTip, link) {
	jQuery(couponToolTip).text('Click to copy code and see store, then simply paste into the coupon field');
	coupon.after(couponToolTip);
	jQuery(couponToolTip).addClass('coupon-tool-tip');
	
	jQuery(couponToolTip).hide();
	jQuery(couponToolTip).css({
		fontSize: 	'12px',
		position: 	'absolute',
		top: 		'26px',
		left: 		'0px',
		padding:	'2px 4px',
		display: 	'none',
		width:		'300px',
		fontWeight: 'normal'
	}).fadeIn(200);
}

function gallerypageselectCallback(page_index, jq){
	var items_per_page = 4;
	var max_elem = Math.min((page_index + 1) * items_per_page, gallery.length);
	var linkTo;
	var newcontent = '<ul class="gallerylist">';
	
	// Iterate through a selection of the content and build an HTML string
	for(var i = (page_index * items_per_page); i < max_elem; i++)
	{  
		if(gallery.eq(i).children('.gallery-caption').text() != '')
			linkTo = gallery.eq(i).children('.gallery-caption').text();
		else
			linkTo = gallery.eq(i).children('.gallery-icon').children('a').eq(0).attr('href');
			
		newcontent += '<li class="gallery-item"><a href="' + linkTo + '"><img src="' + gallery.eq(i).children('.gallery-icon').children('a').children('img').eq(0).attr('src') + '" /></a></li>';
	}
	
	newcontent += '</ul>';
	
	// Replace old content with new content
	jQuery('#gallery-results').empty().append(newcontent);
	
	// Prevent click eventpropagation
	return false;
}

function initPagination() {
	var review_num_entries = jQuery('div#reviews-list ol.commentlist li.comment').length;
	var deal_num_entries = jQuery('div#deals-list ol.commentlist li.comment').length;
	var coupon_num_entries = jQuery('div#coupons-list ol.commentlist li.comment').length;
	var gallery_num_entries = gallery.length;
	
	if(review_num_entries <= 5)
		jQuery("#review-pagination").hide();
		
	if(deal_num_entries <= 4)
		jQuery("#deals-pagination").hide();
		
	if(coupon_num_entries <= 5)
		jQuery("#coupons-pagination").hide();
	
	if(gallery_num_entries <= 5)
		jQuery("#gallery-pagination").hide();
	
	jQuery("#review-pagination").pagination(review_num_entries, {
		num_edge_entries: 2,
		num_display_entries: 8,
		callback: reviewpageselectCallback,
		items_per_page:5,
		prev_text: 'Previous 5',
		next_text: 'Next 5'
	});
	jQuery("#deals-pagination").pagination(deal_num_entries, {
		num_edge_entries: 2,
		num_display_entries: 8,
		callback: dealpageselectCallback,
		items_per_page:4,
		prev_text: 'Previous 4',
		next_text: 'Next 4'
	});
	jQuery("#coupons-pagination").pagination(coupon_num_entries, {
		num_edge_entries: 2,
		num_display_entries: 8,
		callback: couponpageselectCallback,
		items_per_page:5,
		prev_text: 'Previous 5',
		next_text: 'Next 5'
	});
	jQuery("#gallery-pagination").pagination(gallery_num_entries, {
		num_edge_entries: 2,
		num_display_entries: 8,
		callback: gallerypageselectCallback,
		items_per_page:5,
		prev_text: 'Prev 4',
		next_text: 'Next 4'
	});
}

var gallery;

jQuery(document).ready(function(){ 
	jQuery('#reviews-list').hide(); 
	jQuery('#deals-list').hide();
	jQuery('#coupons-list').hide();  
	gallery = jQuery('.tshirtgallery ul div.gallery dl.gallery-item').clone();
	jQuery('.tshirtgallery ul div.gallery').remove();
	initPagination();
});