/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


var app;
if (!app) {
    app = {};
}
var $dia = null;

(function () {
    app.loginForm = null;
    
    function initGallery() {
        if ( $('.slideshow').find("img").length > 0 ) {
            $dia = $('.slideshow').cycle({
                fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
                speed:         1500, 
                timeout:       3000
            });
        
            $(".dia-button-toggle").click(function () {
                if ( $(this).hasClass("play") ) {
                    $(this).removeClass("play");
                    $(this).addClass("pause");
                    $(".slideshow").cycle("pause");
                }
                else {
                    $(this).removeClass("pause");
                    $(this).addClass("play");
                    $(".slideshow").cycle("resume");
                }
                return false;
            })
            
            $(".dia-button-toggle").hover(function () {
                if ( $(this).hasClass("play") ) {
                    $(this).animate({
                        opacity: 0.8
                    }, 200);
                }
            }, function () {
                if ( $(this).hasClass("play") ) {
                    $(this).animate({
                        opacity: 0.4
                    }, 200);
                }
            });
        }
	
        $(".thumbnail").live('click', function () {
            var me = $(this);
            var nr = me.attr("ref");
            var oldNr = $(".big-image").attr("ref");
            //	    alert("img/viva-"+nr+".png");
            me.fadeOut('fast', function () {
                me.attr("src", "img/viva-"+oldNr.replace(/-([0-9])/, "-thumb-$1")+".png")
                me.attr("ref", oldNr);
                me.fadeIn();
            });

            $(".big-image").fadeOut('fast', function () {
                $(".big-image").attr("src", "img/viva-"+nr+".png");
                $(".big-image").attr("ref", nr);
                $(".big-image").fadeIn('fast');
            })
        });
    }
	
    function initLoginInput() {
        $("input.text").focus(function () {
            $(this).addClass("active");
        });
        $("input.text").blur(function () {
            $(this).removeClass("active");
        });
        $("textarea.text").focus(function () {
            $(this).addClass("active");
        });
        $("textarea.text").blur(function () {
            $(this).removeClass("active");
        });
	
        $(".container-login").find("input.text").focus(function () {
            var me = $(this);
				
            me.addClass("active");
				
            if ( (me.attr("name").toLowerCase() == "user" && me.val().toLowerCase() == "username") || (me.attr("name").toLowerCase() == "pass" && me.val().toLowerCase() == "password") ) {
                me.val("");
            }
        });
			
        $(".container-login").find("input.text").blur(function () {
            var me = $(this);
				
            me.removeClass("active");
				
            if ( me.attr("name").toLowerCase() == "user" && me.val().toLowerCase() == "" ) {
                me.val("Username");
            } else if ( me.attr("name").toLowerCase() == "pass" && me.val().toLowerCase() == "" ) {
                me.val("Password");
            }
        });
			
        $("input").corner("5px");
        $("textarea").corner("5px");
        $(".dia-background").corner("15px");
    }
	
    function initMenuEffects() {

        
        $(".fancy-img").fancybox({
            titleShow: false
        });
	
        $(".container-navigation").find("a").click(function () {
            var base = document.baseURI;
            var url = $(this).attr("href");
						
            if ( base.substr(base.length-1, base.length) != "/" ) base += "/";
            if ( url.substr(url.length-1, url.length) == "/" ) url = url.substr(0, url.length-1);

            if (History.getState().title == url) return false;



        //	    History.pushState({
        //		kat:url,
        //		rand:Math.random()
        //	    }, url, url+"/");
        //
        //	    $(".container-content-loading").show();
        //	    $.ajax({
        //		url: base + url,
        //		type: "GET",
        //		data: {
        //		    ajax : "yes"
        //		},
        //		dataType: "html",
        //		success: function (response) {
        //		    //					console.log(response);
        //		
        //		    $(".container-content").fadeOut('fast', function () {
        //			$(".container-content-loading").hide();
        //			
        //			$(".container-content").html( response );
        //						
        //			$(".container-content").fadeIn('fast');
        //		    });
        //		}
        //	    });
        //
        //	    
        //	    return false;
        });
    }
	
    if (typeof app.initEffects !== 'function') {
        app.initEffects = function () {
            initLoginInput();
            initMenuEffects();
            initGallery();
            
            
        }
    }
	
    if (typeof app.initValidation !== 'function') {
        app.initValidation = function () {
            app.loginForm = $("#loginForm").validate({
                submitHandler: function(form) {
                    $(form).ajaxSubmit({
                        target: "#loginFormResult",
                        success: function (response) {
                            var result = json_parse(response);
							
                            if (result.isValid === true) {
                                $("#loginFormResult").html( "&nbsp;" );
                                var base = document.baseURI;
						
                                window.location.href = base+'Shop/';
                            } else {
                                $("#loginFormResult").html( result.msg );
                            }
                        }
                    });
                }
            });
	    
            app.contactForm = $("#contactForm").validate({
                errorLabelContainer: function () {
                    this.defaultShowErrors();
                    return true;
                }
            });
        }
    }
}());




$(document).ready(function () {
	
	
    app.initEffects();
    app.initValidation();
    
    if ($(".sub-nav").length > 0) {
        $(".sub-nav").slideDown();
    }
	
	
//	if (window.location.hash.length > 0) {
//		var urlHash = window.location.hash;
//		var base = document.baseURI;
//		var url = "";
//		
//		if ( urlHash != "" && urlHash != "#" ) {
//			url = urlHash.substr(1, urlHash.length) + "/";
//		}
//		
//		if ( url != "" && url != "/" ) {
//			$.ajax({
//				url: base + url,
//				type: "GET",
//				data: {
//					ajax : "yes"
//				},
//				dataType: "html",
//				success: function (response) {
//					//					console.log(response);
//					$(".container-content").html( response );
//					console.log("ajax done");
//				}
//			});
//		}
//	//		console.log(urlHash);
//	}
});
