/*
	copyright 2009 getconfused.net
*/
var Anuk = function ()  
{
    Anuk.scope = this;
}

var a = Anuk;

a.prototype.init = function()
{
    var _self = this;
//    $(window).bind('resize', this.resize);

    $('.page').click(Anuk.scope.page);

    $.ajax({
         type: "POST",
         url: 'collections.php',
         dataType: "xml",
         success: function(xml) {
            _self.setMenu(xml);
         },
         error: function(xhr, desc, exceptionobj) {
             console.log(xhr);
         }
     });    
         
    $('.menu li').live('click', function(){
        
        if ( $(this).find('ul').size() > 0) return;
        
        $('.menu li').removeClass('selected');
        $(this).addClass('selected');
    })
    
    $("#sendMailFrm").submit(function() {
        return false;
    });

	$("#sendEmail").click(
	    function(){ 
    		$(".error").hide();
    		var hasError = false;
    		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				
    		var emailFromVal = $("#email").val();
    		if(emailFromVal == '') {
    			$("#email").after('<span class="error">You forgot to enter the email address to send from.</span>');
    			hasError = true;
    		} else if(!emailReg.test(emailFromVal)) {	
    			$("#email").after('<span class="error">Enter a valid email address to send from.</span>');
    			hasError = true;
    		}
		
    		var nameVal = $("#firstname").val();

    		var messageVal = $("#message").val();
    		if(messageVal == '') {
    			$("#message").after('<span class="error">You forgot to enter the message.</span>');
    			hasError = true;
    		}

    		if(hasError == false) {
    			$(this).hide();
    			$("#sendEmail li.buttons").append('<img src="images/loading.gif" alt="Loading" id="loading" />');
    			$.post("sendemail.php",
       				{ emailFrom: emailFromVal, name: nameVal, message: messageVal },
    				function(data){
        				$("#sendMailFrm").slideUp("normal", function() {
                            $("#sendMailFrm").before('<p><strong>Thanks !</strong><br/>Thank you for your email, we will answer you shortly.</p>');
        				});
    				}
    		    );
                return false;		    
    		}
    		return false;
	    }
	);						   

    this.intro();

//    this.resize();
}

a.prototype.intro = function()
{
    var t = $('#p_home').html();

    $('#p_home')
        .html('<img src="'+PATH+'public/themes/anuk/images/home_logo.gif" alt="" height="480" />')
        .fadeIn(3000, function(){
            $(this).fadeOut(1000, function(){
                 $(this).html(t)
                 $(this).fadeIn(5000);
                 $('#logo').fadeIn(200);
                 $('ul.menu ').slideDown(2000);
                 $('ul.menu li:first ').addClass('selected');
             });
        });


/*$('#logo').fadeIn(1500, function(){
//        $(this).fadeOut(1000, function(){
//             $(this).html(t)
//             $(this).fadeIn(2000);
            $('#p_home').fadeIn(2000, function(){
                $('ul.menu ').slideDown(2000);
                $('ul.menu li:first ').addClass('selected');                
            });
//         });
    });
*/

}


a.prototype.setMenu = function(xml)
{
    var ul = $('<ul></ul>');
    $(xml).find('menu').each(function(){ 

		var name = $(this).attr('name');

		var a = $('<a href="#">'+name+'</a>');
		a.click(Anuk.scope.collection);
        a.data('images', $(this).find('img'));        
		var li = $('<li class="rov" id='+name+'></li>').append(a);
		
		
        if($(this).find('submenu').length > 0 ) {
    		var subul = $('<ul></ul>');
    		var first = true;
            $(this).find('submenu').each(function(){ 
    		    var name = $(this).attr('name');
        		var a2 = $('<a href="#">'+name+'</a>');
        		a2.click(Anuk.scope.collection);
                a2.data('images', $(this).find('img'));
                a2.data('path', $(this).attr('path'));
                
                var p = $(this).attr('path');
                $(this).find('img').each(function(){
                    var im = new Image();
                    im.src = p+'/'+$(this).attr('src');
                });    

                if (first) {
                    var li2 = $('<li class="first"></li>').append(a2);
                    first=false
                } else {
        		    var li2 = $('<li></li>').append(a2);                    
                }
    		    li2.appendTo(subul);

            });
		    subul.appendTo(li);
        }
		li.appendTo(ul);        
    });  
	ul.appendTo('.sub');

    $('.rov').click(
        function(){
            var id = $(this).attr('id');
            $('.rov').find('ul:visible').each(function(index) {
                if (id != $(this).parent().attr('id')) $(this).slideUp('slow');
            });
            $(this).find('ul').slideDown('slow');
        }
    );
}

a.prototype.collection = function(e)
{
    $('#controls').hide();

    if ( $(this).parent().find('ul').size() > 0) return;
    var collec = $(this).data('images');
    var path = $(this).data('path');
    
    if (path == undefined) {
        var n = $(this).text();
        $('.content_receipt:visible').fadeOut(1000, function() {
            $("#p_coll").html("").append($('<div align="center" style=""><img src="'+PATH+'public/images/'+n+'-comingsoon.jpg" /><h2>coming soon</h2></div>'));
            $('#p_coll').fadeIn(1000);
        });
        return;
    }
//    this = Anuk.scope;

    var items = $('<div class="items"> </div>');
    collec.each(function(){
        items.append('<div><img src="'+path+'/'+$(this).attr('src')+'" /></div>');
    });    
    $('.content_receipt:visible').fadeOut(2000, function() {
        $("#p_coll").html("").append($('<div class="scrollable"></div>').append(items));       
        $('#p_coll').fadeIn(1000, function(){$('#controls').show();});
        var api = $("div.scrollable").scrollable({
            api:true,
            size: 1,
            loop:true,
            speed:1500
        });
        $('#next').click(function(){
            api.next();
        });
        $('#prev').click(function(){
            api.prev();
        });
    });

}
a.prototype.page = function()
{
    
    $('#controls').hide();
    var p = $(this).attr('id');
    $('.content_receipt:visible').fadeOut(1000, function() {
        $('#p_'+p).fadeIn(1000);
    });    
}

$(document).ready(
	function() {
      var an = new Anuk();
      an.init();
	}
);
