(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

$(document).ready(function(){
	$('#preload_status').show();
	
	jQuery.preLoadImages("images/content_shadow.png", "images/top_bg.jpg", "images/content_transparent.png", "images/logo1.png", "images/logo.png", "images/thumbs_bg.png", "images/body_bg.gif", "images/loading.gif", "images/loading_bg.png");
});

$(window).load(function () {

    $('#preload_status').remove();

    jQuery.preLoadImages("images/content_bg12.jpg", "images/content_bg11.jpg", "images/content_bg10.jpg", "images/content_bg9.jpg", "images/content_bg8.jpg", "images/content_bg7.jpg", "images/content_bg6.jpg", "images/content_bg4.jpg", "images/content_bg5.jpg", "images/content_bg3.jpg", "images/content_bg1.jpg", "images/content_bg2.jpg");

    var newHash = "",
        $mainContent = $("#content"),

	    scrollPaneInit =
			function () {
			    $('.scroll-pane').jScrollPane({
			        verticalDragMinHeight: 96,
			        verticalDragMaxHeight: 96
			    });
			},

		lightboxInit =
		    function () {
		        $("a[rel^='prettyPhoto']").prettyPhoto({
		            opacity: 0.50
		        });
		    },

		$logoFirstLine = $('#logo .first_line'),
	    $logoSecondLine = $('#logo .second_line'),
		$loading = $("#loading"),
        $kenticoLogo = $("#kenticoLogo"),

		animateLogo =
			function () {
			    $logoSecondLine.animate({ left: "0px" }, 'fast',
					function () {
					    $logoFirstLine.animate({ top: "5px" }, 'fast');
					});
			},

        animateKenticoLogo =
			function () {
			    $kenticoLogo.animate({ left: "0px" }, 'fast');
			},

	    animateMenu =
			function () {
			    var d = 300;

			    li = $('nav').children('ul').children('li')
			    li.eq(-1).addClass('last')
			    li.each(
				function () {
				    $(this).animate({ right: "0px" }, d += 100, function () {
				        if ($(this).is(".last")) {
				            $('.submenu').contents('li.active').parent().delay(50).slideDown(150);
				        }
				    });
				});
			},

	    animateTextBlock =
			function () {
			    $('.text_block').animate({ left: "-40px" }, 250, function () {
			        $(this).animate({ left: 0 }, 280);
			        $loading.hide();
			    });
			},

        animateDoubleTextBlock =
			function () {
			    $('.doubleWidth').animate({ left: "-40px" }, 250, function () {
			        $(this).animate({ left: 0 }, 280);
			        $loading.hide();
			    });
			},

        slideGalNav =
			function () {
			    $('.gallery_nav').animate({ right: 0 }, 500);
			},

	    initGallery =
			function () {
			    var $gal = $('#gallery'),
				    img_link = $gal.find('.thumbs li:first-child').children('a').attr("href");

			    $gal.find('.thumbs li:first-child').addClass("current").find('img').fadeTo('500', 0);

			    $loading.show();

			    _img = $('<img class="big_img">')
			    _img.css({ left: "100px", top: "30px", opacity: 0 }).load(
					function () {
					    $loading.hide();
					    $gal.prepend(this);
					    $(this).animate({ left: "17px", top: 0, opacity: 1 }, 500);
					})
					.attr({ src: img_link });



			    $gal.children('.gallery_nav').tinycarousel({ axis: 'y' });

			    $gal.find("ul.thumbs li").each(
				    function () {
				        var thumbUrl = $(this).find("img").attr("src");
				        $(this).children().css({ 'background': 'url(' + thumbUrl + ') no-repeat right top' });
				    }
				);

			    $gal.hover(
					function () {
					    $('.gallery_nav', this).stop().animate({ right: 0 }, 'fast')
					},
					function () {
					    $('.gallery_nav', this).stop().animate({ right: "-134px" }, 'fast')
					}
				);
			};

    function toggleSubmenu() {
        $(this).next('.submenu').slideToggle(150);
        $(this).parent().siblings().not('.active').find('.submenu').slideUp(150);
        return false
    };

    function SetMetaValue(meta_value) {

        var my_arr = document.getElementsByTagName("meta");
        for (var counter = 0; counter < my_arr.length; counter++) {
            if (my_arr[counter].name.toLowerCase() == 'description') {
                my_arr[counter].content = meta_value;
            }
        }
    };


    $logoFirstLine.css({ top: "-100px" });
    $logoSecondLine.css({ left: "229px" });
    $kenticoLogo.css({ left: "229px" });
    $('.text_block').css({ left: "619px" });
    $('nav').children('ul').children('li').css({ right: "-169px" });

    scrollPaneInit();
    lightboxInit();
    contactInit();

    $('#content_shadow, #main').hide();
    $('#top_bg').css({ backgroundPosition: "0px -410px" }).animate({ backgroundPosition: "0px 0px" }, 'normal');

    $('#content_shadow').slideDown('normal',
				function () {
				    $('#main').fadeIn(500,
						function () {
						    initGallery();
						    animateLogo();
						    animateMenu();
						    animateKenticoLogo();
						    animateTextBlock();
						    animateDoubleTextBlock();
						    slideGalNav();
						    scrollPaneInit();
						})
				}
	);

		$("nav").delegate("a:not(.noHash)", "click", function () {
        window.location.hash = $(this).attr("href");
        return false;
    });

    $mainContent.delegate("a.listLinkBottom", "click", function () {
        window.location.hash = $(this).attr("href");
        return false;
    });

    $mainContent.delegate("a.listLink", "click", function () {
        window.location.hash = $(this).attr("href");
        return false;
    });

    $(".copy").delegate("a.privacy", "click", function () {
        window.location.hash = $(this).attr("href");
        return false;
    });

    $mainContent.delegate("a.more", "click", function () {
        window.location.hash = $(this).attr("href");
        return false;
    });

    $mainContent.delegate("a.socialLink", "click", function () {
        window.location.hash = $(this).attr("href");
        return false;
    });

    $mainContent.delegate("#gallery .thumbs a", "click", function () {

        if ($(this).parent().is(".current")) {
            return false
        }
        else {
            $(this).parent().addClass("current").find("img").css({ opacity: 0 }).parent().parent().siblings(".current").removeClass("current").find("img").css({ opacity: 1 });
            var img_link = $(this).attr("href");
            var $gal = $('#gallery');
            $loading.show();
            $gal.children('.big_img').animate({ left: "-40px", top: "40px", opacity: 0 }, 500, function () {
                $(this).remove()
            });

            _img = $('<img class="big_img">')
            _img.css({ left: "100px", top: "30px", opacity: 0 }).load(
				function () {
				    $loading.hide();
				    $gal.prepend(this);
				    $(this).animate({ left: "17px", top: 0, opacity: 1 }, 500);
				})
				.attr({ src: img_link });

            return false;
        }
    });

    $mainContent.delegate("#gallery .thumbs li:not(.current) a", "mouseenter",
				    function () {
				        $(this).children().stop().animate({ opacity: 0 }, 500);
				    })
				.delegate("#gallery .thumbs li:not(.current) a", "mouseleave",
					function () {
					    $(this).children().stop().animate({ opacity: 1 }, 500);
					});



    /*$('.show_sub').click(
    toggleSubmenu
    );*/

    $(window).bind('hashchange', function () {

        newHash = window.location.hash.substring(1);

        if (newHash) {
            $loading.show();
            $mainContent
                .children("#content_wrap")
                .fadeOut(200, function () {
                    $mainContent.hide().load(newHash + " #content_wrap", function () {
                        $('.text_block').css({ left: "619px" });
                        $mainContent.fadeIn(200, function () {
                            scrollPaneInit();
                            lightboxInit();
                            contactInit();
                            initGallery();
                            slideGalNav();
                            animateKenticoLogo();
                            animateTextBlock();
                            animateDoubleTextBlock();

                        });
                        $("nav li").removeClass("active");
                        $("nav a[href=" + newHash + "]").parent("li").addClass("active");
                        Cufon.refresh();
                        var r = $('#title_text_marker').text();
                        //alert('Excelium :: ' + r);
                        document.title = 'Excelium :: ' + r;

                        var des = $('#title_text_marker').text();
                        SetMetaValue('Something else ' + des);
                    });
                });
        };

    });

    $(window).trigger('hashchange');
});
