(function(d){d.each(["backgroundColor","borderBottomColor","borderLeftColor","borderRightColor","borderTopColor","color","outlineColor"],function(f,e){d.fx.step[e]=function(g){if(g.state==0){g.start=c(g.elem,e);g.end=b(g.end)}g.elem.style[e]="rgb("+[Math.max(Math.min(parseInt((g.pos*(g.end[0]-g.start[0]))+g.start[0]),255),0),Math.max(Math.min(parseInt((g.pos*(g.end[1]-g.start[1]))+g.start[1]),255),0),Math.max(Math.min(parseInt((g.pos*(g.end[2]-g.start[2]))+g.start[2]),255),0)].join(",")+")"}});function b(f){var e;if(f&&f.constructor==Array&&f.length==3){return f}if(e=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(f)){return[parseInt(e[1]),parseInt(e[2]),parseInt(e[3])]}if(e=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(f)){return[parseFloat(e[1])*2.55,parseFloat(e[2])*2.55,parseFloat(e[3])*2.55]}if(e=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(f)){return[parseInt(e[1],16),parseInt(e[2],16),parseInt(e[3],16)]}if(e=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(f)){return[parseInt(e[1]+e[1],16),parseInt(e[2]+e[2],16),parseInt(e[3]+e[3],16)]}return a[d.trim(f).toLowerCase()]}function c(g,e){var f;do{f=d.curCSS(g,e);if(f!=""&&f!="transparent"||d.nodeName(g,"body")){break}e="backgroundColor"}while(g=g.parentNode);return b(f)}var a={aqua:[0,255,255],azure:[240,255,255],beige:[245,245,220],black:[0,0,0],blue:[0,0,255],brown:[165,42,42],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgrey:[169,169,169],darkgreen:[0,100,0],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkviolet:[148,0,211],fuchsia:[255,0,255],gold:[255,215,0],green:[0,128,0],indigo:[75,0,130],khaki:[240,230,140],lightblue:[173,216,230],lightcyan:[224,255,255],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightyellow:[255,255,224],lime:[0,255,0],magenta:[255,0,255],maroon:[128,0,0],navy:[0,0,128],olive:[128,128,0],orange:[255,165,0],pink:[255,192,203],purple:[128,0,128],violet:[128,0,128],red:[255,0,0],silver:[192,192,192],white:[255,255,255],yellow:[255,255,0]}})(jQuery);

jQuery.fn.center=function(absolute){return this.each(function(){var t=jQuery(this);t.css({position:absolute?'absolute':'fixed',left:'50%',top:'50%',zIndex:'99'}).css({marginLeft:'-'+(t.outerWidth()/2)+'px',marginTop:'-'+(t.outerHeight()/2)+'px'});if(absolute){t.css({marginTop:parseInt(t.css('marginTop'),10)+jQuery(window).scrollTop(),marginLeft:parseInt(t.css('marginLeft'),10)+jQuery(window).scrollLeft()})}})};

jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	}
});

var slide_speed = 200;
var is_ie6 = /MSIE 6/i.test(navigator.userAgent);

var queue_stack_interval;
var main_menu_delay;
var submenu_delay;

var menu_closes_after = 300;

var primer = {
	
	required: function () {
	
		$(".js-clear-form", 'form').click( function () {
							
			var i = $(this);
											 
			do {
	
				i = $(i).parent();
	
				if ($(i).length == 0) break;
	
			} while (($(i)[0].tagName.toLowerCase() != 'form'));
	
			if ($(i)[0].tagName.toLowerCase() == 'form') {
	
				$(i).find('input[type="text"]').val('');
				$(i).find('textarea').val('');
	
			}
	
		});
		
		//- for some bullerproofing!
		
		function queueStack($item) {
			
			if (!$item.children("div").is(':animated')) {
				
				$item.addClass("act hover");
				$item.children(".subnav").slideDown(slide_speed);

				clearInterval(queue_stack_interval);
				
			}
			
		}
		
		if (is_ie6) {
			$("#main-menu > li.act").next().addClass("no-bg");
			$("#main-menu > li:first").addClass("no-bg");
		}
		
		$("#main-menu > li").hover( function () {
			
			clearTimeout(main_menu_delay);
			
			if ($("#main-menu > li").index($("#main-menu > li.has_children.hover")) !=  $("#main-menu > li").index($(this))) {
				closeMenu($("#main-menu > li.has_children.hover"));
			}
			
			if (!$(this).next().hasClass("act")) {
				$(this).next().addClass("no-bg");
				$(this).removeClass("no-bg");
			}
			
			if (!$(this).hasClass("has_children")) {
				
				$(this).addClass("hover act");
				
				return false;
				
			}
											
			var $current_li = $(this);
			
			if ($current_li.data('closing') == true) {
				
				$(this).stop();
				$(this).data('closing', false);
				
			}
			
			//- if we have animation still going on
			if ($(this).children(".subnav:first").is(':animated')) {
	
				clearInterval(queue_stack_interval);
				
				//- put in stack (there is only one animation at the time)
				queue_stack_interval = setInterval( function () { queueStack($current_li) }, 30 );
			
				return false;
			
			}
			
			$(this).addClass("hover act");
			
			$(this).children(".subnav").slideDown(slide_speed);
										
										
		}, function () {
			
			if (!$(this).hasClass("has_children")) {
				
				$(this).removeClass("act hover");
				$("#main-menu .makeact").addClass("act");
				
				$(this).next().removeClass("no-bg");
				$("#main-menu > li.makeact").next().addClass("no-bg");
				
				return false;
				
			}

			var $current_li = $(this);

			main_menu_delay = setTimeout(function () {
				
				closeMenu($current_li);
				
			}, menu_closes_after);
			
		});
		
		$(".subnav .bg-1 a").hover( function () {
			
			$(".subnav .bg-1 > a.act").removeClass("act");
											  
		}, function () {});
		
		$(".has_subchildren").hover( function () {
											   
			var sub_index = $(this).parent().children("a").index(this);
	
			$(this).next().addClass("top-" + (sub_index + 1)).show();
											   
		}, function () {
			
			var obj_sub = $(this).next();
			
			submenu_delay = setTimeout( function () {
												  
				closeSubmenu(obj_sub);
												  
			}, 100);
			
		});
		
		$(".subsubnav a").hover( function () {
										   
			clearTimeout(submenu_delay);
			
			$(this).parent().parent().prev().addClass("act");
										   
		}, function () {} );
		
		function closeMenu($index) { 

			$index.data('closing', true);
			
			$index.children(".subnav").slideUp(slide_speed, function () {
				
				if ($index.data('closing') == false) {
					return false;
				}
																							 
				$index.removeClass("act hover");
				
				$index.children("has_subchildren").removeClass("act");
				
				$index.find(".subsubnav").hide();
				
				$("#main-menu .makeact").addClass("act");
				
				$("#main-menu > li.no-bg").removeClass("no-bg");
				$("#main-menu > li.makeact").next().addClass("no-bg");
				
				if (!$index.hasClass("act") && ($index.data('closing') == true)) {
					$index.next().removeClass("no-bdr");
				}
				
				$index.data('closing', false);
				
			});
			
		}
		
		function closeSubmenu(jq_obj) {
			
			jq_obj.hide();
			
		}
		
		var current_slide = 0;
	
		setInterval( function () {
							   
			var $slider = $("#slider");
			
			if (current_slide == ($slider.find('a').length - 1)) {
				
				current_slide = -1;	
				
			}
			
			current_slide++;
			
			$slider.animate( { left: (current_slide) * -128 }, 600, 'easeOutQuad' );
							   
		}, 2000);
		
		var preload = new Array('next-on.gif', 'next.gif', 'prev-on.gif', 'prev.gif', 'top-tab-ov.gif', 'top-tab-on.gif', 'gallery-thumb-of.gif', 'gallery-thumb-ov.gif', 'buble-red.png', 'buble.png');
		var preloaded = new Array();
		
		for (var i = 0; i < preload.length; i++) { 
			 preloaded[i] = document.createElement('img');
			 preloaded[i].setAttribute('src', '/templates/img/' + preload[i]);
		}
		
	},
		
	index: function () {
		
		if ($(".title-icon").length != 0) {
			
			$(".title-icon").click( function () {
											  
				clearTimeout(iconAnimmationTimeout);
											  
				var index = $(".title-icon").index(this);
											  
				$(this).hide();
				$(".title-splash:eq(" + index + ")").fadeIn();
											  
			} );
			
			$("#click_area *").click( function () {
												
				var $el = $(this);
				
				$el.addClass('clicked');
				
				setTimeout( function () { $el.removeClass('clicked'); }, 50 );
												
			});
			
			$("#click_area").click( function () {
	
				if ($(this).find('.clicked').size() == 0) {
					
					$(".title-splash").fadeOut();
					$(".title-icon").fadeIn();
					
				}
											  
			});
		
			$(".title-icon").hover( function() {
		
				clearTimeout(iconAnimmationTimeout);
		
				$(this).animate( { marginTop: '-=5' }, 200 );
											 
			}, function () {
				
				iconAnimmationTimeout = setTimeout( function () { iconAnimation(); }, 500 + Math.round(Math.random() * (1200 - 500)));
				
				$(this).animate( { marginTop: '+=5' }, 200 );
		
			});
			
			function iconAnimation() {
												   
				var rand = Math.round(Math.random() * ($(".title-icon").length - 1));
				
				var speed = 120 + Math.round(Math.random() * (280 - 120));
				
				$(".title-icon:eq(" + rand + ")").animate( { marginTop: '-=5' }, speed);
				$(".title-icon:eq(" + rand + ")").animate( { marginTop: '+=5' }, speed);
				
				iconAnimmationTimeout = setTimeout( function () { iconAnimation(); }, 500 + Math.round(Math.random() * (1200 - 500)));
												   
			};
			
			var iconAnimmationTimeout = setTimeout( function () { iconAnimation(); }, 500 + Math.round(Math.random() * (1200 - 500)));
			
		}
		
	},
		
	vote: function () {
		
		$(".stars:not(.voted) a").click( function () {
									   
			$.post($('base').attr('href') + 'ajax/vote.php',
				   {
	
					   feedback_id: $(this).parent().parent().parent().attr("id"),
					   stars:	    $(this).parent().children("a").index(this) + 1,
					   vote: 		true
	
					},
				   function (data) { eval(data); });
	
			return false;
	
		});
	
		$(".stars:not(.voted) a").hover( function () {
	
			if ($(this).hasClass("voted")) return false;
									   
			var index = $(this).parent().children("a").index(this) + 1;
	
			$(this).parent().children("a.act").addClass("wasact").removeClass("act");
			$(this).parent().children("a").removeClass("on");
			
			$(this).parent().children("a:lt(" + index + ")").addClass("on");
									   
		}, function () {
			
			if ($(this).hasClass("voted")) return false;
	
			$(this).parent().children("a").removeClass("on")
			$(this).parent().children("a.wasact").removeClass("wasact").addClass("act");
			
		});
		
		$(".feedback-item > a").toggle( function () {
												 
			$(this).parent().children(".content").slideDown();
												 
		}, function () { 
		
			$(this).parent().children(".content").slideUp(); 
			
		});
		
		$("#form_idea").submit( function () {
										  
			$("#form_idea > div > div.error").removeClass("error");									  
	
			$.post($('base').attr('href') + 'ajax/idea.php', 
									{   fname:     $("#opt-1").val(),
										email:     $("#email").val(),
										elpastas:  $("#opt-2").val(),
										idea:      $("#opt-3").val(),
										title: 	   $("#opt-5").val(),
										parent:    $("#opt-4").val(),
										idea_submit: true }, function (data) { 
										
				eval(data); 
				
			} );
			
			return false;
											
		});
		
	},
	
	clickareas: function () {
		
		$(".plef").click( function () {
			
			$(this).find("a:first").click();
			
		});
		
		$(".day-photo, .your-idea, .mishab").hover( function () {
			
			var h = $('base:first').attr('href') + $(this).find("a:first").attr('href');
			
			window.status = h.replace('lt//', 'lt/');
		
			$(this).find("a").addClass("no-underline");
			
		}, function () {
	
			$(this).find("a").removeClass('no-underline');
			
			window.status = '';
			
		});
		
		$(".day-photo, .your-idea, .mishab").click( function () {
			
			var h = $(this).find("a:first").attr('href');
			
			document.location = h;
			
		});
		
	},
	
	gallery: function () {
		
		var photos_loaded = new Array();
		
		var current_page = 1;
		
		$("#ul_photo_paging a").click( function () {

			var new_page = $(this).html();
			
			if (new_page != current_page) {
				
				$("#ul_photo_paging a").removeClass("act");
				$(this).addClass("act");
				
				if ($("#ul_photo_paging a:eq(" + new_page + ")").length == 0) {
					$("#btn_next").css('visibility', 'hidden');
					$("#btn_prev").css('visibility', 'visible');
				} else if (new_page == 1) {
					$("#btn_prev").css('visibility', 'hidden');
					$("#btn_next").css('visibility', 'visible');
				}
				
				$("#photo_slider").animate( { left: (new_page - 1) * ($(".ul_photos:first").width()) * -1 }, 400, 'easeOutQuad', function () {
																		   
					current_page = new_page;
					
				});
				
			}
			
		});
		
		$("#btn_next").click( function () {
										
			$("#btn_prev").css('visibility', 'visible');
										
			if ($("#ul_photo_paging a:eq(" + (current_page + 1) + ")").length == 0) {
				
				$(this).css('visibility', 'hidden');
				
			}
				
			$("#ul_photo_paging a:eq(" + (current_page) + ")").click();
				
		});

		$("#btn_prev").click( function () {
										
			$("#btn_next").css('visibility', 'visible');

			if (current_page == 2) {
				
				$(this).css('visibility', 'hidden');

			}
			
			$("#ul_photo_paging a:eq(" + (current_page - 2) + ")").click();
										
		});
		
		var change_index = false;		
		
		$( function () {
					 
			if (photos.length != photos_loaded.length) {
				lazyLoad(12);
			}
					 
		});
		
		function lazyLoad(index) {

			if (photos_loaded[index] === true) return;
			if (photos[index] === undefined) return;
			
			$container = $("#photo_slider");
			
			var img = new Image();
			
			$(img).load( function () {

				$container.find('img:eq(' + index + ')')
					.removeClass("lazy")
					.css( { background: 'url(/i.php?f=uploads/gallery/' + photos[index] + '&w=184&h=142&m=4) center center no-repeat' } );
					
				photos_loaded[index] = true;

				if ($container.find('.lazy').length != 0) {
					if (change_index == false) {
						lazyLoad(index + 1);
					} else {
						lazyLoad(change_index);
					}
				}
				
			});
			
			
			
			img.src = '/i.php?f=uploads/gallery/' + photos[index] + '&w=184&h=142&m=4';
			
		}
		
		var current_photo = null;
		var photo_count = $("#photo_slider a").length;
		
		$("#photo_slider a").click( function () {
			
			if (/MSIE 6/i.test(navigator.userAgent)) {
				$("#select_gallery").hide();
			}
											  
			$("#dark").width($(document).width()).height($(document).height()).css( { opacity: 0.4 } ).fadeIn('fast');
			
			var y;
			if (self.pageYOffset)  {
				y = self.pageYOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {
				y = document.documentElement.scrollTop;
			} else if (document.body) {
				y = document.body.scrollTop;
			}

			$("#popup_photo").css('top', (y + 250)).fadeIn();
			
			current_photo = $("#photo_slider a").index(this);
			
			showPhotoInPopup(current_photo);
											  
		});
		
		var loadedImages = new Array();		
		
		function showPhotoInPopup(index) {

			var $img_current =  $("#photo_slider img:eq(" + index + ")");
			var photo_url = $img_current.css('background-image');
			
			photo_url.match(/i.php\?f=(.*?)\&w=/);
			photo_url = RegExp.$1;
			
			//alert(photo_url)
			
			if ($.inArray('i.php?f=' + photo_url + '&w=600&h=450&m=8&r=0&g=0&b=0', loadedImages) != -1) {
				
				$("#img_preview").fadeOut('fast', function () {
															
					$("#img_preview").attr( { 'src': 'i.php?f=' + photo_url + '&w=600&h=450&m=8&r=33&g=33&b=33', 'alt': $img_current.attr('alt') } ).fadeIn('fast');

				});
				
			} else {
			
				var img = new Image();
				
				$("#img_preview").fadeOut('normal', function () {

					$("#loading").fadeIn('fast');
					
					$(img).load( function () {
									   
						loadedImages.push('i.php?f=' + photo_url + '&w=600&h=450&m=8&r=33&g=33&b=33');
										   
						$("#loading").stop().fadeOut('fast');
										   
						$("#img_preview").attr( { 'src': img.src, 'alt': $img_current.attr('alt') } ).fadeIn('fast');
	
					});
					
					img.src = 'i.php?f=' + photo_url + '&w=600&h=450&m=8&r=33&g=33&b=33';

				});
															  
			}
			
			if (photo_count == (current_photo + 1)) {
			
				$('#popup_photo .next').hide();
				
			} else {
				
				$('#popup_photo .next').show();
				
			}
			
			if (0 == current_photo) {
			
				$('#popup_photo .prev').hide();
				
			} else {
				
				$('#popup_photo .prev').show();
				
			}
			
		}
		
		$("#btn_close_popup").click( function () { 
			$("#img_preview").hide(); 
			$("#popup_photo").fadeOut("fast"); $("#dark").fadeOut(); 
			if (/MSIE 6/i.test(navigator.userAgent)) {
				$("#select_gallery").show();
			}
		} );
		
		$('#popup_photo .next').click( function () {
												 
			showPhotoInPopup(++current_photo);
												 
		});

		$('#popup_photo .prev').click( function () {
												 
			showPhotoInPopup(--current_photo);			
												 
		});
		
		$("#select_year").change( function () {
											 
			document.location = $(this).val();
											 
		});
		$("#select_gallery").change( function () {
											 
			document.location = $(this).val();
											 
		});
		
		$("#dark").click( function () {
			
			$("#btn_close_popup").click();
			
		});
		
	}
	
}
	
$(function () {

	var module = $("#tag_body").attr("class").match(/(mod-)([a-zA-Z]+)/)[2];

	primer.required();
	
	if (module != 'blog') {
		primer.clickareas();
	}
	
	if (module == 'feedback') {
		primer.vote();
	}

	if (module == 'index') {
		primer.index();
	}
	
});

function printpage() {
	var url = document.location.href;
	
	for (i = url.length; i > 0; i--) {
		if (url.charAt(i) == '?') {
			url = url + '&';
			break;
		} else if ((url.charAt(i) == '/') || (url.charAt(i) == '\\')) {
			url = url + '?';
			break;
		}
	}

	win = window.open(url + 'do=print', 'popup', 'toolbar = 0, scrollbars = 1, status = 0');
	win.window.print();
}