(function($){
    $(document).ready(function(){

		// PASSWORD FIELD
		$("input[name=post_password]").each(function() {
			$(this).addClass("first-gradient").css("padding","5px").css("border","1px #6b6762 solid").css("margin-bottom","5px");
			$(this).parent().parent().children("input[name=Submit]").addClass("second-gradient").css("padding","5px").css("border","1px #6b6762 solid");
		});
	
		// PDF FILE FRAME
		$(".pdf-file-container").addClass("box");
		$(".pdf-file-container").wrapAll('<div class="pdf-file-container-container" />');
		$(".pdf-file-container:nth-child(2n+1)").css("clear","both");
		$(".pdf-file-container:nth-child(2n)").css("float","right").css("margin-right","0");
		
		
		// FIX IE6 POSITION FIXED => ABSOLUTE
		if($.browser.msie && $.browser.version=="6.0") {
			$("body").css("width","100%").css("height","100%").css("position","relative");
			$(".usniezkow-background-image-wrap").css("position","absolute");
			$("li.usniezkow-background-image-container img").css("position","absolute");
			$("#expresso-front-menu-container-wrap").css("position","absolute");
			$("#expresso-page-header-floating").css("position","absolute");
		}
	
		// WEATHER WIDGET
		var weather_description = $("#right-sidebar-weather-text-container").html();
		$("#right-sidebar-weather-text-container").remove();
		
		$(".wp-forecast-curr-details").append(weather_description);
	
		// FRONT ANIMATION
		$("#expresso-front-menu > ul > li:last-child").addClass("last-li");
		$("#expresso-front-menu > ul > li").hover(
			function() {
				$(this).addClass("li-hover");
				$(this).children("a").stop(true,true).animate({
					top: '-230px'
				});
			},
			function() {
				$(this).removeClass("li-hover");
				$(this).children("a").stop(true,true).animate({
					top: '0px'
				});
			}
		);
		// BACKGROUND IMAGE SIZE
		function background_image_control() {
			$("li.usniezkow-background-image-container img").each(function() {
				var $winwidth = parseFloat($(window).width());
				var $winheight = parseFloat($(window).height());
				var $imagewidth = parseFloat($(this).width());
				var $imageheight = parseFloat($(this).height());

				if ($imageheight > $winheight) {
					$(this).css("width","100%").css("height","auto");
				};
				if (($imageheight < $winheight) && ($imagewidth >= $winwidth)) {
					$(this).css("height","100%").css("width","auto");
				};
				if ($winwidth > $imagewidth) {
					$(this).css("width","100%").css("height","auto");
				};
			});
		};
		$(window).load(function() {
			background_image_control(); 
			$(window).bind("resize", function(){
				background_image_control(); 
				});
		});
		
		// WIDGET CLEANING
		$("div.wp-forecast-copyright").remove();
		$("div.wp-forecast-curr-head div a").removeAttr("href").wrap("<h3/>");
		
		// 00. GLOBAL CUNTIONS
		
		
		// Functions used in many places
		
		// updateGlobalColors(); Used for updating global colors when working with configurator
			function updateGlobalColors(hex) {
				// Create dynamic variables
				var gstart = $("input#expresso_gradient_start").val();
				var gend = $("input#expresso_gradient_end").val();
				var gcolor = $("input#expresso_gradient_color").val();
				var altstart = $("input#expresso_alt_gradient_start").val();
				var altend = $("input#expresso_alt_gradient_end").val();
				var altcolor = $("input#expresso_alt_gradient_color").val();
				var menuheight = parseFloat($("#expresso_menu_height").val());
						
				// Update input formatting
				$("input.currently-editing").val(hex).css("background","#"+hex).css("color","#"+hex);

				// Generate dynamic change
				$("#temp-stylesheet style").empty();
				$("#temp-stylesheet style")
					.append("#expresso-page-header-menu-wrap-wrap {background: #"+gstart+" url('http://www.usniezkow.pl/wp-content/themes/33-usniezkow-final/expresso/gradient.php?start="+gstart+"&end="+gend+"&height="+menuheight+"&width=1') repeat-x;background-position: center bottom;color: #"+gcolor+";}")
					.append("ul#expresso-page-header-menu > li {border-right: 1px #"+gend+" solid;}")
					.append("ul#expresso-page-header-menu li a {color: #"+gcolor+";}")
					.append("ul#expresso-page-header-menu > li > a {border-right: 1px #"+gstart+" solid;}")
					.append("ul#expresso-page-header-menu li a:hover {background: #"+altstart+" url('http://www.usniezkow.pl/wp-content/themes/33-usniezkow-final/expresso/gradient.php?start="+altstart+"&end="+altend+"&height="+menuheight+"&width=1') repeat-x;background-position: center bottom;color: #"+altcolor+";}")
					.append("ul#expresso-page-header-menu > li.currently-hovered {border-right: 1px #"+altstart+" solid;}")
					.append("ul#expresso-page-header-menu > li.currently-hovered > a {border-right: 1px #"+altend+" solid;}")
					.append("ul#expresso-page-header-menu ul {border-right: 1px #"+altstart+" solid;border-left: 1px #"+altstart+" solid;border-bottom: 1px #"+altstart+" solid;}")
					.append("ul#expresso-page-header-menu ul li a {background: #"+altstart+" url('http://www.usniezkow.pl/wp-content/themes/33-usniezkow-final/expresso/gradient.php?start="+altstart+"&end="+altend+"&height=40&width=1') repeat-x;background-position: center bottom;color: #"+altcolor+";border-top: 1px #"+altend+" solid;}")
					.append("ul#expresso-page-header-menu ul li a:hover {background: #"+gstart+" url('http://www.usniezkow.pl/wp-content/themes/33-usniezkow-final/expresso/gradient.php?start="+gstart+"&end="+gend+"&height=40&width=1') repeat-x;background-position: center bottom;color: #"+gcolor+";}")
					.append("ul#expresso-page-header-menu li.currently-hovered a.currently-hovered{background: #"+altstart+" url('http://www.usniezkow.pl/wp-content/themes/33-usniezkow-final/expresso/gradient.php?start="+altstart+"&end="+altend+"&height=40&width=1') repeat-x;background-position: center bottom;color: #"+altcolor+";}")
					.append("ul#expresso-page-header-menu li ul li.currently-hovered a.currently-hovered{background: #"+gstart+" url('http://www.usniezkow.pl/wp-content/themes/33-usniezkow-final/expresso/gradient.php?start="+gstart+"&end="+gend+"&height=40&width=1') repeat-x;background-position: center bottom;color: #"+gcolor+";}")
					.append("ul#expresso-page-header-menu > li > a.expresso-current-page{background: #"+gend+" url('http://www.usniezkow.pl/wp-content/themes/33-usniezkow-final/expresso/gradient.php?start="+gend+"&end="+gstart+"&height=40&width=1') repeat-x;background-position: center bottom;color: #"+gcolor+";}")
					.append("ul#expresso-page-header-menu > li > a {text-shadow: #"+gend+" 1px 1px 1px;}ul#expresso-page-header-menu li a:hover,ul#expresso-page-header-menu li:hover > a {text-shadow: #"+altstart+" 1px 1px 1px;}ul#expresso-page-header-menu ul li a {text-shadow: #"+altstart+" 1px 1px 1px;}ul#expresso-page-header-menu ul li a:hover {text-shadow: #"+gend+" 1px 1px 1px;}")
					


						
									
					.append("div#expresso-footer-widgets-container-wrap {background: #"+gstart+" url('http://www.usniezkow.pl/wp-content/themes/33-usniezkow-final/expresso/gradient.php?start="+gstart+"&end="+gend+"&height=70&width=1') repeat-x;background-position: center bottom;color: #"+gcolor+";}")
					.append("div#expresso-second-menu-container-wrap {background: #"+altstart+" url('http://www.usniezkow.pl/wp-content/themes/33-usniezkow-final/expresso/gradient.php?start="+altstart+"&end="+altend+"&height=30&width=1') repeat-x;background-position: center bottom;color: #"+gcolor+";}")
					.append("div#expresso-second-menu-container-wrap a {color: #"+altcolor+";}")
					.append("div#expresso-copyright-container-wrap {background: #"+altstart+" url('http://www.usniezkow.pl/wp-content/themes/33-usniezkow-final/expresso/gradient.php?start="+altstart+"&end="+altend+"&height=30&width=1') repeat-x;background-position: center bottom;color: #"+gcolor+";}");
			};
		
		// updateMenuDimensions(); Used for updating Main Menu Dimensions while working with configurator
			function updateMenuDimensions() {
				var menuheight = parseFloat($("#expresso_menu_height").val());
				var menupaddingleft = parseFloat($("#expresso_menu_padding_left").val());
				var menupaddingright = parseFloat($("#expresso_menu_padding_right").val());
				var menufontsize1 = parseFloat($("#expresso_menu_first_font_size").val());
				var menufontsize2 = parseFloat($("#expresso_menu_second_font_size").val());
				var menupaddingtop = Math.round((menuheight - menufontsize1)/2);
				var menupaddingbottom = Math.round((menuheight - menufontsize1)/2);
				var gstart = $("input#expresso_gradient_start").val();
				var gend = $("input#expresso_gradient_end").val();
				var gcolor = $("input#expresso_gradient_color").val();
				var altstart = $("input#expresso_alt_gradient_start").val();
				var altend = $("input#expresso_alt_gradient_end").val();
				var altcolor = $("input#expresso_alt_gradient_color").val();
							
				$("#temp-menu-stylesheet style").empty();
				$("#temp-menu-stylesheet style")
					.append("ul#expresso-page-header-menu li a {padding-left: "+menupaddingleft+"px;padding-right: "+menupaddingright+"px;padding-top: "+menupaddingtop+"px;padding-bottom: "+menupaddingbottom+"px;font-size: "+menufontsize1+"px;}")
					.append("ul#expresso-page-header-menu ul {top: "+(menuheight+2)+"px;}")
					.append("ul#expresso-page-header-menu ul li a {font-size: "+menufontsize2+"px;}");
					
				$("#temp-stylesheet style")
					.append("#expresso-page-header-menu-wrap-wrap {background: #"+gstart+" url('http://www.usniezkow.pl/wp-content/themes/33-usniezkow-final/expresso/gradient.php?start="+gstart+"&end="+gend+"&height="+menuheight+"&width=1') repeat-x;background-position: center bottom;color: #"+gcolor+";}")
					.append("ul#expresso-page-header-menu li a:hover {background: #"+altstart+" url('http://www.usniezkow.pl/wp-content/themes/33-usniezkow-final/expresso/gradient.php?start="+altstart+"&end="+altend+"&height="+menuheight+"&width=1') repeat-x;background-position: center bottom;color: #"+altcolor+";}")
					
					
			};
		
		
		
		
		// 01. MAIN MENU
		// Menu on the top of the page

			// 01a. FIX FOR WHEN MENU IS A WIDGET
			$("ul#expresso-page-header-menu div").unwrap();
			$("div#expresso-page-header-menu-wrap div ul:first").attr('id','expresso-page-header-menu');
		
			// 01b. ANIMATIONS
				// First level animations
					$("ul#expresso-page-header-menu > li").hover(
						function() {
							$(this).children("ul").stop(true, true).slideDown(300);
						},
						function() {
							$(this).children("ul").stop(true,true).slideUp(100);
						}
					);
					
				// All levels but firstChild
					$("ul#expresso-page-header-menu li ul li").hover(
						function() {
							$(this).children("ul").stop(true,true).show(200);
						},
						function() {
							$(this).children("ul").stop(true,true).hide(100);
						}
					);
				
			// 01c. IE fix
				$("ul#expresso-page-header-menu li").hover(
					function() {
						$(this).addClass("hovered");
						$(this).children("ul").addClass("ul-hovered");
					},
					function() {
						$(this).removeClass("hovered");
						$(this).children("ul").removeClass("ul-hovered");
					}
				);
			
			// 01d. CORNERS
				$("ul#expresso-page-header-menu li").each(function() {
					// Overall Radius
					$(this).children("ul").addClass("shadow").addClass("bottomradius");
					
					// Bottom Radius
					$(this).children("ul").children("li:last").addClass("bottomradius");
					$(this).children("ul").children("li:last").children("a").addClass("bottomradius");

					// Top Corners of menu
					// $("#expresso-page-header-menu-wrap-wrap").addClass("topradius");
					// $("#expresso-page-header-menu-wrap-wrap-wrap li:first").addClass("topleftradius");
					// $("#expresso-page-header-menu-wrap-wrap-wrap li:first a:first").addClass("leftradius");
					
				});
			
			// 01e. CURRENT
				$("ul#expresso-page-header-menu li").hover(
					function() {
						$(this).addClass("currently-hovered");
						$(this).children("a").addClass("currently-hovered");

					},
					function() {
						$(this).removeClass("currently-hovered");
						$(this).children("a").removeClass("currently-hovered");

					}
				);
				$("ul#expresso-page-header-menu li.current_page_item, ul#expresso-page-header-menu li.current_page_ancestor").each(function() {
					$(this).addClass("expresso-current-page");
					$(this).children("a").addClass("expresso-current-page");
				});
			
			// 01f. LAST CLASSES
				$("#expresso-page-header-menu ul li:last-child").addClass("menu-last-li");

			// 01g. NAVIGATION ARROWS
				$("#expresso-page-header-menu > li:has('ul') > a").append("<span class='arrow'></span>");
				$("#expresso-page-header-menu li ul li:has('ul') > a").append("<span class='arrow'></span>");
			// 01h. LAST UL
				$("#expresso-page-header-menu > li:last-child > ul").addClass("last-ul");

				
		// 02. SECOND MENU
		// Secone menu at the bottom of the page
		
			// 02a. FIX FOR WHEN SECOND MENU IS A WIDGET
			$("div#expresso-second-menu-container ul div").unwrap();
			$("div#expresso-second-menu-container div ul:first").unwrap();
		
			// 02b. ANIMATE FIRST LEVEL SUBMENU
				// First level animations
					$("div#expresso-second-menu-container > ul > li").hover(
						function() {
							$(this).children("ul").stop(true, true).slideDown(300);
						},
						function() {
							$(this).children("ul").stop(true,true).slideUp(100);
						}
					);
			// 02c. ANIMATE SECOND LEVEL SUBMENU
					$("div#expresso-second-menu-container ul li ul li").hover(
						function() {
							$(this).children("ul").stop(true,true).show(200);
						},
						function() {
							$(this).children("ul").stop(true,true).hide(100);
						}
					);
			// 02d. ADD CORNERS TO MENU
				$("div#expresso-second-menu-container").addClass("box");
				$("div#expresso-second-menu-container > ul").addClass("box");
				$("div#expresso-second-menu-container > ul > li:first-child").addClass("leftradius");
				$("div#expresso-second-menu-container > ul > li:first-child > a").addClass("leftradius");
				$("div#expresso-second-menu-container > ul > li:last-child").addClass("rightradius");
				$("div#expresso-second-menu-container > ul > li:last-child > a").addClass("rightradius");
				$("div#expresso-second-menu-container > ul > li:first-child").hover(
					function() {
						$(this).toggleClass("leftradius").toggleClass("topleftradius");
						$(this).children("a").toggleClass("leftradius").toggleClass("topleftradius");
					},
					function() {
						$(this).toggleClass("leftradius").toggleClass("topleftradius");
						$(this).children("a").toggleClass("leftradius").toggleClass("topleftradius");
					}
				);
				$("div#expresso-second-menu-container > ul > li:last-child").hover(
					function() {
						$(this).toggleClass("rightradius").toggleClass("toprightradius");
						$(this).children("a").toggleClass("rightradius").toggleClass("toprightradius");
					},
					function() {
						$(this).toggleClass("rightradius").toggleClass("toprightradius");
						$(this).children("a").toggleClass("rightradius").toggleClass("toprightradius");
					}
				);		
				
				
				
				
				$("div#expresso-second-menu-container ul li").each(function() {
					// Overall Radius
					$(this).children("ul").addClass("shadow").addClass("bottomradius");
					
					// Bottom Radius
					$(this).children("ul").children("li:last").addClass("bottomradius");
					$(this).children("ul").children("li:last").children("a").addClass("bottomradius");

					// Top Right Radius for last ul
					
				});
			// 02e. FLOAT LAST ELEMENT TO THE RIGHT
				$("div#expresso-second-menu-container > ul > li:last-child").addClass("floatright").addClass("no-right-border");
			
			// 02e. CURRENT
				$("div#expresso-second-menu-container ul li.current_page_item, div#expresso-second-menu-container ul li.current_page_ancestor")
				.not("div#expresso-second-menu-container > ul > li.current_page_item")
				.not("div#expresso-second-menu-container > ul > li.current_page_ancestor")
				.each(function() {
					$(this).addClass("expresso-current-page");
					$(this).children("a").addClass("expresso-current-page");
				});
			$("div#expresso-second-menu-container > ul > li.current_page_item, div#expresso-second-menu-container > ul > li.current_page_ancestor").each(function() {
					$(this).addClass("expresso-current-page-first");
					$(this).children("a").addClass("expresso-current-page-first");
				});
			// 02f. LAST CLASSES
				$("div#expresso-second-menu-container > ul > li:last-child > ul").addClass("second-last-ul");
			// 02g. HOVER CLASSESS
				$("div#expresso-second-menu-container > ul > li").hover(
					function() {
						$(this).addClass("first-li-hover");
						$(this).children("a").addClass("first-a-hover");
					},
					function() {
						$(this).removeClass("first-li-hover");
						$(this).children("a").removeClass("first-a-hover");
					}
				);
				$("div#expresso-second-menu-container ul li li").hover(
					function() {
						$(this).addClass("li-hover");
						$(this).children("a").addClass("a-hover");
					},
					function() {
						$(this).removeClass("li-hover");
						$(this).children("a").removeClass("a-hover");
					}
				);
				
			// 02h. CHILDREN CLASSESS
				$("div#expresso-second-menu-container > ul > li").addClass("first-level-li");
				$("div#expresso-second-menu-container > ul > li > a").addClass("first-level-a");

			// 02i. NAVIGATION ARROWS
				$("div#expresso-second-menu-container > ul > li:has('ul') > a").append("<span class='arrow'></span>");
				$("div#expresso-second-menu-container ul li ul li:has('ul') > a").append("<span class='arrow'></span>");	
			
		// 03. ANIMATION
			// START ANIMATION
				// CODE FOR JQUERY CYCLE
									$(window).load(function() {
						  if ($.browser.webkit) {
							$("#usniezkow-background-image").cycle({
								fx: 'fade',
								speed: 4000,
								pager: "#usniezkow-background-navigation",
								timeout: 15000,
								after: function() {
									background_image_control();
								}
								
							});
						  } else {
							$("#usniezkow-background-image").cycle({
								fx: 'none',
								pager: "#usniezkow-background-navigation",
								timeout: 15000, 
								after: function() {
									background_image_control();
								}
							});
						  };

						$("div#usniezkow-background-navigation a").empty();
					});
								
				// CODE FOR NIVO SLIDER
									
		// 04. RIGHT SIDEBAR
			$("h3.expresso-sidebar-widget-title").addClass("topradius");
			$("div.expresso-sidebar-widget-container").addClass("bottomradius");
				
		// 06. BREADCRUMBS
			// Add class bold to the first element
			$("#expresso-breadcrumbs span:last").addClass("bold");
			
			// Add bottom corners
			// $("#expresso-breadcrumbs-container-wrap").addClass("bottomradius");
			
			// Animation Toggle
			$("div#expresso-animation-toggle").click(function() {
				$("div#expresso-show-animation").toggle();
				$("div#expresso-hide-animation").toggle();
				$("div#expresso-animation-container-wrap-wrap-wrap").slideToggle(500);
			});
				
				
		// 07. FOOTER WIDGETS
			// 07a. FIRST CLASS
			$("div.expresso-footer-widget-wrap:first").addClass("first-footer-widget-wrap");
			$("div.expresso-footer-widget:first").addClass("first-footer-widget");
			
			$("div.expresso-footer-widget-wrap:nth-child(2)")
				.addClass("second-footer-widget-wrap")
				.children("div.expresso-footer-widget")
				.addClass("second-footer-widget");
		
			$("div.expresso-footer-widget-wrap:nth-child(2)")
				.addClass("third-footer-widget-wrap")
				.children("div.expresso-footer-widget")
				.addClass("third-footer-widget");
		
			$("div.expresso-footer-widget-wrap:nth-child(2)")
				.addClass("fourth-footer-widget-wrap")
				.children("div.expresso-footer-widget")
				.addClass("fourth-footer-widget");
		
			$("div.expresso-footer-widget-wrap:nth-child(2)")
				.addClass("fifth-footer-widget-wrap")
				.children("div.expresso-footer-widget")
				.addClass("fifth-footer-widget");
		
		// 08. COPYRIGHT BAR
			// 08a. CURRENT CLASSESS
			$("div#expresso-copyright-container ul li.current_page_item, div#expresso-copyright-container ul li.current_page_ancestor").each(function() {
					$(this).addClass("expresso-current-page");
					$(this).children("a").addClass("expresso-current-page");
				});
			// 08b. HOVER CLASSESS
				$("div#expresso-copyright-container ul li").hover(
					function() {
						$(this).addClass("li-hover");
						$(this).children("a").addClass("a-hover");
					},
					function() {
						$(this).removeClass("li-hover");
						$(this).children("a").removeClass("a-hover");	
					}
				);
			// 08c. FIRST CLASS
				$("div#expresso-copyright-container ul li:first").addClass("first-li");
				$("div#expresso-copyright-container ul li:first a").addClass("first-a");
				$("div#expresso-copyright-container ul li:first").hover(
					function() {
						$(this).addClass("first-li-hover");
						$(this).children("a").addClass("first-a-hover");
					},
					function() {
						$(this).removeClass("first-li-hover");
						$(this).children("a").removeClass("first-a-hover");
					}
				);
				
				
				
		// 09. GLOBAL SCRIPTS
		// Used to handle the whole page and its elements
			
			// Show page after jQuery is executed
				$("body").show();
		
							
					
					
					
					
					
					
					
					
					
					
					
					
					
					
					
					
					
					
					
					
					
					
					
					
					
					
					
				
				
				$(window).load(function() {
			$("#expresso-body").show();
		});
});
})(jQuery);
