//jQuery.noConflict();

function openClose(){
	var _d = 500;
	var _widgets = jQuery('div.widget');
	var _defStatusCookie = getCookie('widgetStatus');
	var _defStatus = [];
	if(_defStatusCookie && _defStatusCookie!=''){
		_defStatus = _defStatusCookie.split('&');
		for(var i=0; i<_defStatus.length; i++) {
			if(_defStatus[i].indexOf('=')) _defStatus[i] = _defStatus[i].substr(_defStatus[i].indexOf('=')+1);
		}
	};
	_widgets.each(function(i){
		var _this = jQuery(this);
		var _title = jQuery('.open-close-holder', this);

		//build
		if(_title.next('ul').length){
			_title.next('ul').wrap('<div />');
			var _slider = _title.next().css({
				position: 'relative',
				overflow: 'hidden',
				width:'100%'
			});
		}else if(_title.next('div').length){
			var _slider = _title.next('div').css({
				position: 'relative',
				overflow: 'hidden',
				width:'100%'
			});
		};

		//init
		if(_defStatus.length){
			if(_defStatus[i]==0) _slider.css({height: 0});
			else _slider.css({height: ''});
		}
		if(_slider.height() == 0){
			_this.addClass('closed');
		}

		_title.find('a').click(function(){
			if(_slider.height() > 0){
				_slider.animate({height: 0},{duration: _d, complete: function(){
					_this.addClass('closed');
					savePositions(_widgets);
				}});
			}else{
				_slider.animate({height: _slider.children().outerHeight(true)},{duration: _d, complete: function(){
					_this.removeClass('closed');
					jQuery(this).css({height: ''})
					savePositions(_widgets);
				}});
			};
			return false;
		});
	});
	
	function savePositions(elements){
		var _status = '';
		elements.each(function(i){
			_status += i+'=' + ((jQuery(this).hasClass('closed')) ? '0' : '1') + '&';
		});
		setCookie('widgetStatus',_status,60);
	}
}

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}

	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function copyToClipboard(){
	var btn_copy = jQuery('div.coupon-number').find('a:eq(0)');
	btn_copy.each(function(){
		clip = new ZeroClipboard.Client();
		var _link = this;
		clip.setText(_link.rel);
		clip.glue(_link);
		clip.addEventListener('mouseOver', function(){
			jQuery(_link).parents('.coupon-number').addClass('hover');
		});
		clip.addEventListener('mouseOut', function(){
			jQuery(_link).parents('.coupon-number').removeClass('hover');
		});
		clip.addEventListener('complete', function(){
			window.location = _link.href;
		});
	});
}

jQuery(document).ready(function(){
	openClose();
	copyToClipboard();
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
});


function clearFormFields(o)
{
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filter) o.filter = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass)) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass)) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}


