
var PRODUCT = (function($) {
	
	var amountToScroll = 330;
	
	var modalWindowBox = function(content) {
		return '<table class="modalwindow"><tr><td class="tl"></td><td class="tm"></td><td class="tr"></td></tr><tr><td class="ml"></td><td class="mm">' + content + '</td><td class="mr"></td></tr><tr><td class="bl"></td><td class="bm"></td><td class="br"></td></tr></table>';
	};
	
	var wdyliChoice = false;
	var wdyliEID = 'wdyli';
	var wdyliAnswerID = false;
		
	return {
		init: function() {
		
			jQuery('.related-info').click(function(){
				
				var el = jQuery(this).parent().parent().find('.related-info-box');
				
				if (jQuery(this).hasClass('related-info-shown')) {
					el.hide();
					jQuery(this).removeClass('related-info-shown');
				} else {
					
					el.show();
					el.css('z-index',300).offsetParent().css('z-index',200);
					jQuery(this).addClass('related-info-shown');
				}
				return false;
			});
			
			PRODUCT.wdyli.init();
			
			jQuery('.seeall a').click(function(){
				
				 //Get the screen height and width  
		         var maskHeight = jQuery(document).height();  
		         var maskWidth = jQuery(window).width();  
		         var bg_overlay = jQuery(jQuery('<div class="ui-widget-overlay"></div>').hide());
		         
		         var ap = jQuery('#all-products').clone(); // detach
		                  
		         
		         //Set height and width to mask to fill up the whole screen  
		         jQuery(bg_overlay).css({'width':maskWidth,'height':maskHeight,'z-index': 140}); 
		         
		         jQuery('body').append(bg_overlay).append(ap);  
		         var winW = jQuery(window).width();
		         jQuery(ap).css('left', winW/2-jQuery(ap).width()/2);  
		                
		         jQuery(bg_overlay).fadeIn(400);
		         jQuery(ap).fadeIn(400); 
		         
		         jQuery('.closeAllProducts').unbind('click');
		         jQuery('.closeAllProducts').click(function(){
					jQuery(this).parent().parent().parent().parent().fadeOut();
					jQuery('.ui-widget-overlay').fadeOut(400).remove();
					return false;
				 });
		         
				return false;
			});
			
			
			jQuery('.product-features').accordion({
				active: jQuery('h3.rp-head').length - 1,
				clearStyle: true,
				header: 'h3.rp-head',
				event: 'click'
			});
			
			jQuery('.enlarge-me').each(function(idx,el){
				var div = jQuery('<div></div>');
				jQuery(div).addClass('indicator').css({ 'height': jQuery(el).height()}).mouseenter(function(ev){
					jQuery(this).addClass('indicator-on');
				}).mouseleave(function(ev){
					jQuery(this).removeClass('indicator-on');
				}).appendTo(el);
			});
			
			jQuery('.enlarge-me').click(function(d){
				d.preventDefault();
				
				var item = jQuery(this);
				
				var imageHTML = '<div class="boptions"><a class="closemw" href="#">Close</a></div>';
				imageHTML += '<img src="'+item.attr('href')+'" alt="'+item.attr('title')+'" />';
				
				
				var theBox = jQuery(modalWindowBox(imageHTML));
				
				var maskHeight = jQuery(document).height();  
		        var maskWidth = jQuery(window).width();  
		        var bg_overlay = jQuery(jQuery('<div class="ui-widget-overlay"></div>').hide());
		        jQuery(bg_overlay).css({'width':maskWidth,'height':maskHeight,'z-index': 140}); 
		         
		        jQuery('body').append(bg_overlay).append(theBox);
		        var winW = jQuery(window).width();
		        jQuery(theBox).css('left', winW/2-jQuery(theBox).width()/2);  
		                
		        //transition effect       
		        jQuery(bg_overlay).fadeIn(400);
		        jQuery(theBox).fadeIn(400);   
		        
		        jQuery('.closemw').click(function(){
		        	jQuery(theBox).fadeOut(400);
					jQuery(bg_overlay).fadeOut(400).remove();
					return false;
		        });
				
			});
			
			PRODUCT.comment.init();
			
		},
		
		comment: {
			init: function() {
				$('#tx_comments_pi1_type_2, #tx_comments_pi1_type_1').click(this.check);
				this.check();
			},
			check: function() {
				if ($('#tx_comments_pi1_type_2').is(':checked')) {
					if (!$('#rightNowMsg').is(':visible')) {
						$('#rightNowMsg').slideDown();
					}
				} else if ($('#rightNowMsg').is(':visible')) {
					$('#rightNowMsg').slideUp();
				}
			}
		},
		
		wdyli: { 
			
			init: function() {
				if ($('#wdyli-poll-id').length > 0) {
					PRODUCT.wdyli.load();
					jQuery('.wdyli-button').click(PRODUCT.wdyli.submit);
					//jQuery('#wdyli-reset').live('click',PRODUCT.wdyli.reset);
				}
			},
			
			load: function() {
				var poll_id = jQuery('#wdyli-poll-id').attr('value');
				jQuery.ajax({
					type: 'post',
					data: 'eID='+wdyliEID+'&action=load&poll='+poll_id,
					dataType: 'json',
					success: function(result,data,xhr) {
					
						if (result.status == 'success') {
							PRODUCT.wdyli.setTotals(result);
						}
						
					}
				});
			},
			
			setTotals: function(result) {
				
				jQuery('.pval').each(function(i,e){ 
					var choice_id = e.id.match(/[0-9]{1,2}/)[0];
					var result_obj = result.totals[choice_id];
					if (result_obj && result_obj.percentage > 0) {
						jQuery(e).html(result_obj.percentage);
					} else {
						jQuery(e).html(0);
					}
				});
			},
			
			reset: function() {
				
				var poll_id = jQuery('#wdyli-poll-id').attr('value');
				
				jQuery.ajax({
					type: 'post',
					data: 'eID='+wdyliEID+'&action=reset&poll='+poll_id+'&answer='+wdyliAnswerID,
					dataType: 'json',
					success: function(result,data,xhr) {
					
						if (result.status == 'success') {
							
							jQuery('.wdyli-chosen').removeClass('wdyli-chosen');
							jQuery('.wdyli-inactivelink').removeClass('wdyli-inactivelink');
							jQuery('.wdyli-link').fadeIn();
							
							wdyliChoice = false;
							wdyliAnswerID = false;
							
							jQuery('.wdyli p:first').html('Click on the buttons below to cast your vote.');
							
							PRODUCT.wdyli.setTotals(result);
							
						}
					}
				
				});
				
				return false;
			},
			
			submit: function() {
				
				var button = jQuery(this);
				
				var choice_id = jQuery(button).find('.wdyli-ib:first').attr('value');
				var poll_id = jQuery('#wdyli-poll-id').attr('value');
				
								
				if (!wdyliChoice) {
				
					jQuery('.wdyli-link').not(jQuery(button).find('.wdyli-link')).fadeOut();
					
					jQuery('.percentage').hide();
					
					jQuery.ajax({
						type: 'post',
						data: 'eID='+wdyliEID+'&choice='+choice_id+'&action=vote&poll='+poll_id+'&answer='+wdyliAnswerID,
						dataType: 'json',
						success: function(result,data,xhr) {
						
							if (result.status == 'success') {
								
								jQuery('.wdyli-chosen').removeClass('wdyli-chosen');
								jQuery('.wdyli-inactivelink').removeClass('wdyli-inactivelink');
								jQuery(button).addClass('wdyli-chosen');
								jQuery(button).find('.wdyli-link').addClass('wdyli-inactivelink');
								
								wdyliChoice = choice_id;
								wdyliAnswerID = result.answer_id;
								
								jQuery('.wdyli p:first').html('Your vote has been cast.');
								
								
								PRODUCT.wdyli.setTotals(result);
								jQuery('.percentage').fadeIn({duration: 1500});
								
							}
						}
					
					});
				}
				return false;
			}
			
		}
		
	};
	
	
		
})(jQuery);

$(document).ready(function(){ 
	PRODUCT.init();
});

