$(document).ready(function(){
    
    // Category Boxes
    
    if($('html').hasClass('ie6') || $('html').hasClass('ie7')  || $('html').hasClass('ie8')){
        $('.large .cbox:not(.active) .cboxoverlay').show().css('height',0);
        $('.small .cbox:not(.active) .cboxoverlay').show().css('height','100%');   
    }else{
        $('.large .cbox:not(.active) .cboxoverlay').show().css('opacity',0);
        $('.small .cbox:not(.active) .cboxoverlay').show().css('opacity',1);      
    }
    
    $('.large .cbox').hover(
        function(){
            if(!$(this).hasClass('active')){
                if($('html').hasClass('ie6') || $('html').hasClass('ie7')  || $('html').hasClass('ie8')){
                    $('.cboxoverlay',this).stop().animate({height:'100%'});
                }else{
                    $('.cboxoverlay',this).stop().animate({'opacity':1});
                }
                var currheight = $('.cboxlinks',this).css('height').replace('px','');
                var height = $('.cboxlinks',this).css('height','auto').height();
                if($.trim($('.cboxlinks',this).html())!=''){
                    $('.cboxlinks',this).stop().css({'height':currheight}).animate({'height':height});
                    $('.cboxcontent',this).stop().animate({'top':50,'opacity':1});
                }else{
                    $('.cboxcontent',this).stop().animate({'opacity':1});
                }
                
                
            }
        },function(){
            if(!$(this).hasClass('active')){
                if($('html').hasClass('ie6') || $('html').hasClass('ie7')  || $('html').hasClass('ie8')){
                    $('.cboxoverlay',this).stop().animate({height:'0'});
                }else{
                    $('.cboxoverlay',this).stop().animate({'opacity':0});
                }
                $('.cboxlinks',this).stop().animate({'height':0});
                $('.cboxcontent', this).stop().animate({'top':'130','opacity':0.5});
            }
        }
    );
    
    $('.small .cbox').hover(
        function(){
            if(!$(this).hasClass('active')){
                if($('html').hasClass('ie6') || $('html').hasClass('ie7')  || $('html').hasClass('ie8')){
                    $('.cboxoverlay',this).stop().animate({height:'0'});
                }else{
                    $('.cboxoverlay',this).stop().animate({'opacity':0});
                }
                $('.cboxcontent', this).stop().animate({'opacity':1});
            }
        },function(){
            if(!$(this).hasClass('active')){
                if($('html').hasClass('ie6') || $('html').hasClass('ie7')  || $('html').hasClass('ie8')){
                    $('.cboxoverlay',this).stop().animate({height:'100%'});
                }else{
                    $('.cboxoverlay',this).stop().animate({'opacity':1});
                }
                $('.cboxcontent', this).stop().animate({'opacity':0.5});
            }
        }
    );
    
    
    
    
    // Site credits
    
    $('#wcreditsbt').click(
        function(){
            $(this).toggleClass('open');
            if($(this).hasClass('open')){
                $('#wcredits').slideDown();
            }else{
                $('#wcredits').slideUp();
            }
        }
    );
    
    
    
    
    
// Search
    
    //// Focus the keywords field after the category has been selected
    //$('#srchbarmini .srchfield_cat').change(function(){
    //    
    //});
    
    
    // Synchronise Category Drop downs
    
    $('.srchfield_cat').change(function(){
        $(this).siblings('.srchfield_keyw').focus();
        var val = $(this).val();
        var id = $(this).attr('id').replace('mini_','').replace('big_','');
        $('#srchbarmini .srchfield_cat option, #srchbarbig .srchfield_cat option').each(function(){
            $(this).removeAttr('selected');
        });
        $('.srchfield_cat option[value="'+val+'"]').attr('selected','selected');
        
        if(val){
            $('.subcat_loading').show();
            $('label[for="searchfield_subcat"], .srchfield_subcat').hide();
            qs = "cat_url="+val;
            $.ajaxSetup({
                timeout:100000,
                dataType: 'html',
                error:function(xhr){
//                    alert(xhr.status+" : "+xhr.statusText);
                }
            });
            $.post(ajax_url+'/subcats.php',qs,function(data){
                if(data){
                    
                    $('.srchfield_subcat').html(data);
                    $('label[for="searchfield_subcat"], .srchfield_subcat').fadeIn();
                }
                $('#subcat_loading').hide();
            });

            
        }else{
            $('#subcat_loading').hide();
            $('label[for="searchfield_subcat"], .srchfield_subcat').hide();
        }
        
    });
    
    // Synchronise keyword fields
    $('#srchbarmini .srchfield_keyw, #srchbarbig .srchfield_keyw').keyup(function(){
        var val = $(this).val();
        $('#srchbarmini .srchfield_keyw, #srchbarbig .srchfield_keyw').val(val);
    });
    
    $('#srchadvancedbt, #srchadvancedclosebt').click(function(){
        $('#srchadvancedbt').toggleClass('open');
        if($('#srchadvancedbt').hasClass('open')){
            $('#srchbarbig').slideDown();
        }else{
            $('#srchbarbig').slideUp();
        }
    });
    
    $('#srchadvancedsubmit').click(function(){
        $('#search form').submit();
    })
    
    
    // Scroll to the top
    
    $('#tothetop').click(function(){
	$('html, body').animate({scrollTop:0},'slow');
    })
        
});

// Map
function initialise_map(){

    $('area, .region' ,'#newzealand_box').hover(
        function(){
            var region = $(this).attr('rel');
            var indent = map_arr[region];
            if($('input[rel="'+region+'"]').is(':checked')){
                $('div[rel="'+region+'"]','#newzealand_names').css({'text-decoration':'none'});
                $('#mapoverlay_'+region).hide().css({'background-position':'207px 0'});
                $('input[rel="'+region+'"]').blur();
            }else{
                $('div[rel="'+region+'"]','#newzealand_names').css({'text-decoration':'underline'})
                $('#mapoverlay_'+region).show().css({'background-position':indent+'px 0'});
                $('input[rel="'+region+'"]').focus();
            }
        },
        function(){
            var region = $(this).attr('rel');
            var indent = map_arr[region];
            if($('input[rel="'+region+'"]').is(':checked')){
                $('div[rel="'+region+'"]','#newzealand_names').css({'text-decoration':'underline'})
                $('#mapoverlay_'+region).show().css({'background-position':indent+'px 0'});
                $('input[rel="'+region+'"]').focus();
            }else{
                $('div[rel="'+region+'"]','#newzealand_names').css({'text-decoration':'none'});
                $('#mapoverlay_'+region).hide().css({'background-position':'207px 0'});
                $('input[rel="'+region+'"]').blur();
            }
        }
    );
    $('#newzealand_imagemap area, .region').click(
        function(e){
            var region = $(this).attr('rel');
            var indent = map_arr[region];
            if($('input[rel="'+region+'"]').is(':checked')){
                $('div[rel="'+region+'"]','#newzealand_names').css({'text-decoration':'none'});
                $('input[rel="'+region+'"]').removeAttr('checked');
                $('#mapoverlay_'+region).hide().css({'background-position':'207px 0'});
            }else{
                $('div[rel="'+region+'"]','#newzealand_names').css({'text-decoration':'underline'})
                $('input[rel="'+region+'"]').attr('checked','checked');
                $('#mapoverlay_'+region).show().css({'background-position':indent+'px 0'});
            }
        }
    );
    
    $('.location input').change(function(){
        var region = $(this).val();
        var indent = map_arr[region];
        if($(this).is(':checked')){
            $('div[rel="'+region+'"]','#newzealand_names').css({'text-decoration':'underline'})
            $('input[rel="'+region+'"]').attr('checked','checked');
            $('#mapoverlay_'+region).show().css({'background-position':indent+'px 0'});
        }else{
            $('div[rel="'+region+'"]','#newzealand_names').css({'text-decoration':'none'});
            $('input[rel="'+region+'"]').removeAttr('checked');
            $('#mapoverlay_'+region).hide().css({'background-position':'207px 0'});
        }
    });
    
    $('#locations input').each(function(){
        var region = $(this).val();
        var indent = map_arr[region];
        if($(this).is(':checked')){
            $('div[rel="'+region+'"]','#newzealand_names').css({'text-decoration':'underline'})
            $('input[rel="'+region+'"]').attr('checked','checked');
            $('#mapoverlay_'+region).show().css({'background-position':indent+'px 0'});
        }else{
            $('div[rel="'+region+'"]','#newzealand_names').css({'text-decoration':'none'});
            $('input[rel="'+region+'"]').removeAttr('checked');
            $('#mapoverlay_'+region).hide().css({'background-position':'207px 0'});
        }
    });
    
    $('#searchfield_cat').change();
    
    
}

// Map for Discover Page
function initialise_map_discover(){

    $('area, .region' ,'#newzealand_map_discover').hover(
        function(){
            var region = $(this).attr('rel');
            var indent = map_arr[region];
            $('div[rel="'+region+'"]','#newzealand_names_discover').css({'text-decoration':'underline'})
            $('#mapoverlay_discover_'+region).show().css({'background-position':indent+'px 0'});
        },
        function(){
            var region = $(this).attr('rel');
            var indent = map_arr[region];
            $('div[rel="'+region+'"]','#newzealand_names_discover').css({'text-decoration':'none'});
            $('#mapoverlay_discover_'+region).hide().css({'background-position':'207px 0'});
        }
    );
    
    
}


/* MENU */
$('#nav-main').addClass('js-active');
	    $('ul ul', '#nav-main').css('visibility', 'hidden');
	    $('li', '#nav-main').hover(
		function(){
		    var ul     = $('ul:first', this);
		    if (ul.length){
			if (!ul[0].hei) ul[0].hei = ul.height();
			if (!ul[0].wid) ul[0].wid = ul.width();
			ul.css({opacity:0, height: 0, overflow: 'hidden', width: ul[0].wid}).retarder(0.5, function(i){
			    i.css({'visibility':'visible','display':'block'}).stop().animate({height: ul[0].hei, opacity:1}, {duration: 400, complete : function(){ ul.css('overflow', 'visible'); }});
			});
		    }
		},
		function(){
		    var ul  = $('ul:first', this);
		    if (ul.length){
			var css = {opacity:0, height: 0};
			ul.stop().retarder(1, function(i){ i.animate(css,400,function(){
			    i.css({'display':'none'});
			}); });
			
		    }
		}
	    );

/* FAVOURITES */
    $('.btfav').live('click',function(){
	var saved = $(this).hasClass('saved');
	var operator = $(this).attr('rel');
	var obj = $(this);
	obj.addClass('loading');
	if(saved){
	    obj.find('span').html('Removing favourite');
	    $.post(ajax_url+'/favourites.php','id='+operator+'&a=unset',function(data){
		obj.removeClass('loading');
		if(data.status=='unset'){
		    obj.removeClass('saved');
		    obj.find('span').html('Add to favourites');
		    $('.favcount').html(data.count);
		}
	    },'json');
	}else{
	    obj.find('span').html('Adding favourite');
	    $.post(ajax_url+'/favourites.php','id='+operator+'&a=set',function(data){
		obj.removeClass('loading');
		if(data.status=='set'){
		    obj.addClass('saved');
		    obj.find('span').html('Saved favourite');
		    $('.favcount').html(data.count);
		}
	    },'json');
	}
    });
    
    
