$(document).ready(function() {  
		initTree()
		
		if($(".secure").length >= 1) {
                $.get("/token.php",function(txt){
    	            $(".secure").append('<input type="hidden" name="ts" value="'+txt+'" />');
	            });
 		}
		

	    $("#maintabs").tabs({fx:{                                                                                                                                                                     
                opacity: "toggle",                                                                                                                                                            
                duration:"fast"         
        }}).tabs("rotate", 6000, true); 

		
		$(".fancy").fancybox();

		$("#select_gallery").change(function(){

			var url="/projects.php?gallery="+$(this).val();

			$.ajax({
			   type: "GET",
			   url: url,
			   dataType: "json",
			   success: function(r){

					$("#galleryTitle").html(r.title);
					$("#galleryThumbBox").html(r.html);
					startTrans('DaImage',r.image)
					
			   }
			 });

		});

		
		/*
$( "#search" ).autocomplete({
			source: "/?key",
			minLength: 3,
			select: function( event, ui ) {
				if(ui.item){
					window.location="/"+ui.item.fullurl;
				}
			}
		});
*/



		$("#newslettersubscription").validate({
             errorPlacement: function(error, element) {
				
	             if (element.attr("name") == "newslettername" ) {
	                $("#name").css("border","1px solid red");
	             }
	
	             if(element.attr("name") == "newsletteremail" ) {
	                $("#email").css("border","1px solid red");
	             }
           }
	    });


    $('#newslettersubscription').submit(function(){
    	if($("#newslettersubscription").valid()) {
	        $.ajax({
	            url: $(this).attr('action'),
	                data: $(this).serialize(),
	                type: $(this).attr('method'),
	                dataType: 'json',
	                success: function(response){
	                    if (response['status']==false) {
	                        m($('#newsletteremail').val()+' is already subscribed to our newsletter!');
	                    } else {
	                        m('You have been subscribe to our newsletter successfully.');
	                    }
	             }
	
	        });
        }
        return false;
    });
		$("#contactus .submit").click(function(){
			$("#contactus").submit();	
		});

		$("#contactus").validate({
             errorPlacement: function(error, element) {
				
	             if (element.attr("name") == "name" ) {
	                element.prev("label").find("span").show()
	             }
	
	             if(element.attr("name") == "email" ) {
					element.prev("label").find("span").show()
	             }
	
	             if(element.attr("name") == "message" ) {
					element.prev("label").find("span").show()
	             }
           }
	    });
		
});



function m(m) {

                $.blockUI({
                message: m ,
                         css: {
                                padding:        0,
                                margin:         0,
                                width:          '30%',
                                top:            '200px',
                                left:           '35%',
                                textAlign:      'center',
                                color:          '#1CA049',
                                backgroundColor:'',
                                border:         '0px solid #aaa',
                                fontSize:       '25px',
                                cursor:         'wait'
                            },
                                });
     setTimeout($.unblockUI, 2000);
}



function initTree() {

        simpleTreeCollection = $('.simpleTree').simpleTree({
                afterClick:function(node){
                },
                afterDblClick:function(node){
                },
                afterMove:function(destination, source, pos){
                },
                afterAjax:function()
                {

                },
                animate:true,
                docToFolderConvert:false
        });
        
    $(".collapse").click(function() {
       simpleTreeCollection.get(0).closeAll();
    });



   	$(".expand").click(function() {
       simpleTreeCollection.get(0).expandAll();
    });
    
    if(opencatid >= 0) {
		$("#cat_"+opencatid).parents().filter("li").each(function(){
			if(!$(this).hasClass("root")) {
				var t=$(this).find(".trigger:first");
				t.click();
			}
		});
		$("#cat_"+opencatid+" .trigger:first").click();
	}
}

function cartManage(product_id,action) {

		$.ajax({
			   type: "GET",
			   url: "/cart.php?shop=modify&action="+action+"&product_id="+product_id,
			   dataType: "html",
			   success: function(r){
					$("#cart").html(r);
			   }
		});

}
