/** * Config * */var itemsPerPage = 18;var cartItemsPerPage = 3;var inSearch = false;var resetPaging = false;    $(document).ready(function(){		$('.cluetip').cluetip({tracking: true, splitTitle: "|"});		if ($("#list_type").val()) { 		if ( $.browser.opera || $.browser.mozilla || $.browser.safari ) {			$.history.init(load);	    } else {	    	load();	    }    }    	function changePlaces(e) {						couponId	= this.id.replace(/city_/, '');		cityId		= this.value;				$.ajax({			type		: 'GET',			url			: baseUrl+'get_addresses/'+couponId+'/'+cityId+'/',			dataType	: 'json',						success		: function (response) {							$('#address_'+couponId).html('');									$('#address_'+couponId).createAppend([								'option', {'value': 0}, "-- Adresas --"							]);										if (response) {								for (i in response) {									$('#address_'+couponId).createAppend([										'option', {'value': response[i].id}, response[i].address_line									]);								}																$('#address_'+couponId).attr('disabled', false);							} else {								$('#address_'+couponId).attr('disabled', true);							}						}		});	}		function createCoupon(couponData, i) {		var item = null;				if (i % 2 == 2) {			className = 'item lastcol';		} else {			className = 'item';		}				item = $('#listing').createAppend('div', {"class": className});		var cart_link_class = (couponData.in_cart == 1) ? 'from_cart_link_div' : 'cart_link_div';				item.hide();				item.createAppend(			'div', {"class": 'coupon_link_div'}, [				'div', {"class": cart_link_class}, [						'a', {href: baseUrl+'krepselis/'+couponData.coup_alias}, ''					],				'a', {href: ((couponData.prov_alias)? baseUrl+couponData.prov_alias+"/"+couponData.coup_alias : baseUrl+couponData.coup_alias)}, ''			]		);				$('.cart_link_div a').click(addToCart);		$('.from_cart_link_div a').click(addToCart);				logo = item.createAppend('div', {"class": "logo"});		logo.createAppend('h1', {}, couponData.coup_title);		logo.css({'background-image': 'url("'+baseUrl+'images/logos/'+couponData.coup_logo+'")'});				item.createAppend(			'div', {"class": 'left'}, couponData.coup_discount.replace(/\.00$/, '')+couponData.discount_type		);				item.createAppend(			'div', {"class": 'right'}, couponData.coup_title		);				return item;	}		function reloadCoupons (e) {		e.preventDefault(); 				if ($("#list_type").val() != "cart") {								if ($("#list_type").val() != "search") {							$('html, body').animate({scrollTop:0}, 'slow'); 							var url			= String(window.location).split("#")[0];				var anchor		= String(window.location).split("#")[1];								var city		= null;				var category	= null;				var page		= null;								if (anchor) {					city		= (anchor.split(":")[0] != undefined) ? anchor.split(":")[0] : "visi";					category	= (anchor.split(":")[0] != undefined) ? anchor.split(":")[1] : "visi";				} else {					city		= "visi";					category	= "visi";				}												if ($(this).hasClass("cities"))			city		= $(this).attr("rel");				if ($(this).hasClass("cats"))			category	= $(this).attr("rel");				if ($(this).hasClass("paging_link"))	page		= $(this).attr("rel");									var append = "#"+city+":"+category;				if (page) {					append += ":"+page;									}								window.location = url+append; 								// category active				if ($(this).hasClass("cats"))				{					$('a.filter_link.cats').removeClass('active');					$(this).addClass('active');				}								if ($.browser.opera || $.browser.mozilla || $.browser.safari) {					$.history.load();			    } else {			    	load();			    }		    } else { 					    	if ($(this).hasClass("paging_link")) 				{					page = $(this).attr("rel");				}						    	if (page) { 		    		location.hash = "#"+page;		    	} else  {		    		location.hash = "#0";		    	}		    			    	if (this.id == "adv_search_form") {		    		resetPaging = true;		    	}		    			    	inSearch = true;		    			    	if ($.browser.opera || $.browser.mozilla || $.browser.safari) {					$.history.load();			    } else {			    	load();			    }			    			   		    }		}				return false;	}		function load() {				if ($("#list_type").val() != "cart") {					$('#more_cities_box').fadeOut();			$('#more_categories_box').fadeOut();						$('#filter li').removeClass("active");						$('#listing').fadeOut('normal', function() {				$('#nuolaidos').addClass('loading');			});						var method	= null;			var type	= $("#list_type").val();			switch(type) {				case "search":					method = "paieska";					break;				case "1":					method = "nuolaidos";					break;				case "2":					method = "geriausi";					break;				case "3":					method = "naujausi";					break;				default :					method = "nuolaidos";			}						if (method != "paieska") { 				var anchor = String(window.location).split("#")[1];								if (anchor) {					var city		= (anchor.split(":")[0] != undefined) ? anchor.split(":")[0] : null;					var category	= (anchor.split(":")[0] != undefined) ? anchor.split(":")[1] : null;					var page		= (anchor.split(":")[1] != undefined) ? anchor.split(":")[2] : null;				}								if (city) {					$("#f_"+city).addClass("active");				} else {					city = "visi";				}								if (category) {					$("#f_"+category).addClass("active");				} else {					category = "visi";				}						} else {				var page = location.hash.replace(/^#/, '');			}						if (!page) {				page = "0";			}						if (resetPaging) {				page = '';				resetPaging = false;			}						if (method != "paieska") { 				url = baseUrl+method+"/"+city+"/"+category+"/"+page+"?random="+(new Date().getTime());			} else {				if (inSearch) {					var searchKeyword = $("#search_keywords").val();				} else {					var searchKeyword = ($("#search_keyword").val() != "nuolaidų paieška...") ? $("#search_keyword").val() : "a";				}							url = baseUrl+method+"/"+searchKeyword+"/"+page+"?random="+(new Date().getTime());			}									page = parseInt(page);						$.ajax({				type		: (method != 'paieska') ? 'GET'		: 'POST',				url			: url,				dataType	: 'json',				data		: (method != 'paieska') ? null		: $('#adv_search_form').serialize(),								success		: function (response) {									$('#listing').html('');																		var data		= response.data;									var numPages	= Math.ceil(response.totalItems/itemsPerPage);									var totalItems	= response.totalItems;																		var regexp			= new RegExp("[0-9]+$");																	// reset prev and next links									var $prevLink		= $('#pagging .prev');									var $nextLink		= $('#pagging .next');																		var newPrev = parseInt(page) - itemsPerPage;									var newNext = parseInt(page) + itemsPerPage;																		if (newPrev <= 0) newPrev = "";									if (newNext >= numPages*itemsPerPage) newNext = currentPage;																	if (regexp.test($prevLink.attr('href'))) {										var newPrevLink	= $prevLink.attr('href').replace(/[0-9]+$/, newPrev);									} else {										var newPrevLink	= $prevLink.attr('href') + newPrev;									}																		if (regexp.test($nextLink.attr('href'))) {										var newNextLink	= $nextLink.attr('href').replace(/[0-9]+$/, newNext);									} else {										var newnextLink	= $nextLink.attr('href') + newNext;									}																		$prevLink.attr('href', newPrevLink);									$nextLink.attr('href', newNextLink);																		$prevLink.attr('rel', newPrev);									$nextLink.attr('rel', newNext);																		// build new paging links									$('#pagging .center').html('');																		if (numPages < 2) {										$('#pagging').slideUp();									} else {										var first		= true;										var searchUrl	= '';										var newLink		= '';																				for (i=1; i<=numPages; i++) {											var limit = (i * itemsPerPage) - itemsPerPage;											if (limit == 0) limit = '';																						href		= baseUrl+method+'/'+limit;											className	= (first) ? 'active' : ''; 																						newLink = $('#pagging .center').createAppend(												'a', {'href': href,  'id': 'page_'+limit, 'class': 'paging_link', "rel": limit}, [													'span', {}, i+''												]											);																						if( i == 10 )											{												$('#pagging .center').createAppend(												'br'												);											}																						newLink.click(reloadCoupons);																						first = false;										}																				$('#pagging:hidden').slideDown();									}																		// set active page									var currentPage		= parseInt(page);									if (!currentPage) currentPage = "";									$('#page_'+currentPage).addClass('active');																		if (data.length > 0) {										newHeight = 127 * Math.ceil(data.length/2);										for (i in data) {																					coupon = createCoupon(data[i], i);																						if (i % 2 == 2) {												$('#listing').createAppend(													'br', {"class": "clear"}												);											}										}									} else {										newHeight = 13;										$('#listing').createAppend(											'div', {"class": "message_box"}, "<p>Atsiprašome, bet pagal pasirinktus parametrus kuponų nerasta.</p>"										);									}																		$('#outter_listing').animate({height: newHeight}, function () {										$('#nuolaidos').removeClass('loading');										$('#listing .item').show();										$('#listing').fadeIn();									});																	}			});		}	}		function addToCart(e) {				e.preventDefault();				var cartLink	= this;		var cityId		= $('.city_select').val();		var addressId	= $('.address_select').val();				if (cityId == '' || typeof(cityId) == "undefined" ) cityId = 0;		if (addressId == '' || typeof(addressId) == "undefined" ) addressId = 0;				var sLink = this.href+'/'+cityId+'/'+addressId+"?random="+(new Date().getTime());				$.ajax({				type		: 'GET',				url			: sLink,				dataType	: 'json',				success		: function (response) {													var coupon		= $(cartLink).parent().parent();									var cart_link	= $(cartLink).parent();																		if (cart_link.hasClass('cart_link_div')) {										var height	= coupon.height();										var width	= coupon.width();										var top		= coupon.offset().top;										var left	= coupon.offset().left;																				var newHeight	= $('.counter').height();										var newWidth	= $('.counter').width();										var newTop		= $('.counter').offset().top;										var newLeft		= $('.counter').offset().left;									} else {										var newHeight	= coupon.height();										var newWidth	= coupon.width();										var newTop		= coupon.offset().top;										var newLeft		= coupon.offset().left;																				var height	= $('.counter').height();										var width	= $('.counter').width();										var top		= $('.counter').offset().top;										var left	= $('.counter').offset().left;									}																		outline	= $('body').createAppend('div', {"class": "coupon_outline"});																		outline.css('position', 'absolute');									outline.css('z-index', '999999999');									outline.css('top', top);									outline.css('left', left);																		outline.height(height);									outline.width(width);																		cartCount = (response) ? response : '0';																		outline.animate(										{											"width"		: newWidth,											"height"	: newHeight,											"top"		: newTop,											"left"		: newLeft										},										500,										function() {											outline.remove();											$('.counter').html(cartCount);											if (cart_link.hasClass('cart_link_div')) {												cart_link.removeClass('cart_link_div');												cart_link.addClass('from_cart_link_div');											} else {												cart_link.removeClass('from_cart_link_div');												cart_link.addClass('cart_link_div');											}										}									);																										}		});	}		function printSingle(e) {		e.preventDefault();				var ok = true;		$('#coupon_details_form select').each(function () {			if (this.value == '0' || this.value == '') { 				ok = false;			} 		});				if (!ok) {			alert('Nepasirinkote kupono adreso!');			return;		} 				var data = $('#coupon_details_form').serialize();				newwindow=window.open(baseUrl+'print/?'+data, 'name', 'height=600,width=650,scrollbars=yes');		if (window.focus) {			newwindow.focus()		}	}		function printCart(e) {		e.preventDefault();				var ok = true;		$('#cart_form select').each(function () {			if (this.value == '0' || this.value == '') { 				ok = false;			} 		});				if (!ok) {			alert('Nepasirinkote kupono adreso!');			return;		} 				var data = $('#cart_form').serialize();				newwindow=window.open(baseUrl+'print/?'+data, 'name', 'height=600,width=650,scrollbars=yes');		if (window.focus) {			newwindow.focus()		}	}		function showSublist(e) {		e.preventDefault();				$('#more_cities_box').fadeOut();		$('#more_categories_box').fadeOut();				$(this).html('<span>Uždaryti</span>');			var clickId		= this.id;		var newHeight	= 21;				if (clickId == 'more_cities') {			var childsCount	= $('#more_cities_box').children('ul').children().length;		}		if (clickId == 'more_categories') {			var childsCount	= $('#more_categories_box').children('ul').children().length;		}				if (childsCount < 10) {			newHeight = 21*childsCount;		} else {			newHeight = 210;		}				var objectOffset = $(this).offset();				//newHeight = 500;				if (clickId == 'more_cities') {			$('#more_cities_list').height(newHeight);			$('#more_cities_box').css("top", objectOffset.top+21).css("left", objectOffset.left).fadeIn();		}				if (clickId == 'more_categories') {			$('#more_categories_list').height(newHeight);			$('#more_categories_box').css("top", objectOffset.top+21).css("left", objectOffset.left).fadeIn();		}	}		function showCommentForm(e) {		e.preventDefault();		$('#write_coment').slideDown();	}		function showAnswer() {		$(this).parent().children('dd').slideDown();	}		function hideAnswer() {		$(this).parent().children('dd').slideUp();	}		function subscribe(e) {		e.preventDefault();				var sendData = $('#newsletter_form').serialize();				$.ajax({			type		: 'POST',			url			: baseUrl+'subscribe/'+"?random="+(new Date().getTime()),			data		: sendData,			dataType	: 'text',			success		: function (response) {														switch (response) {								case '0':									$('#newsletter_msg').html('Jums buvo išsiųstas patvirtinimo laiškas');								break;																case '1':									$('#newsletter_msg').html('Toks adresas jau egzistuoja');								break;																case '2':									$('#newsletter_msg').html('Prašome įvesti teisingą el. pašto adresą');								break;							}						}		});	}		function submitSearch(e) {		e.preventDefault();				if ($("#search_keyword").val() != "nuolaidų paieška...") {			window.location.href = baseUrl+'paieska/'+$("#search_keyword").val();		}	}		/**	 * Bind events	 */	 	function hideSubList() {		$("#more_categories").html('<span>Daugiau...</span>');		$("#more_cities").html('<span>Daugiau...</span>');				$('#more_cities_box').fadeOut();		$('#more_categories_box').fadeOut();	}		$('#search_form').submit(submitSearch);		$('#newsletter_form').submit(subscribe);	 	$('#cart_print').click(printCart);		$('#details_print').click(printSingle);		$('#coupon_page .filter_link, #listing_pagination a').click(reloadCoupons);		//$('#adv_search_submit').click(reloadCoupons);		$('#adv_search_form').submit(reloadCoupons);		$('.city_select').change(changePlaces);		$('.cart_link_div a, .from_cart_link_div a').click(addToCart);		$('.col2 a').toggle(showSublist, hideSubList);		$('#write_comment').click(showCommentForm);		$('#duk dt').toggle(showAnswer, hideAnswer);		$('form#send_form').submit(sendToFriend);		$('.back_link').click(function (e) {		e.preventDefault();				window.history.back();	});		// clear default messages	$('.keyword').focus(function() {		if (this.value == "nuolaidų paieška...") {			$(this).val('');		}	});	$('.keyword').blur(function() {		if (this.value == "") {			$(this).val('nuolaidų paieška...');		}	});		$('.textfield').focus(function() {		if (this.value == "vartotojo vardas") {			$(this).val('');		}	});	$('.textfield').blur(function() {		if (this.value == "") {			$(this).val('vartotojo vardas');		}	});		$('#write_coment textarea').focus(function() {		if (this.value == "Jūsų komentaras...") {			$(this).val('');		}	});	$('#write_coment textarea').blur(function() {		if (this.value == "") {			$(this).val('Jūsų komentaras...');		}	});		$('#guestbook textarea').focus(function() {		if (this.value == "Jūsų tekstas...") {			$(this).val('');		}	});	$('#guestbook textarea').blur(function() {		if (this.value == "") {			$(this).val('Jūsų tekstas...');		}	});		$('#msg').focus(function() {		if (this.value == "Jūsų žinutė...") {			$(this).val('');		}	});	$('#msg').blur(function() {		if (this.value == "") {			$(this).val('Jūsų žinutė...');		}	});		// IE6 specific events	if (false && $.browser.msie && $.browser.version < 7) {			$('#content #right_side #nuolaidos .cart_link_div').hover(			function() {				$(this).css("background-image", "url("+baseUrl+"images/coupon_to_cart_active.gif)");			},			function() {				$(this).css("background-image", "url("+baseUrl+"images/coupon_to_cart.gif)");			}		);				$('#content #right_side #nuolaidos .coupon_link_div').hover(			function() {				$(this).css("background-image", "url("+baseUrl+"images/nuolaida_frame_active.gif)");			},			function() {				$(this).css("background-image", "url("+baseUrl+"images/nuolaida_frame2.gif)");			}		);				$('#content #right_side #nuolaidos .from_cart_link_div').hover(			function() {				$(this).css("background-image", "url("+baseUrl+"images/coupon_from_cart_active.gif)");			},			function() {				$(this).css("background-image", "url("+baseUrl+"images/coupon_from_cart.gif)");			}		);				$('#content #right_side #coupon .from_cart_link_div').hover(			function() {				$(this).css("background-image", "url("+baseUrl+"images/coupon_from_cart_big_active.gif)");			},			function() {				$(this).css("background-image", "url("+baseUrl+"images/coupon_from_cart_big.gif)");			}		);				$('#content #right_side #coupon .cart_link_div').hover(			function() {				$(this).css("background-image", "url("+baseUrl+"images/coupon_to_cart_big_active.gif)");			},			function() {				$(this).css("background-image", "url("+baseUrl+"images/coupon_to_cart_big.gif)");			}		);	}	//$('#cart_pagination #pagging a').click(loadCart);		//$('.remove_cart_link_div a').click(removeFromCart);});function sendToFriend() {		if (!$('#your_email').val().match(/^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}$/)			&& !$('#friends_email').val().match(/^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}$/))		{			alert('Prašome užpildyti visus laukus');			return false;		}		var send_form	= $('#send_form').serialize();		var id			= $('#coupon_id').val();		var cart_form	= $('#cart_form').serialize().substring(1);				if (id) {			send_form += '&coupon_id='+id;		}				if (cart_form) {			send_form += '&'+cart_form;		}				$.ajax({			type		: 'POST',			url			: $('#send_form').attr('action'),			data		: send_form,			dataType	: 'text',			success		: function (response) {							tb_remove();						}		});				return false;	}
