jQuery(document).ready(function() {
	specialOfferStyles();
	$('.view-why-us-blocks h2').addClass('hid');
	/* SERVICE LEVELS */
	setActiveState('.view-ServiceLevels.view-display-id-block_1 td.views-field-nothing a');
	
	//Language change form submit
	$("#lang_select").change(function () {
		document.s_lang.submit();
	});
	
	var elements = '.block-simplenews #edit-mail';
	$('.block-simplenews #edit-mail').attr('title', 'Email Address');
	$(elements).hint();
	
	abcNavWrap('ul.nice-menu');
	
	$('.page-header-menu li').biggerlink({
		clickableclass: 'biggerlink',
		hoverclass: 'hover'
	});
	
	/* Sliding Panel Auto-Activation */
	var controller = '#sliding-panel-controller';
	var container = '#sliding-panel';
	var path = window.location.pathname;
	
	$(controller).click(function() {
		slidingPanelFX(controller, container);
	});
	
	$('.view-why-us-blocks h2').click(function() {
		slidePanelClose(controller, container);
	});
 	
 	$('#block-block-76').hover(function() {
	  	$('#block-block-76 p').addClass( 'aactive' )		
	},
	function() {
		$('#block-block-76 p').removeClass( 'aactive' )	
	}
	);
		$('#block-block-81').hover(function() {
	  	$('#block-block-81 p').addClass( 'aactive' )		
	},
	function() {
		$('#block-block-81 p').removeClass( 'aactive' )	
	}
	);
	function newsletterbyurl() {
 		var n = $("#simplenews-subscription-manager-form input:checked").length;
		if(n == 0){
			if($(location).attr('href') == 'http://abc-lang.com/newsletter/subscriptions?news=webinar') {
				$('input#edit-newsletters-176').attr('checked', true);
			}
			else if($(location).attr('href') == 'http://abc-lang.com/newsletter/subscriptions?news=tips') {
				$('input#edit-newsletters-26').attr('checked', true);
			}
		}		
	}	
	newsletterbyurl();
	/*$('#simplenews-subscription-manager-form #edit-subscribe').click(function() {
  				if(n == 0)
				{

				}
		});*/
		$('#show_pop').click(function() {
			$('#sliding-panel').addClass('expanded');
			$('#sliding-panel').stop().animate({ bottom : '0' }, '300');
			$('#show_pop').addClass('pinactive');
			$('.view-why-us-blocks h2').removeClass('hid');
		});
});

/* SPECIAL OFFERS STYLES */
function specialOfferStyles() {
	$('.view-SpecialOffers .field-discount').each(function() {
		if($(this).html() != '') {
			$(this).parents('.views-field-title').addClass('with-discount');
		}
	});
}

/* SLIDING PANEL */
function slidingPanelFX(controller, container) {
	panelState = getCookie('panel_state');
	
	if(panelState == 'on' || panelState == null || panelState == '') {
		if($(container).hasClass('expanded')) {
			$(controller).removeClass('active');
			$(container).removeClass('expanded');
			$(container).stop().animate({ bottom : '-350px' }, '300');
			$('.view-why-us-blocks h2').addClass('hid');			
			setCookie('panel_state', 'off', 1, path);
		} else {
			$(controller).addClass('active');
			$(container).addClass('expanded');
			$(container).stop().animate({ bottom : '0' }, '300');
			$('.view-why-us-blocks h2').removeClass('hid');
		}
	}
}

function slidePanelOnLoad(controller, container, tSlideUp, tSlideDown, path) {
	panelState = getCookie('panel_state');
	
	if(panelState == 'on' || panelState == null || panelState == '') {
		setTimeout(function() {
			$(container).stop().animate({ bottom : '0' }, function() {
				setCookie('panel_state', 'off', 1, path);
				$('#show_pop').addClass('pinactive');
				$('.view-why-us-blocks h2').removeClass('hid');
				$(controller).addClass('active');
				$(container).addClass('expanded');
				setTimeout(function() {
			        $(container).stop().animate({ bottom : '-350px' }, '300');
			        $(controller).removeClass('active');
					$(container).removeClass('expanded');
					$('#show_pop').addClass('pinactive');
					$('.view-why-us-blocks h2').removeClass('hid');
			    }, tSlideDown);
			});
		}, tSlideUp);
	}
	/**To enable sliding panel uncomment this part of code and comment everything else in this function**/
	/*$(controller).addClass('active');
	$(container).addClass('expanded');
	$(container).stop().animate({ bottom : '0' }, '300');
	$('#show_pop').addClass('pinactive');
	$('.view-why-us-blocks h2').removeClass('hid');*/
}

function slidePanelClose(controller, container) {
	if($(container).hasClass('expanded')) {
		$(controller).removeClass('active');
		$(container).removeClass('expanded');
		$(container).stop().animate({ bottom : '-350px' }, '300');
		$('#show_pop').removeClass('pinactive');
		$('.view-why-us-blocks h2').addClass('hid');			
	}
	
}

/*
 * Hover class for Services page
 */
function servicesHover() {
	var parent = '.view-Services.view-display-id-block_1';
	var img = parent+' .views-field-field-image-fid';
	var title = parent+' views-field-title';
	
	//$(img).hover
}

/*
 * CUSTOM DROP DOWN MENU LOOK
 * Ads <li> item before the first li found inside <ul> provided, and another li item after last li found.
 */
function abcNavWrap(e) {
	$(e+' ul li:first-child').before('<li class="nav-item-top">&nbsp;</li>');
	$(e+' ul li:last-child').after('<li class="nav-item-bottom">&nbsp;</li>');
}

jQuery.fn.hint = function (blurClass) {
	if (!blurClass) {
		blurClass = 'blur';
	}

	return this.each(function () {
		// get jQuery version of 'this'
		var $input = jQuery(this),
		
		// capture the rest of the variable to allow for reuse
		title = $input.attr('title'),
		$form = jQuery(this.form),
		$win = jQuery(window);
		
		function remove() {
			if ($input.val() === title && $input.hasClass(blurClass)) {
				$input.val('').removeClass(blurClass);
			}
		}
	
		// only apply logic if the element has the attribute
		if (title) { 
			// on blur, set value to title attr if text is blank
			$input.blur(function () {
				if (this.value === '') {
					$input.val(title).addClass(blurClass);
				}
			}).focus(remove).blur(); // now change all inputs to title
			
			// clear the pre-defined text when form is submitted
			$form.submit(remove);
			$win.unload(remove); // handles Firefox's autocomplete
		}
	});
};

/* COOKIES */
function setCookie(c_name, value, expiredays, path) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	var cookie_string = c_name+"="+escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	
	  if ( path )
        cookie_string += "; path=" + escape ( path );

	
	document.cookie = cookie_string;

}

function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			
			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
 	return null;
}

/* Set Active State to element based on current URL. USAGE: setActiveState('.some-class a'); */
function setActiveState(e) {
	var link;
	var urlVars = $.getUrlVars();
	$(e).each(function() {
		link = $(this).attr('href'); // something like "/work/service-levels?slid=43"
		
		if(link == '/work/service-levels?slid='+urlVars['slid']) {
			$(this).addClass('active');
			$(this).parents('tr').addClass('active-trail');
		}
	});
}

jQuery.extend ({
	getUrlVars: function() {
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++) {
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	},
	getUrlVar: function(name){
		return $.getUrlVars()[name];
	}
});
