if(typeof Start=='undefined') {
    var Start = {};
}

// UTILITY FUNCTIONS

Start = Class.create();
Start.prototype = {
	initialize: function(baseurl, skinurl, loggedin) {
		this.baseurl = baseurl;
		this.skinurl = skinurl;
		this.loggedin = loggedin;
		jQuery(function() {
		jQuery(".window-close").click(function(e) {
    		e.preventDefault();
    		jQuery(".simple_overlay").hide();
    	});
		});
	},
	showLoader: function(element, loader) {
		element.hide();
		loader.show();
	},
	hideLoader: function(element, loader) {
		element.show();
		loader.hide();
	},
	addMessage: function(success, message) {
		if(success) {
			jQuery('.messages').html('<li class="success-msg"><ul><li>' + message + '</li></ul></li>');
		}
		else {
			jQuery('.messages').html('<li class="error-msg"><ul><li>' + message + '</li></ul></li>');
		}
	},
	refreshModal: function() {
		jQuery('#simplemodal-container').css('height', 'auto');
		jQuery.modal.setPosition();
		jQuery('#simplemodal-container').css('height', jQuery('#simplemodal-container').height());
	}
}

// POLL JAVASCRIPT

Start.Poll = Class.create();
/**
 * Poll Ajax
 *
 * @author      Robertson & Associates (http://www.robertsonandassociates.org.uk)
 */
Start.Poll.prototype = {
    initialize: function(url){
    	jQuery('#pollForm button').click(function(e) {
			e.preventDefault();
			start.showLoader(jQuery('#pollForm button'), jQuery('#pollForm .loader'));
			var poll_id = jQuery('#pollForm #poll_id').val();
			var vote = jQuery('input:radio[name=vote]:checked').val();
			jQuery.post( 
				url,
				"poll_id=" + poll_id + "&vote=" + vote,
				function(data){
					if(data.result) {
						// Successful
						jQuery('.block-poll').replaceWith(data.html);
					}
					else {
						// Unsuccessful
					}
				},
				"json"
			);
    	});
    }
    
};

// CART JAVASCRIPT

Start.Cart = Class.create();
/**
 * Cart Ajax
 *
 * @author      Robertson & Associates (http://www.robertsonandassociates.org.uk)
 */
Start.Cart.prototype = {
	initialize: function(){
		
	},
    productPageAddToCart: function(){
    		if (productAddToCartForm.validator.validate()) {
			start.showLoader(jQuery('.add-to-cart button'), jQuery('.add-to-cart .loader'));
			var data = jQuery('#product_addtocart_form').serialize();
			jQuery.post( 
					start.baseurl + 'startajax/cart/add/',
					data,
					function(data){
						if(data.result) {
							// Successful
							start.addMessage(true, data.message);
							jQuery('.block-cart').replaceWith(data.html);
							start.hideLoader(jQuery('.add-to-cart button'), jQuery('.add-to-cart .loader'));
							jQuery('.top-link-cart').html(data.top_link);
						}
						else {
							// Unsuccessful
							
							start.addMessage(false, data.message);
							start.hideLoader(jQuery('.add-to-cart button'), jQuery('.add-to-cart .loader'));
						}
					},
					"json"
				);
    		}
    },
    listAddToCart: function(id){
		start.showLoader(jQuery('.item-' + id + ' button'), jQuery('.item-' + id + ' .loader'));
		var data = 'product=' + id + '&qty=1';
		jQuery.post( 
				start.baseurl + 'startajax/cart/add/',
				data,
				function(data){
					if(data.result) {
						// Successful
						start.addMessage(true, data.message);
						jQuery('.block-cart').replaceWith(data.html);
						start.hideLoader(jQuery('.item-' + id + ' button'), jQuery('.item-' + id + ' .loader'));
						jQuery('.top-link-cart').html(data.top_link);
					}
					else if(data.message = 'configurable') {
						start.hideLoader(jQuery('.item-' + id + ' button'), jQuery('.item-' + id + ' .loader'));
						jQuery('<div></div>').hide().html(data.html).modal();
						var spConfig = new Product.Config(jsonConfig);
					}
					else {
						// Unsuccessful
						jQuery('.block-cart').replaceWith(data.html);
						start.addMessage(false, data.message);
						start.hideLoader(jQuery('.item-' + id + ' button'), jQuery('.item-' + id + ' .loader'));
					}
				},
				"json"
			);

    },
    optionsAddToCart: function(id){
    	var productAddToCartForm = new VarienForm('optionsaddtocart');
    	if(productAddToCartForm.validator.validate()) {
    	start.showLoader(jQuery('#simplemodal-container .btn-cart'), jQuery('#simplemodal-container .loader'));
    	data = jQuery('.super-attribute-select').serialize() + '&product=' + id + '&qty=' + jQuery('#simplemodal-container #qty').val();
    	jQuery.post( 
				start.baseurl + 'startajax/cart/add/',
				data,
				function(data){
					if(data.result) {
						// Successful
						start.hideLoader(jQuery('#simplemodal-container .btn-cart'), jQuery('#simplemodal-container .loader'));
						start.addMessage(true, data.message);
						jQuery.modal.close();
						jQuery('.block-cart').replaceWith(data.html);
						jQuery('.top-link-cart').html(data.top_link);
					}
					else {
						// Unsuccessful
						jQuery('.block-cart').replaceWith(data.html);
						start.addMessage(false, data.message);
						start.hideLoader(jQuery('.add-to-cart button'), jQuery('.add-to-cart .loader'));
					}
				},
				"json"
			);
    	}
    },
    remove: function(id){
    	start.showLoader(jQuery('.item-' + id + ' .btn-remove'), jQuery('.item-' + id + ' .loader'));
		jQuery.post( 
				start.baseurl + 'startajax/cart/delete/',
				'id=' + id,
				function(data){
					if(data.result) {
						// Successful
						start.addMessage(true, data.message);
						jQuery('.block-cart').replaceWith(data.html);
						jQuery('.cart').replaceWith(data.bigcart);
						start.hideLoader(jQuery('.item-' + id + ' .btn-remove'), jQuery('.item-' + id + ' .loader'));
						jQuery('.top-link-cart').html(data.top_link);
					}
					else {
						// Unsuccessful
						jQuery('.block-cart').replaceWith(data.html);
						start.addMessage(false, data.message);
						start.hideLoader(jQuery('.item-' + id + ' .btn-remove'), jQuery('.item-' + id + ' .loader'));
					}
				},
				"json"
			);
    }
    
};

//CUSTOMER JAVASCRIPT

Start.Customer = Class.create();
/**
 * Customer Ajax
 *
 * @author      Robertson & Associates (http://www.robertsonandassociates.org.uk)
 */
Start.Customer.prototype = {
    initialize: function(){
    	jQuery(".top-link-login").click(function(e) {
    		e.preventDefault();
    		jQuery("#login-overlay").modal({maxWidth: 800});
    		start.refreshModal();
    	});
    	jQuery("#login-overlay .account-login #send2").click(function(){
    		start.showLoader(jQuery('.simplemodal-container .account-login'), jQuery('.simplemodal-container .loader'));
    	});
    	jQuery("#login-overlay .account-login #send1").click(function(e) {
    		e.preventDefault();
    		start.showLoader(jQuery('#login-overlay .account-login'), jQuery('#login-overlay .loader'));
    		jQuery.post( 
    				start.baseurl + 'startajax/customer/register/',
    				function(data){
    					jQuery('#login-overlay .account-login').html(data);
    					start.hideLoader(jQuery('#login-overlay .account-login'), jQuery('#login-overlay .loader'));
    					start.refreshModal();
    				}
    			);
    	});
    },
    backToLogin: function(){
    	start.showLoader(jQuery('#login-overlay .account-login'), jQuery('#login-overlay .loader'));
    	jQuery.post( 
				start.baseurl + 'startajax/customer/login/',
				function(data){
					jQuery('#login-overlay .account-login').html(data);
					start.hideLoader(jQuery('#login-overlay .account-login'), jQuery('#login-overlay .loader'));
					start.refreshModal();
				}
			);
    }
    
};

//SENDFRIEND JAVASCRIPT

Start.SendFriend = Class.create();
/**
 * Send Friend Ajax
 *
 * @author      Robertson & Associates (http://www.robertsonandassociates.org.uk)
 */
Start.SendFriend.prototype = {
    initialize: function(){
    	jQuery("p.email-friend a").click(function(e) {
    		e.preventDefault();
    		if(start.loggedin !== '') {
    		start.showLoader(jQuery('#sendfriend-overlay .container'), jQuery('#sendfriend-overlay .loader'));
    		jQuery("#sendfriend-overlay").modal();
    		start.refreshModal();
    		id = jQuery('input[name="product"]').val();
    		
    			var url = 'startajax/sendfriend/index/';
    			jQuery.post( 
    	    			start.baseurl + url,
    	    			'id=' + id,
    	    			function(data){
    	    				start.hideLoader(jQuery('#sendfriend-overlay .container'), jQuery('#sendfriend-overlay .loader'));
    	    				jQuery('#sendfriend-overlay .container').html(data);
    	    				start.refreshModal();
    	    			}
    	    		);
    		}
    		else {
    			jQuery("#login-overlay").modal({maxWidth: 800});
        		start.refreshModal();
        		return;
    		}
    		
    	});
    }
};

//REVIEW JAVASCRIPT

Start.Review = Class.create();
/**
 * Review Ajax
 *
 * @author      Robertson & Associates (http://www.robertsonandassociates.org.uk)
 */
Start.Review.prototype = {
    initialize: function(){
    	
    },
    review: function(id){

    		if(start.loggedin !== '') {
    		start.showLoader(jQuery('#review-overlay .container'), jQuery('#review-overlay .loader'));
    		jQuery("#review-overlay").modal({maxWidth: 800});
    		start.refreshModal();
    			var url = 'startajax/review/index/';
    			jQuery.post( 
    	    			start.baseurl + url,
    	    			'id=' + id,
    	    			function(data){
    	    				start.hideLoader(jQuery('#review-overlay .container'), jQuery('#review-overlay .loader'));
    	    				jQuery('#review-overlay .container').html(data);
    	    				start.refreshModal();
    	    			}
    	    		);
    		}
    		else {
    			jQuery("#login-overlay").modal({maxWidth: 800});
        		start.refreshModal();
        		return;
    		}
    }
};
    
//TAG JAVASCRIPT

Start.Tag = Class.create();
/**
 * Tag Ajax
 *
 * @author      Robertson & Associates (http://www.robertsonandassociates.org.uk)
 */
Start.Tag.prototype = {
    initialize: function(){
    	
    },
    addTag: function(){
    	if(addTagFormJs.validator.validate()) {
    	start.showLoader(jQuery('#addTagForm button'), jQuery('#addTagForm .loader'));
    	data = jQuery('#addTagForm').serialize();
    	product = jQuery('input[name="product"]').val();
    	jQuery.get( 
    		start.baseurl + 'startajax/tag/add/',
    		data + '&product=' + product,
    		function(data){
    			if(data.result) {
    				start.hideLoader(jQuery('#addTagForm button'), jQuery('#addTagForm .loader'));
    				jQuery('.box-tags').html(data.html);
    				start.addMessage(true, data.message);
    			}
    			else {
    				start.addMessage(false, data.message);
    			}
    		}
    	);
    	}
    }
};    

//WISHLIST JAVASCRIPT

Start.Wishlist = Class.create();
/**
 * Wishlist Ajax
 *
 * @author      Robertson & Associates (http://www.robertsonandassociates.org.uk)
 */
Start.Wishlist.prototype = {
    initialize: function(){
    	
    },
    addToWishlist: function(id){
    	start.showLoader(jQuery('.wishlist-link-' + id), jQuery('.wishlist-loader-' + id));
    	jQuery.get( 
        		start.baseurl + 'startajax/wishlist/add/',
        		'product=' + id,
        		function(data){
        			if(data.result) {
        				start.hideLoader(jQuery('.wishlist-link-' + id), jQuery('.wishlist-loader-' + id));
        				if(jQuery('.block-wishlist')) {
        					jQuery('.block-wishlist').replaceWith(data.html);
        				}
        				jQuery('.quick-access .links a[href*="wishlist"]').html(data.top_link);
        				start.addMessage(true, data.message);
        			}
        			else {
        				start.addMessage(false, data.message);
        			}
        		}
        	);
    }
}; 

jQuery(function () {
	  jQuery('.bubbleInfo').each(function () {
	    // options
	    var distance = 20;
	    var time = 150;
	    var hideDelay = 200;

	    var hideDelayTimer = null;

	    // tracker
	    var beingShown = false;
	    var shown = false;
	    
	    var trigger = jQuery('.trigger', this);
	    var popup = jQuery('.popup', this).css('opacity', 0);

	    // set the mouseover and mouseout on both element
	    jQuery([trigger.get(0), popup.get(0)]).mouseover(function () {
	      // stops the hide event if we move from the trigger to the popup element
	      if (hideDelayTimer) clearTimeout(hideDelayTimer);

	      // don't trigger the animation again if we're being shown, or already visible
	      if (beingShown || shown) {
	        return;
	      } else {
	        beingShown = true;

	        // reset position of popup box
	        popup.css({
	          top: 0,
	          left: 0,
	          display: 'block' // brings the popup back in to view
	        })

	        // (we're using chaining on the popup) now animate it's opacity and position
	        .animate({
	          top: '-=' + distance + 'px',
	          opacity: 1
	        }, time, 'swing', function() {
	          // once the animation is complete, set the tracker variables
	          beingShown = false;
	          shown = true;
	        });
	      }
	    }).mouseout(function () {
	      // reset the timer if we get fired again - avoids double animations
	      if (hideDelayTimer) clearTimeout(hideDelayTimer);
	      
	      // store the timer so that it can be cleared in the mouseover if required
	      hideDelayTimer = setTimeout(function () {
	        hideDelayTimer = null;
	        popup.animate({
	          top: '-=' + distance + 'px',
	          opacity: 0
	        }, time, 'swing', function () {
	          // once the animate is complete, set the tracker variables
	          shown = false;
	          // hide the popup entirely after the effect (opacity alone doesn't do the job)
	          popup.css('display', 'none');
	        });
	      }, hideDelay);
	    });
	  });
	});
