var now = 1

var max = 0;

var recount;

$(document).ready(function(){
	topnav()
	nav_control()
	getLength()
	recount = setInterval("selfRo()",6000)
	hoverChange()
	top_nav_control()
	
	$('.fancybox').fancybox({'type':'iframe','width':665,'height':430,'padding':0,'margin':0,'showCloseButton':false,'autoScale':false})

	

    $('#networks marquee').marquee('pointer').mouseover(function(){
            $(this).trigger('stop')}).mouseout(function(){$(this).trigger('start')})
    
	$('.param').click(function(){
		
		var borrow = $('#borrow').val()
		var value = $('#value').val()
		var enquiry = $("#id_enquiry").val()
		var link;
		if(!borrow && !value && !enquiry){
			link = '/mortgage.html'
		}else if(!enquiry){
			link = '/mortgage.html?borrow='+borrow+'&value='+value
		}else{
			
			link = '/mortgage.html?borrow='+borrow+'&value='+value+'&enquiry='+enquiry
		}
		
		$.fancybox({'type':'iframe','width':665,'height':430,'padding':0,'margin':0,'showCloseButton':false,'autoScale':false,'href':link,'onClosed':function(){$('#borrow').val('');$('#value').val('');$("#id_enquiry").val('')}})
	})

})



function top_nav_control(){
	$('#header ul li').hover(function(){
		$(this).find('ul').css('visibility','visible')
	},function(){
		$(this).find('ul').css('visibility','hidden')
	})
	
	$('#header ul li ul li.last-li').hover(function(){
		$(this).parent('a').parent("ul").css('background','url(/media/assets/images/ul-bottom-ro.png) no-repeat left bottom')
	},function(){
		$(this).parent('a').parent("ul").css('background','url(/media/assets/images/ul-bottom.png) no-repeat left bottom')
	})
	
}



function clearText(text){
	if($(this).val() == text){
		$(this).val('')
	}
	
}

function subscribe(){
	var r = new Date().getTime()
    var s = $('#subscribe').val()
    $("#subscribemessage").removeClass("error");
    $("#subscribemessage").html("Subscribing...");
    $.get("/subscribe.json", {email: s, r:r},
        function(data){
            if (data['error'] == 0){
                $("#subscribemessage").text(data['message']);
                
                window.location = '/subscribe.html?email=' + s
                //setTimeout("window.location.reload(true);",3000);
            }else{
                $("#subscribemessage").text(data['message']);
                $("#subscribemessage").addClass("error");
            }
        }, "json");
    return false;
}

function hoverChange(){
	$('#navi .flash-btn').hover(function(){
		clearInterval(recount)
		now = $(this).attr('id').split('nav')[1]
		changeBanner(now)
	},function(){
		
		recount = setInterval("selfRo()",6000)
	})
	
}

function getLength(){
	
	$('#slider #slider-wrapper img').each(function(){
		
		max++;
	})
}

function selfRo(){
	now++;
	if(now > max){
		now = 1
		
	}
	changeBanner(now)

}


function changeBanner(to){
	
	$('#navi .flash-btn').each(function(){
		$(this).removeClass('active')
		$(this).find('.flash-txt').removeClass('active')
		$(this).find('.arrow').removeClass('active')
		
	})
	
	$('#slider #slider-wrapper img').stop(true,true).each(function(){
		
		$(this).fadeOut(600)
	})
	
	$('#navi #nav'+to).addClass('active')
	$('#navi #nav'+to).find('.flash-txt').addClass('active')
	$('#navi #nav'+to).find('.arrow').addClass('active')
	
	$('#slider #slider-wrapper #image'+to).stop(true,true).fadeIn(600)
	
}

function nav_control(){
	var opennav = $('ul.nav1 li .nav2 li.active')
	
	if (opennav) {
		opennav.parent('.nav2').parent('li').addClass('open')
		opennav.parent('.nav2').show()
	}
	
	$('ul.nav1 li a').click(function(){
		if ($(this).attr('href') == 'javascript:void(0)') {
			$('ul.nav1 li').each(function(){
				$(this).removeClass('open')
				$(this).children('.nav2').hide()
			})
			
			if ($(this).parent('li').hasClass('open')) {
			
				$(this).parent('li').removeClass('open')
				$(this).parent('li').children('.nav2').hide()
			}
			else {
				$(this).parent('li').addClass('open')
				$(this).parent('li').children('.nav2').show()
				
			}
			
		}
		
		
	})
}

function topnav(){
	
	$('#header ul li a').hover(function(){
		if (!$(this).hasClass('active')) {
			$(this).find('span').css('background', 'url(/media/assets/images/global-mid-ro.jpg) no-repeat right top')
		}
		}, function(){
			if (!$(this).hasClass('active')) {
				$(this).find('span').css('background', 'url(/media/assets/images/global-mid.jpg) no-repeat right top')
			}
		})
	
}

