(function($) {
    $(document).ready(function() {
        $('.mainmenuitem').click(function(e){
            e.preventDefault();
            l=$(this).attr('href');
            $.ajax({
                url: baseURL + 'src/set_menu_id.php',
                type: 'POST',
                data: 'mid='+$(this).attr('id').split('_')[1],
                dataType: 'json',

                error:function(x,e){
                    alert(x.responseText);
                },
                success: function(data){
                    location.href=l;
                }
            });
        });
        $('.mainmenuitemh').click(function(e){
            e.preventDefault();
            l=$(this).attr('href');
            $.ajax({
                url: baseURL + 'src/set_menu_id.php',
                type: 'POST',
                data: 'mid='+$(this).attr('id').split('_')[1],
                dataType: 'json',

                error:function(x,e){
                    alert(x.responseText);
                },
                success: function(data){
                    location.href=l;
                }
            });
        });

        $('.submenuitem').click(function(e){
            e.preventDefault();
            l=$(this).attr('href');
            $.ajax({
                url: baseURL + 'src/set_smenu_id.php',
                type: 'POST',
                data: 'mid='+$(this).attr('id').split('_')[1],
                dataType: 'json',

                error:function(x,e){
                    alert(x.responseText);
                },
                success: function(data){
                    location.href=l;
                }
            });
        });
        $('.submenuitemh').click(function(e){
            e.preventDefault();
            l=$(this).attr('href');
            $.ajax({
                url: baseURL + 'src/set_smenu_id.php',
                type: 'POST',
                data: 'mid='+$(this).attr('id').split('_')[1],
                dataType: 'json',

                error:function(x,e){
                    alert(x.responseText);
                },
                success: function(data){
                    location.href=l;
                }
            });
        });

        $('.footermenuitem').click(function(e){
            e.preventDefault();
            l=$(this).attr('href');
            $.ajax({
                url: baseURL + 'src/set_menu_id.php',
                type: 'POST',
                data: 'mid='+$(this).attr('id').split('_')[1],
                dataType: 'json',

                error:function(x,e){
                    alert(x.responseText);
                },
                success: function(data){
                    location.href=l;
                }
            });
        });
        $('.footermenuitemh').click(function(e){
            e.preventDefault();
            l=$(this).attr('href');
            $.ajax({
                url: baseURL + 'src/set_menu_id.php',
                type: 'POST',
                data: 'mid='+$(this).attr('id').split('_')[1],
                dataType: 'json',

                error:function(x,e){
                    alert(x.responseText);
                },
                success: function(data){
                    location.href=l;
                }
            });
        });

        //animated collapse

        $('.slidelisttrigger').click(function(e){
            e.preventDefault();
            $(this).parent().next().slideToggle('slow', function() {
                // Animation complete.
                });
        });

        $('.collapsediv').slideUp();

        //animated collapse end

        $('#contact_submit').click(function(e){
            e.preventDefault();
            dataString=$('#contactform').serialize();
            v=$('#res').val();
            ok=false;
            if(($('#name').val()!='' && $('#name').val()!=undefined) && ($('#email').val()!='' && $('#email').val()!=undefined) && ($('#res').val()!='' && $('#res').val()!=undefined)){
                ok=true;
            }else{
                alert('Please fill all of the fields!');
            }

            if(ok){
                $.ajax({
                    url: baseURL + 'src/security.php',
                    type: 'POST',
                    data: 'res='+v,
                    dataType: 'json',

                    error:function(x,e){
                        alert(x.responseText);
                    },
                    success: function(data){
                        if(data.success){
                            $.ajax({
                                url: baseURL + 'src/contact.php',
                                type: 'POST',
                                data: dataString,
                                dataType: 'json',

                                error:function(x,e){
                                    alert(x.responseText);
                                },
                                success: function(data){
                                    alert('V-ati inscris cu success!');
                                    location.reload();
                                }
                            });
                        }else{
                            alert('Security answer incorrect!');
                        }
                    }
                });
            }
        });

    
        $('#newslettersignup').click(function(e){
            e.preventDefault();
            dataString=$('#newslettersub').serialize();
            ok=false;
		
            v=$('#res2').val();
		
            if(($('#nume2').val()!='' && $('#nume2').val()!=undefined) && ($('#email2').val()!='' && $('#email2').val()!=undefined) && ($('#res2').val()!='' && $('#res2').val()!=undefined)){
                ok=true;
            }else{
                alert('Please fill all of the fields!');
            }

            if(ok){
                $.ajax({
                    url: baseURL + 'src/security.php',
                    type: 'POST',
                    data: 'res='+v,
                    dataType: 'json',

                    error:function(x,e){
                        alert(x.responseText);
                    },
                    success: function(data){
                        if(data.success){
                            $.ajax({
                                url: baseURL + 'src/bnewsletter.php',
                                type: 'POST',
                                data: dataString,
                                dataType: 'json',

                                error:function(x,e){
                                    alert(x.responseText);
                                },
                                success: function(data){
                                    alert('V-ati inscris cu success!');
                                    location.reload();
                                }
                            });
                        }else{
                            alert('Security answer incorrect!');
                        }
                    }
                });
            }
        });

        $('.gallerythumb').click(function(){
            $('.mainimage').children('img').attr('src',baseURL+'thumb.php?mode=7&file='+$(this).attr('src').split('file=')[1]);
        });

        $('.mainimage').children('img').click(function(){
            o = $(this).attr('src');
            s = o.split('file=');
            jQuery.slimbox(baseURL+'thumb.php?mode=5&file='+s[1]);
        });
	
        $('.sampleimg').click(function(){
            o = $(this).attr('src');
            s = o.split('file=');
            jQuery.slimbox(baseURL+'thumb.php?mode=5&file='+s[1],$(this).attr('title'));
		
        });

    
        $('.seyear').change(function(){
            $.ajax({
                url: baseURL + 'src/admin_set_year.php',
                type: 'POST',
                data: 'year=' + $('.seyear').val(),
                dataType: 'json',

                error:function(x,e){
                    alert(x.responseText);
                },
                success: function(data){
                    location.reload();
                }
            });
        });

        $('.day').click(function(e){
            e.preventDefault();
            t=$(this);
            $.ajax({
                url: baseURL + 'src/admin_get_day.php',
                type: 'POST',
                data: 'day='+parseInt(t.html())+'&month='+t.parent().children('.hmonth').val()+'&year='+$('.seyear').val(),
                dataType: 'json',

                error:function(x,e){
                    alert(x.responseText);
                },
                success: function(data){
                    $('.terms').html(data.info);
                }
            });

        });

        $('.sevent').click(function(e){
            e.preventDefault();
            $.ajax({
                url: baseURL + 'src/admin_get_searched.php',
                type: 'POST',
                data: 'phrase='+$('#ssttring').val(),
                dataType: 'json',

                error:function(x,e){
                    alert(x.responseText);
                },
                success: function(data){
                    $('.terms').html(data.info);
                }
            });
        });

        $('.sellang').click(function(e){
            e.preventDefault();
            l=$(this).attr('href');
            $.ajax({
                url: baseURL + 'src/set_lang.php',
                type: 'POST',
                data: 'lang='+l+'&page_id='+getUrlVars()['page_id'],
                dataType: 'json',

                error:function(x,e){
                    alert(x.responseText);
                },
                success: function(data){
                    location.href = baseURL + l + '/' + getUrlVars()['page_id']+'/'+data.url;
                }
            });
        });

        if($('.album_holder').length){
            Galleria.loadTheme(baseURL + 'libs/galleria/themes/classic/galleria.classic.min.js');
        }
        var data = new Array;
        var first = 1
        var aindex = ''
        $('.album_holder a').each(function(){
            if(first){
                first = 0;
                aindex = $(this).attr('rel');
            }
            data[$(this).attr('rel')] = [{}];
        });
            
        $('.album_holder a').each(function(){
            data[$(this).attr('rel')].push({
                image: $(this).attr('href'),
                title: $(this).attr('title')
            });
        });


        for(i=0;i<$('.album_holder').length;i++){
            j = i + 1;
            
            data[$('.ah'+j+' a').attr('rel')].shift()
            
            $('.ah'+j+' a').click(function(e){ 
                e.preventDefault();

                id = $(this).attr('id') - 1
                $('.alb').hide();
                $('#alb'+id).show();
                
                $('#alb'+id).galleria({
                    width: 703,
                    height: 300,
                    imageMargin: 4,
                    data_source: data[$(this).attr('rel')]
                });
            });

            $('.alb').hide();
            $('#alb0').show();

            $('#alb0').galleria({
                width: 703,
                height: 300,
                imageMargin: 4,
                data_source: data[aindex]
            });
            
        }
        
    });

    
})(jQuery);

function dump(arr,level) {
    var dumped_text = "";
    if(!level) level = 0;

    //The padding given at the beginning of the line.
    var level_padding = "";
    for(var j=0;j<level+1;j++) level_padding += "    ";

    if(typeof(arr) == 'object') { //Array/Hashes/Objects
        for(var item in arr) {
            var value = arr[item];

            if(typeof(value) == 'object') { //If it is an array,
                dumped_text += level_padding + "'" + item + "' ...\n";
                dumped_text += dump(value,level+1);
            } else {
                dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
            }
        }
    } else { //Stings/Chars/Numbers etc.
        dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
    }
    return dumped_text;
}
