(function($) {
  
  $.fn.preInput = function(text, args) {
    
    var options = $.extend({},
                           $.fn.preInput.defaults,
                           args,
                           {preInput_text: text});

    var callback = $.isFunction(options.preInput_text);

    if (!$.fn.preInput.bound_class_names[options.class_name]) {
      
      $(window).unload(function() {
        $('.' + options.class_name).val('');
      });
      
      $('form').submit(function() {
        
        /* Clear only the fields inside this particular form. */
        $(this).find('.' + options.class_name).val('');
      });
      
      /* Add the class name to the array. */
      $.fn.preInput.bound_class_names[options.class_name] = true;
    }
    
    return this.each(function() {
      
      /* Reduce method calls by saving the current jQuery object. */
      var $this = $(this);
      
      var o = $.metadata ? $.extend({}, options, $this.metadata()) : options;
      
      if ($.browser.msie && !$this.attr('defaultValue') &&
          (callback ? $this.val() != '' : $this.val() == o.preInput_text)) {
        $this.val('');
      }
      
      /* Initially place the preInput text in the field if it is empty. */
      if ($this.val() == '') {
        $this.addClass(options.class_name);

        $this.val(callback ? o.preInput_text.call(this) : o.preInput_text);
      }
      
      if (options.hide_label) {
        var label = $('label[@for=' + $this.attr('id') + ']');
        
        label.next('br').hide();
        label.hide();
      }
      
      $this.focus(function() {
        
        /* jQuery 1.1 has no hasClass(), so is() must be used instead. */
        if ($(this).is('.' + options.class_name)) {
          $(this).val('');
          $(this).removeClass(options.class_name);
        }
      });
    
      /* Make the preInput text reappear if the input is blank on blurring. */
      $this.blur(function() {
        if ($(this).val() == '') {
          $(this).addClass(options.class_name);
          
          $(this).val(callback ? o.preInput_text.call(this) : o.preInput_text);
        }
      });
    });
  };
  
  $.fn.preInput.defaults = {
    preInput_text: '',
    class_name: 'preInput',
    
    /* DEPRECATION WARNING: I am considering removing this option. */    
    hide_label: false
  };
  
  /* All the class names used are stored as keys in the following array. */
  $.fn.preInput.bound_class_names = [];
  
})(jQuery);

/*------------------------
 FRESHFOLIOS
 -----------------------*/
FreshFolios = {
	init:function(){
		//Declare Data Store Object, Set Site URL Global Var
		$.fn.ds = {}
		$.fn.ds.site_url = 'http://www.freshfolios.com';
		$.fn.ds.ssl_site_url = 'https://www.freshfolios.com';
		
		/*BIND PROMOTION CHECK*/
		$('.check_promotion').bind("click",function(e){
			FreshFolios.checkPromotion();	
		});
		
		/*BIND PREFERENCES CHECK*/
		$('#billingPreference').bind("blur",function(e){
			FreshFolios.checkPreference();	
		});
		
		/*SET PREINPUT*/
		$('.domainInput').preInput('yourdomain.com');
		
		/*IE6 CHECK*/
		FreshFolios.browserCheck();
		
		/*PORTFOLIOS*/
	 	$(".thumbnailImageWrapper").hover(
			  function () {
				$(this).find("div:first").fadeIn(300).fadeTo("slow",0.6);
			  }, 
			  function () {
				$(this).find("div:first").fadeOut(300);
			  }
		);
		
		$("a.info").hover(
			  function () {
				$(this).parent().find("span:first").fadeIn(300);
			  }, 
			  function () {
				$(this).parent().find("span:first").fadeOut(300);
			  }
		);
		
		//PURCHASE SUMMARY TOOLTIP
		$("a.cscTip").hover(
				  function () {
					$(this).parent().find("span:first").fadeIn(300);
				  }, 
				  function () {
					$(this).parent().find("span:first").fadeOut(300);
				  }
			);

		/*FAQS MENU*/
		FreshFolios.initFaqsMenu();

		/*BIND PURCHASE LINKS*/
		$('.purchase').click(function(){
				purchaseFolio($(this).attr('id'));
			});

		/*HOME BRANDING MASK*/
		setTimeout(function() { 
			if(!$.browser.msie){
				$('.homeOverlay').fadeIn(10000);
				$('#photoCredit').fadeTo('slow', 0);
			}else{
				$('.homeOverlay').show();
			}
		}, 2000);
		setTimeout(function() { 
			$('#photoCredit').removeClass("photoCreditWhite");
			$('#photoCredit').addClass("photoCreditBlack");
			$('#photoCredit').fadeTo('normal', 1);
			
		}, 4000);
		
		
	},
	ajax:function(uri, perms, callback){
		$.ajax({
			   type: "POST",
			   cache: false,
			   url: $.fn.ds.ssl_site_url+uri,
			   data: perms,
			   dataType: "json",
			   success: function(data){callback(data);},
			   error:function(){alert('error');}
			 });
		return false;
	},
	browserCheck:function(){
		if ( $.browser.msie ){
			if($.browser.version<7){
				//we dont support ie 6
				//send them to firefox
				$("body").find("*").remove();
				$("body").append("<div class='updateIE6'><img src='http://www.freshfolios.com/images/FirefoxIcon.gif'/>"+
						 	 	 "<br/><p>FreshFolios does not support Internet Explorer 6. "+
						 	 	 "We recommend upgrading to Mozilla FireFox, the world's leading web browser."+
						 	 	 "<br/><br/><a href='http://www.mozilla.com/firefox/'>Click here to Download</a></p>");
			}
		}
	},
	checkPromotion:function(){
		
		var promotion_code = $(".promotionInput").val();
		var uri = "/index.php/sslfreshfolios/checkpromotion";
		var callback = FreshFolios.checkPromotionHandler;
		var perms = "promotion_code="+promotion_code;
		FreshFolios.ajax(uri, perms, callback);
	},
	checkPromotionHandler:function(obj){
		if(obj.success==1){
			$(".promotion_check_resp").children().remove();
			$(".promotion_check_resp").append("<img src='../images/valid_code_icon.gif' title='valid'/>");
			$(".promotion_check_resp").show();
		}else{
			$(".promotionInput").val("");
			$(".promotion_check_resp").children().remove();
			$(".promotion_check_resp").append("<img src='../images/invalid_code_icon.gif' title='invalid'/>");
			$(".promotion_check_resp").show();
		}
	},
	checkPreferenceHandler:function(obj){
		if(obj.success==1){
			var upgradesTotal = $("#upgradesTotal").val();
			var promotionBillingOption = $("#promotionBillingOption").val();
			var promotionDiscount = $("#promotionDiscount").val();
			var purchaseTotal;
			
			if(Number(promotionDiscount)>0){
				if(promotionBillingOption.length>0){
					//proper billing option must be selected
					if(Number(promotionBillingOption)==$("#billingPreference").val()){
						purchaseTotal = (Number(obj.preference) + Number(upgradesTotal)-Number(promotionDiscount));
					}else{
						purchaseTotal = Number(obj.preference) + Number(upgradesTotal);
					}
				}else{
					//non specific
					purchaseTotal = (Number(obj.preference) + Number(upgradesTotal)-Number(promotionDiscount));
				}
			}else{
				purchaseTotal = Number(obj.preference) + Number(upgradesTotal);
			}
			
			
			$(".purchaseTotal").html("Order Total: $"+purchaseTotal);
		}else{
			$(".promotionInput").val("");
			$(".promotion_check_resp").children().remove();
			$(".promotion_check_resp").append("<img src='../images/invalid_code_icon.gif' title='invalid'/>");
			$(".promotion_check_resp").show();
		}
	},
	checkPreference:function(){
		var preference_code = $("#billingPreference").val();
		var uri = "/index.php/sslfreshfolios/checkpreference";
		var callback = FreshFolios.checkPreferenceHandler;
		var perms = "preference_code="+preference_code;
		FreshFolios.ajax(uri, perms, callback);
		
		
	},
	initFaqsMenu:function() {
		 $('#faqsMenu ul').hide();
		 $('#faqsMenu li a').click(
		 	function() {
		 	var checkElement = $(this).next();
		 		if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
		 			checkElement.slideUp('normal');
		 			checkElement.parent().find('a').removeClass('opened');
		 			checkElement.parent().find('a').addClass('closed');
				return false;
		 	}
		 
		 	if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
		 		$('#faqsMenu ul:visible').slideUp('normal');
				$('#faqsMenu ul:visible').parent().find('a').removeClass('opened');
				$('#faqsMenu ul:visible').parent().find('a').addClass('closed');
		 		checkElement.slideDown('normal');
				checkElement.parent().find('a').removeClass('closed');
				checkElement.parent().find('a').addClass('opened');
		 		return false;
		 	}
		 });
	 } 
}

$(function(){
	FreshFolios.init();	// FreshFolios Init
	
	
});