﻿
//Plugin for styling select tag
(function($){
	$.fn.extend({
		customStyle : function(options) {
			if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){
				return this.each(function() {

					var self = this;
					var currentSelected = $(this).find(':selected');
					$(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">'+currentSelected.text()+'</span><span style="display: inline-block; float: right;" class="customStyleSelectBoxArrow"></span></span>').css({position:'absolute', opacity:0,fontSize:$(this).next().css('font-size')});
					var selectBoxSpan = $(this).next();
					var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) -parseInt(selectBoxSpan.css('padding-right'));
					var selectBoxSpanInner = selectBoxSpan.find('.customStyleSelectBoxInner');
					selectBoxSpan.css({display:'inline-block'});
					selectBoxSpanInner.css({width:selectBoxWidth - selectBoxSpan.find('.customStyleSelectBoxArrow').width(), display:'inline-block'});
					var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
					$(this).height(selectBoxHeight).change(function(){
						// selectBoxSpanInner.text($(this).val()).parent().addClass('changed');   This was not ideal
					selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
					});
				});
			}
		}
	});
})(jQuery);

function getInternetExplorerVersion()
{
	var rv = -1; // Return value assumes failure.
	if (navigator.appName == 'Microsoft Internet Explorer')
	{
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
			rv = parseFloat( RegExp.$1 );
	}
	return rv;
}


$(document).ready(function(){

	if( getInternetExplorerVersion() < 0 )
		$('.sickness-selector select').customStyle();

	/*************************************************************************
		Search section
	*/
	$('.common-search').alphanumeric({ichars:'<>'});

	$('.common-search').focus(function(){
		if( $(this).val() == $(this).attr('empty-text') ){
			$(this).removeClass('empty');
			$(this).val('');
		}
	});

	$('.common-search').blur(function(){
		if( $(this).val() == '' || $(this).val() == $(this).attr('empty-text') ){
			$(this).addClass('empty');
			$(this).val($(this).attr('empty-text'));
		}
	});

	// Submit form w/o hint
	$(".tp-search-do").click(function() {
		$(this).parent().find('.common-search-form').submit();
	});
	$(".common-search").keypress(function(e) {
		if (e.which == 13)
			$(this).parent().submit();
	});
	$(".common-search-form").submit(function() {
		var sfield = $(this).find(".common-search");
		if (sfield.hasClass("empty"))
			sfield.val("");

		return true;
	});

	if( $('.sickness-selector select').val() == '' )
		$('.sickness-selector .customStyleSelectBox').addClass('empty');

	$('.sickness-selector select').change(function(){
		var href = $(this).val();
		if( href != '' ){
			window.location = href;
			$('.sickness-selector .customStyleSelectBox').removeClass('empty');
		}
		else
			$('.sickness-selector .customStyleSelectBox').addClass('empty');
	});


	/*************************************************************************
		Cart
	*/
	$.ajax({
		type: "POST",
		url: "/svc/ClientJS.asmx/GetCart",
		dataType: "json",
		contentType: "application/json; charset=utf-8",
		data: "{}",
		success: function( r ) {
			RefreshCart( r );
		}
	});

	$('.sdf-product-add .small-green').click(function(){
		var prodId = $(this).parent().parent().attr('prodid');
		var amount = $('.sdf-cart-amount-input').val() || 1;

		$.ajax({
			type: "POST",
			url: "/svc/ClientJS.asmx/UpdateCart",
			dataType: "json",
			contentType: "application/json; charset=utf-8",
			data: "{ 'productId' : '" + prodId + "', 'amount' : '" + amount + "', 'update' : 'false' }",
			success: function( r ) {
				RefreshCart( r );
				showAlert(prodId);
			}
		});
	});

	RefreshCart = function(r) {
		if (!(r && r.d && r.d.IsSuccess && r.d.TotalRecords != 0)) {
			/*$('.cart-blue-panel').hide();*/
			$('.cart-list').html($('.cart-list').attr('emptystring'));
			$('.cart-total-price').html('0.00');
			return;
		}

		var array = r.d.Object;
		var htmlString = '', lastHtmlString;
		for (i = 0; i < array.length; i++) {
			var o = array[i];
			lastHtmlString = htmlString;
			htmlString = '<div class="blue-cart-item"><div class=blue-item-name>' +
				'<a href="' + o.Href + '">' + o.Name + '...' +
					'</a>' + '</div><div class="blue-item-price" ';
			if (i == array.length - 1)
				htmlString = htmlString + 'style="margin-bottom: 0 !important;"';

			htmlString = htmlString + '><div class="blue-item-price-inner"><img alt="P" src="/client/default/images/brur.png" class="brur-slash">' +
				o.Price + '</div></div><div style="clear: both;"></div></div>' + lastHtmlString;
			$('.product-in-cart-' + o.Id).show();
		}

		$('.cart-list').html("");
		$('.cart-list').html(htmlString);
		$('.cart-total-price').html(r.d.Code);
	};

	$('.cart-add-product-alert .alert-close, .auth-info-mess .alert-close').click(function() {
		$(this).parent().fadeOut("slow");
	});

	showAlert = function(prodId) {
		$('.product-in-cart-' + prodId).show();
		$('.cart-alert-' + prodId).fadeIn("fast", function (){
			window.setTimeout(function() {
				$('.cart-alert-' + prodId).fadeOut("slow");
			}, 3000); 
		});
		
	};
	
});
