
// Browser check

var minMSIEVersion = 8;
var uBrowser;
var uVersion;

jQuery.each(jQuery.browser, function(i, val)
{
	if ((i == "msie") && (val == true))
	{
		uBrowser = i;
	}
	else if ((i == "mozilla") && (val == true))
	{
		uBrowser = i;
	}
	else if ((i == "safari") && (val == true))
	{
		uBrowser = i;
	}
	else if ((i == "opera") && (val == true))
	{
		uBrowser = i;
	}

	uVersion = new Number(jQuery.browser.version.substr(0,3));
});



/* --------------------------------------------------------------
 * Design solution page
 */

var DesignSolution = function($){

    function addProduct(e)
    {
			if($(e).children('a').attr("class")=="remove-solution"){
				removeProduct(e);
			}else{

			$(e).children('a').html('Remove from solution');
			$(e).children('a').addClass('remove-solution'); 
			$(e).children('a').removeClass('add-to-solutions'); 


			$('#addproduct img').attr("src","/images/buttons/remove-this-h.png");
			$('#removeproduct img').attr("src","/images/buttons/remove-this-h.png");

			// Update the inline product

			if ((uBrowser == 'safari') || (uBrowser == 'mozilla') || (uBrowser == 'opera') || ((uBrowser == 'msie') && (uVersion > 7)))
			{
				$(e).parent('ul').parent('div').fadeTo(300, 0.5);
			}
			else if ((uBrowser == 'msie') && (uVersion == 7))
			{
				$(e).parent('ul').parent('div').children('span.img-wrapper').fadeTo(300, 0.5);		    		    
				$(e).parent('ul').parent('div').children('h3').children('a').css('color','#90adcf');
				$(e).children('a').css('color','#aaa');
				$(e).parent('ul').children('li.quickview').children('a').css('color','#aaa');
			}
			else if ((uBrowser == 'msie') && (uVersion < 7))
			{
				$(e).parent('ul').parent('div').children('span.img-wrapper').fadeTo(300, 0.5);
				$(e).parent('ul').parent('div').children('h3').children('a').css('color','#90adcf');
				$(e).children('a').css('color','#aaa');
				$(e).parent('ul').children('li.quickview').children('a').css('color','#aaa');	
			}
			
		}
	};
  
    
    function removeProduct(e)
    {

		$(e).children('a').html('Add to my solution');
		$(e).children('a').addClass('add-to-solutions'); 
		$(e).children('a').removeClass('remove-solution');
		$(e).parent('ul').parent('div').removeClass('insolution');

		$('#removeproduct img').attr("src","/images/buttons/add-solution.png");
		$('#addproduct img').attr("src","/images/buttons/add-solution.png");
		
		// Update the inline product

		if ((uBrowser == 'safari') || (uBrowser == 'mozilla') || (uBrowser == 'opera') || ((uBrowser == 'msie') && (uVersion > 7)))
		{
			$(e).parent('ul').parent('div').fadeTo('fast',1);
		}
		else if ((uBrowser == 'msie') && (uVersion == 7))
		{
			$(e).parent('ul').parent('div').children('span.img-wrapper').fadeTo('fast',1);
			$(e).parent('ul').parent('div').children('h3').children('a').css('color','#225b9f');
			$(e).children('a').css('color','#555');
			$(e).parent('ul').children('li.quickview').children('a').css('color','#555');
		}
		else if ((uBrowser == 'msie') && (uVersion < 7))
		{
			$(e).parent('ul').parent('div').children('span.img-wrapper').fadeTo('fast',1);
			$(e).parent('ul').parent('div').children('h3').children('a').css('color','#225b9f');
			$(e).children('a').css('color','#555');
			$(e).parent('ul').children('li.quickview').children('a').css('color','#555');
		}
	};
        
	function updateEvent(){
		// radiobox
		
		$(':radio').each(function(i){

			$(this).click(function(){
				
				subUpdateEvent();
				$(':radio').each(function(i){
					$(this).click(function(){
						
						subUpdateEvent();
					});
				});
				$(this).unbind();
			});
			
		});
	};
	
	function subUpdateEvent(){
		$('.design-options').fadeOut(200, 
			function(){
				
					  
				  if (document.getElementById("design-count").innerHTML != "0") {
					  		$('.inner-box').find('p').show();
					  		$('.design-solution').removeClass('zero-products');
					  }else{
					  
					  	$('.inner-box').find('p').hide();
					  }
					  $('.step1').hide(); 
					  $('.step2').show();   
					  $('.reset-button').show();
				  				
			}
		);
	};
	


	//--
	function reviewSolution()
	{
		var img1 = new Image();
		var img2 = new Image();
		img1.src = '/images/buttons/review.png';
		img2.src = '/images/buttons/close.png';
				if($('.solution-box').attr("class")=="solution-box collapsed"){
					openReviewBox();
				}else{
					closeReviewBox();
				}
	};
	
	/**
	 *
	 */
	function openReviewBox()
	{
        $('a.review-button').html('<span></span><img src="/images/buttons/close.png" alt="CLOSE" width="55" height="26" />');
        $('a.review-button').addClass('review-button-close');

		if(document.getElementById("design-count")!=null){
			var s = document.getElementById("design-count").innerHTML;
			s = s.trim();
			$("#design-count").html(s);	        
			if (document.getElementById("design-count").innerHTML != "0") {
				$('.solution-box').removeClass('collapsed').addClass('expanded');
				$('.product-review-box').slideDown('450');
				$('.solution-box-b').slideDown('450');
				$('.design-solution').removeClass('zero-products');
			}else{
					$('.design-solution').addClass('zero-products');
			}

			if ($("#design-count").length>0){
				$(".revise-this-design").show();
			}
		}

		
	}
	
	/**
	 * 
	 */
	function closeReviewBox()
	{
	    $('a.review-button').html('<span></span><img src="/images/buttons/review.png" alt="REVIEW" width="55" height="26" />');
		$('a.review-button').removeClass('review-button-close');
		$('.solution-box').removeClass('expanded').addClass('collapsed');
		$('.product-review-box').slideUp('450');
		$('.solution-box-b').slideUp('450');
		
		// Fix for an IE6 "HasLayout" issue.
		if(document.getElementById("design-count")!=null){
			var s = document.getElementById("design-count").innerHTML;
			s = s.trim();
			$("#design-count").html(s);			

			if (document.getElementById("design-count").innerHTML != "0") {
				$('.design-solution').removeClass('zero-products');
			}else{
				$('.design-solution').addClass('zero-products');
				if ($("#design-count").length>0){
					$("#widget2").hide();
					$("#widget1").show();
				}				
			}

			if ($("#design-count").length>0){
				$('.revise-this-design').hide();
				$('.design-solution').addClass('zero-products');
				$(".revise-this-design").hide();
			}
		}


	};

	//-- 
	function sliderEvent(){
	
		$('#slider1').slider( { steps:3, range:false, change: function(e,ui) {
		
		sendDesignASolutionFormValuesToServer(ui,"slider1");
		}});
		
		$('#slider2').slider( { steps:3, range:false, change: function(e,ui) {
		
		sendDesignASolutionFormValuesToServer(ui,"slider2");
		}});
		
		$('#slider3').slider( { steps:3, range:false, change: function(e,ui) {
		
		sendDesignASolutionFormValuesToServer(ui,"slider3");
		}});		
	};

	/**
	 * Help Tips Events
	 */
	 function helpHover(){


		$('.design-solution a.help').hover(
			function() {        
			},
			function() {
			}
		);
	 };
	 
	 function quickViewHover(){
	 	$('.quickview a').hoverIntent({	 
            sensitivity: 7,
            interval: 175,
            over: function() {$(this).siblings('.balloon').fadeIn('100');},
            timeout: 100,
            out:  function() {$(this).siblings('.balloon').fadeOut('250');}                
        });
    };

	
	 /**
	  * Share Event
	  */
	  function shareClick(){
		$('.design-solution .share-button').click(
			function() {
				if (parseInt($('div.inner-box h3 span').html()) > 0){
					$(this).html('<span></span><img src="/images/buttons/share-disabled.png" alt="SHARE" width="50" height="26" />');
					$('select').hide();
					/* The share_window should start appearing AFTER site_overlay have finished the animation - currently they both fade in in the same time */
					$('#site_overlay').css({filter:'alpha(opacity=60)'}).fadeIn(300,
						function() {
							$('#share_window').fadeIn(300);
						}
					);
				}
				
			}
		);
	  };

	 /**
	  * Image Gallery Event
	  */
	  function imageGalleryClick(){
		$('.image-gallery').click(
			function() {
				/* The share_window should start appearing AFTER site_overlay have finished the animation - currently they both fade in in the same time */
				$('#image_gallery_site_overlay').css({filter:'alpha(opacity=60)'}).fadeIn(300,
					function() {
						$('#image_gallery_window').fadeIn(300);
					}
				);				
			}
		);
	  };

	 /**
	  * Related Products Event
	  */
	  function relatedProductsClick(){
		$('.related-products').click(
			function() {
				$("#relatedProductsTab").click();		
			}
		);
	  };

    /**
     * Cancel Event
     */	
	  function cancelClick(){
		 $('#share_window a.cancel').click(
			function() {
				$('#share_window').fadeOut(300);
				$('select').show();
				$('#site_overlay').fadeOut(300);	
				$('.design-solution .share-button').html('<span></span><img src="/images/buttons/share.png" alt="SHARE" width="50" height="26" />');
			});
	  };


    /**
     * Cancel Event
     */	
	  function imageGalleryCancelClick(){
		 $('#image_gallery_window a.cancel').click(
			function() {
				$('#image_gallery_window').fadeOut(300);
				$('select').show();
				$('#image_gallery_site_overlay').fadeOut(300);	
			});
	  };

	return {
		init : function(){
			updateEvent();
			quickViewHover();
			shareClick();
			imageGalleryClick();
			relatedProductsClick();
			cancelClick();
			imageGalleryCancelClick();
		},
		
		p_reviewSolution : function(){
			reviewSolution();
		}
		,
		
		p_sliderEvent : function(){
			sliderEvent();
		}		
		,
		
		p_removeProduct : function(e){
			removeProduct(e);
		}
		,		
		
		p_closeReviewBox : function(){
			closeReviewBox();
		}
		,				

		p_subUpdateEvent : function(){
			subUpdateEvent();
		}
		,
		
		p_addProduct : function(e){
			addProduct(e);
		}
		,
		
		p_cancelClick : function(){
			cancelClick();
		}			
		
	}
}(jQuery);


/**
 * window load (initialize)
 */
$(document).ready(function () {
    DATACARD.init();        
    DesignSolution.init();
    page.init();
    /* Mehigh's New Functions */
    $('.design-solution a.help').click( function() {
    	return false;
    })
});


/**
 * AJAX PAGE REFRESH
 */

var isBrowserIE = jQuery.browser.msie;
var xmlHttpReq = false;
var xmlHttpReqRefreshProductsUrl = false;
var setFormValuesUrl;

// reset right side form    		
function clearForm() {
	$("form#solution-form").reset();
	$("#one-sided").checked = false;
	$(".slider-ui").slider("moveTo", 0);
	sendDesignASolutionFormValuesToServer(this, "emptyformvalues");
}

function replaceHtml(el, html) {
 var oldEl = el;
 /*@cc_on // Pure innerHTML is slightly faster in IE
 oldEl.innerHTML = html;
 return oldEl;
 @*/
 var newEl = oldEl.cloneNode(false);
 
 newEl.innerHTML = html;
 oldEl.parentNode.replaceChild(newEl, oldEl);
 /* Since we just removed the old element from the DOM, return a reference
 to the new element, which can be used to restore variable references. */
 return true;

}


// send form values to other pages via ajax
function updateDesignASolution(labelName, ui) {
	var labelObject = document.getElementById(labelName);
	sendDesignASolutionFormValuesToServer(labelObject, labelName);
	

}


function sendDesignASolutionFormValuesToServer(ui, labelName) {
	
	$('#updating-solutions').show().fadeIn(200);
					  
	var setFormValuesUrl = "";
	if (ui != "") {
		var sliderValue = Math.round(ui.value);
		setFormValuesUrl = "/id/design_a_solution/design_a_solution_set_form_values.jhtml?sliderName=" + labelName + "&sliderValue=" + sliderValue;
	} else {
		setFormValuesUrl = "/id/design_a_solution/design_a_solution_set_form_values.jhtml?sliderName=" + labelName + "&sliderValue=empty";
	}

	$.ajax({
	  url: setFormValuesUrl,
	  cache: false,
	  success: function(){
		 refreshDesignASolutionProducts();
	  }
	});

}

function refreshDesignASolutionProducts(){
	$.ajax({
		url: "/id/design_a_solution/design_a_solution_ajax.jhtml",
		cache: false,
		success: function(html){
			var el = document.getElementById("ajaxsection");
			replaceHtml(el, html);
			refreshDesignASolutionProductsAjax();		  
		}
	});
}

// get external jhtml page via ajax and replace left side with results
function refreshDesignASolutionProductsAjax() {
$('#updating-solutions').hide().fadeOut('fast');

	if (isBrowserIE) {			
					  			
			DesignSolution.p_subUpdateEvent();

			$(".tabs .tabs-nav").tabs();
			DATACARD.expandableContents();
			DATACARD.checkBrowseProductsTab();
			DATACARD.setTabLinks();
			DATACARD.updateProductsNumbers();
			DesignSolution.init();
			var sp = document.getElementById("sp");
			var ep = document.getElementById("ep");

			if(document.getElementById("design-count")!=null){
				var s = document.getElementById("design-count").innerHTML;
				s = s.trim();
				$("#design-count").html(s);		

				if (sp.innerHTML == "(0)" && ep.innerHTML != "(0)") {
					document.getElementById("ep").click();
				}
				// hide the Review/Share/SendToReseller buttons if no products in cart
				if (document.getElementById("design-count").innerHTML != "0") {
					$(".inner-box").find("p").show();
				} else {
					DesignSolution.p_closeReviewBox();
					$(".inner-box").find("p").hide();
				}		

			}
		
	} else if (isBrowserIE == false) {
		DesignSolution.p_subUpdateEvent();
		$(".tabs .tabs-nav").tabs();
		DATACARD.expandableContents();
		DATACARD.checkBrowseProductsTab();
		DATACARD.setTabLinks();
		DATACARD.updateProductsNumbers();
		DesignSolution.init();
		var sp = document.getElementById("sp");
		var ep = document.getElementById("ep");
		var op = document.getElementById("op");
		if (sp.innerHTML == "(0)" && ep.innerHTML != "(0)") {
			$(".tabs .tabs-nav").tabs("select", 1);
			$("#enterprise-products .top-nav li a").removeClass("current");
			if (!(isBrowserIE && jQuery.browser.ver == 7)) {
				$("#enterprise-products .expandable").each(function () {$(this).find(".expandable-inner").slideDown(0);$(this).removeClass("expandable-closed");});
			}
		}
		
		if (sp.innerHTML == "(0)" && ep.innerHTML == "(0)" && op.innerHTML != "(0)") {
			$(".tabs .tabs-nav").tabs("select", 2);
			$("#enterprise-products .top-nav li a").removeClass("current");
			if (!(isBrowserIE && jQuery.browser.ver == 7)) {
				$("#enterprise-products .expandable").each(function () {$(this).find(".expandable-inner").slideDown(0);$(this).removeClass("expandable-closed");});
			}
		}		
		
		// hide the Review/Share/SendToReseller buttons if no products in cart
		if(document.getElementById("design-count")!=null){
			var s = document.getElementById("design-count").innerHTML;
			s = s.trim();
			$("#design-count").html(s);		
		}
		if (document.getElementById("design-count").innerHTML != "0") {
			$(".inner-box").find("p").show();
		} else {
			DesignSolution.p_closeReviewBox();
			$(".inner-box").find("p").hide();
		}
		
	}
	$('#updating-solutions-firstload').hide();
	if(document.getElementById("industry")!=null && document.getElementById("industry").value != "select-industry"){
		$('#industryHeader').html(document.getElementById("industry").value);
	}
}

/*
/ ADD TO SOLUTION
/
*/

// passes contentId of selected model to server via ajax and refreshed cart on server
// and on page as well as changes status under clicked model
var contentIdTemp = '';
var cartClickTemp = '';
function addToMySolution(contentId, cartClick) {
	contentIdTemp = contentId;
	cartClickTemp = cartClick;
	var e = "#" + contentId;
	if (cartClick == "true") {
		DesignSolution.p_removeProduct(e);
	} else {
		DesignSolution.p_addProduct(e);
	}
	designASolutionCartUrl = "/id/design_a_solution/design_a_solution_models.jhtml?contentId=" + contentId;
	if (window.XMLHttpRequest) {
		xmlHttpReq = new XMLHttpRequest;
	} else if (window.ActiveXObject) {
		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlHttpReq) {
		xmlHttpReq.onreadystatechange = addToMySolutionSub;
		xmlHttpReq.open("GET", designASolutionCartUrl, true);
		xmlHttpReq.send(null);
	}
}

// gets current count for cart and refreshed cart on page
function addToMySolutionSub() {
	if (xmlHttpReq.readyState == 4) {
		if (xmlHttpReq.status == 200) {
			$("#left-content").html(xmlHttpReq.responseText);
			var designASolutionCartUrl = "/id/design_a_solution/design_a_solution_models_count.jhtml";
			var designASolutionCart = document.getElementById("designASolutionCart");
			$.ajax(
				{
					url:designASolutionCartUrl,
					cache:false,
					success:function (html)
					{
						var s = html;s = s.trim();
						$("#design-count").html(s);
						// hide the Review/Share/SendToReseller buttons if no products in cart							
						if (document.getElementById("design-count").innerHTML != "0") {
							$(".inner-box").find("p").show();
							if($('#widget1').length>0){
								$("#widget2").show();
								$("#widget1").hide();

								if(contentIdTemp == $('#contentIdTemp').html() && cartClickTemp == 'false'){
									$('#removeproduct').show();
									$('#addproduct').hide();					
								}

								if(contentIdTemp == $('#contentIdTemp').html() && cartClickTemp == 'true'){
									$('#removeproduct').hide();
									$('#addproduct').show();					
								}
							}
						} else {
							DesignSolution.p_closeReviewBox();
							$(".inner-box").find("p").hide();
							if($('#widget1').length>0){
								$('#addproductemptybox').show();				
							}								
						}
					}
				}
			);
		}
	}
}

// trims variables passed to it --- var s = "test ";s = s.trim();
String.prototype.trim = function () {return this.replace(/^\s*/, "").replace(/\s*$/, "");};
