$(document).ready(function(){

    /*---------- Dynamic Lead ----------*/
    $('#dl').cycle({
    	fx:     'scrollVert',
    	pager:  '#nav',
    	speed:   500,
    	timeout: 6000
    });

    /*---------- Dynamic Lead in internal pages ----------*/
    $('#dlBg').cycle({
    	fx:     'fade',
    	//pager:  '#nav',
    	speed:   500,
    	timeout: 6000
    });

    /*---------- Slide Scroll ----------*/
    $(".scroll").click(function(event){
    	event.preventDefault();
    	var full_url = this.href;
    	var parts = full_url.split("#");
    	var trgt = parts[1];
    	var target_offset = $("#"+trgt).offset();
    	var target_top = target_offset.top - 21;
    	$('html, body').animate({scrollTop:target_top}, 500);
    	return false;
    });

    $(".bkTop").click(function(event){
    	$('body,html').animate({scrollTop:0},500);
    	return false;
    });


    /*---------- Films Carousel ----------*/
     if($('.carousel').length > 0){
        $('.carousel').carouFredSel({
            auto        : false,
            items       : 4,
            scroll      : 4,
            infinite    : true,
            circular    : true,
            align       : "center",
            prev        : ".filmsCarousel a#prev",
            next        : ".filmsCarousel a#next"
            //  direction   : "up" //Possible values: "right", "left", "up" or "down".
    	});
        //http://caroufredsel.frebsite.nl/configuration.php
    };


    /*---------- Work Gallery ----------*/
    $('#workNav').html('<em><!-- --></em><a class="gal2"><!-- --></a>');

    $('#workNav a.gal2').click(function(){
    	changeValuesGal2();
    });
    $('#workNav a.gal1').click(function(){
    	changeValuesGal1();
    });

    function changeValuesGal1(){
    	$('#workNav').html('<em><!-- --></em><a class="gal2"><!-- --></a>');
    	$('#work-galery-2').removeClass('active');
    	$('#work-galery-1').addClass('active');
    	$('#workNav a.gal2').click(changeValuesGal2);
    }
    function changeValuesGal2(){
    	$('#workNav').html('<a class="gal1"><!-- --></a><em><!-- --></em>');
    	$('#work-galery-1').removeClass('active');
    	$('#work-galery-2').addClass('active');
    	$('#workNav a.gal1').click(changeValuesGal1);
    }


  /*---------- Label + Checkboxes ----------*/
    $('input[type="checkbox"] + label').click(function(){
        if( $(this).parent().find('input[type="checkbox"]').attr('checked') == 'checked' ){
            $(this).parent().find('input[type="checkbox"]').removeAttr('checked');
        }else{
            $(this).parent().find('input[type="checkbox"]').attr('checked', 'checked');
        };
    });

  /*---------- Link Block ----------*/
  if( $('.linkBlock').length > 2 ){
    $('.linkBlock:eq(3)').addClass('last')
  };


  /*---------- Menu Current Button ----------*/
  var URLwhere = window.location;

  if( (URLwhere == 'http://' + location.host + '/the-company/global-presence') || (URLwhere == 'http://' + location.host + '/the-company/innovation') || (URLwhere == 'http://' + location.host + '/the-company/helping-real-music-stay-alive') || (URLwhere == 'http://' + location.host + '/the-company/strategic-allies') ){
    $('.menu .btn1').addClass('current');
  }

  if( (URLwhere == 'http://' + location.host + '/services/music-licensing-online') || (URLwhere == 'http://' + location.host + '/services/corporate-programs') || (URLwhere == 'http://' + location.host + '/services/catalog-management-with') || (URLwhere == 'http://' + location.host + '/services/white-label-store') ){
    $('.menu .btn2').addClass('current');
  }

  if( (URLwhere == 'http://' + location.host + '/our-clients/' + 'att') || (URLwhere == 'http://' + location.host + '/our-clients/' + 'nickelodeon') || (URLwhere == 'http://' + location.host + '/our-clients/' + 'discovery') || (URLwhere == 'http://' + location.host + '/our-clients/' + 'malibu') ){
    $('.menu .btn3').addClass('current');
  }

  if( (URLwhere == 'http://' + location.host + '/team/management-team') || (URLwhere == 'http://' + location.host + '/team/advisory-board') || (URLwhere == 'http://' + location.host + '/team/music-supervisor-advisory-board') ){
    $('.menu .btn4').addClass('current');
  }

  /*---------- Register Form ----------*/
  $('a.reg, .signup_btn').click(function(){
        $('.registerBlock').fadeIn(300);
        $('.whiteLb').fadeIn(300);

        if($('#register_form').length > 0){
            $('#register_form').validate({
                errorElement: "div",
                errorClass: 'error',
                rules: {
                    iName:{
                        required:true,
                        minlength:2,
                    },
                    iEmail:{
                        required:true,
                        email:true,
                    }
                },

                messages: {
                    iName:{
                        required: "<span class='icon alert'><!-- --></span>Please enter your Name",
                        minlength: "<span class='icon alert'><!-- --></span>Your First name must be at least 2 characters long"
                    },
                    iEmail:{
                        required: "<span class='icon alert'><!-- --></span>Please enter an Email address",
                        email: "<span class='icon alert'><!-- --></span>Please enter a valid Email address"
                    }
                },

                submitHandler: function(form){
                    $.ajax({
                        type:   "POST",
                        url:    "http://" + location.host + "/addons/themes/sirgroovy/bin/register.php",
                        data: $(form).serialize(),

                        success: function(){
                            $('#register_form').animate({opacity:0}, 300 , function(){
                                $('#register_form').html('<h2 class="title" style="margin:23px 0 14px">Thank you!</h2>'+
                                                         '<a class="closeThis">x</a>'+
                                                         '<p>We will get back you shortly.</p>')
                                                         .css({textAlign:'center'})
                                                         .animate({opacity:1});

                                                         $('a.closeThis').click(function(){
                                                            $('.whiteLb').fadeOut(300);
                                                         $(this).parent().parent().fadeOut(300);
                            });
                            });


                        }
                    });
                    return false;
                  }
            });
        }
  });


  /*---------- Join Label ----------*/
  $('.join_btn').click(function(){
        $('.joinLabel').fadeIn(300);
        $('.whiteLb').fadeIn(300);

        if($('#label_form').length > 0){
            $('#label_form').validate({
                errorElement: "div",
                errorClass: 'error',
                rules: {
                    iName:{
                        required:true,
                        minlength:2,
                    },
                    iEmail:{
                        required:true,
                        email:true,
                    }
                },

                messages: {
                    iName:{
                        required: "<span class='icon alert'><!-- --></span>Please enter your Name",
                        minlength: "<span class='icon alert'><!-- --></span>Your First name must be at least 2 characters long"
                    },
                    iEmail:{
                        required: "<span class='icon alert'><!-- --></span>Please enter an Email address",
                        email: "<span class='icon alert'><!-- --></span>Please enter a valid Email address"
                    }
                },

                submitHandler: function(form){
                    $.ajax({
                        type:   "POST",
                        url:    "http://" + location.host + "/addons/themes/sirgroovy/bin/join_mail.php",
                        data: $(form).serialize(),

                        success: function(){
                            $('#label_form').animate({opacity:0}, 300 , function(){
                                $('#label_form').html('<h2 class="title" style="margin:23px 0 14px">Thank you!</h2>'+
                                                         '<a class="closeThis">x</a>'+
                                                         '<p>We will get back you shortly.</p>')
                                                         .css({textAlign:'center'})
                                                         .animate({opacity:1});

                                                         $('a.closeThis').click(function(){
                                                            $('.whiteLb').fadeOut(300);
                                                         $(this).parent().parent().fadeOut(300);
                            });
                            });


                        }
                    });
                    return false;
                  }
            });
        }
  });


    $('a.closeThis').click(function(){
        $('.whiteLb').fadeOut(300);
        $(this).parent().parent().fadeOut(300);
    });


});// end


$(window).load(function(){
    /*---------- Work Gallery ----------*/
    $('#mainImg').cycle({
    		fx:     'scrollHorz',
    		pager:  '#nav',
    		speed:   800,
    		timeout: 4500
    	});

    $().UItoTop({});
});
