﻿ /*
    @Australian Unity
    @Author reactive.com
    @October 2010
*/

var slideTimer;
var currentPosition;
var numberOfSlides;
var animateSpeed;
var slideWidth;
var newsTimer;
var newsIndex;
var newsLength;
var newsscrollDirection = "forward";

$.extend(ausunity, {
	hometiles:{
		init: function () {
			$(document).ready(function(){
				var sections = $('#sections');
				
				$('> li', sections).each(function(){						  
					$('.top', this).bind('mouseenter mouseleave', function(event){
						if (event.type == 'mouseenter'){
							$(this).next('a').addClass('statehover');
						}
						else
						{
							$(this).next('a').removeClass('statehover');
						}
					});
				});
			});
		}
	},
	slides: {
		init: function() {
			$(document).ready(function() {
				currentPosition = 0;
				slideWidth = 664;
				var slideContainer = $('#promos');
				
				//Tail Slide
				$('> .slide:first', slideContainer).clone().appendTo(slideContainer).addClass('cloned');
				var slides = $('> .slide', slideContainer);
				
				//Change any ids
				$('#promos .cloned select').each(function(){
					$(this).attr('id', $(this).attr('id') + "2");
				});
				
				numberOfSlides = slides.length;
				animateSpeed = 900;
			
				slideContainer.css('overflow', 'hidden');
				slides.width(slideWidth); // Remove scrollbar
				slides.wrapAll('<div id="slideInner"></div>').css({ 'float': 'left', 'width': slideWidth });
				$('#slideInner').css('width', slideWidth * numberOfSlides);
				//slideContainer.after("<ul id='controlsList'></ul>");
				slides.each(function (i) { 
					$('#controlList ul').append('<li><a href="#" id="' + i + '" class="slideTH">' + (i + 1) + '</a></li>');
				})
				$('#controlList ul li:first a').addClass('active');
				//Remove Clone
				$('#controlList ul li:last').remove();

				if (typeof iga != 'undefined') {
				    ausunity.slides.presentSlideBasedFromKeywords(iga.getTerms());
				}
				
				//AutoScroller
				//ausunity.slides.startTimer();
				
				$('.slideTH').bind('click', function () {							  
					$('#controlList ul li .active').removeClass('active');
					$(this).addClass('active');
					currentPosition = $(this).attr('id');
					$('#slideInner').animate({ 'marginLeft': slideWidth * (-currentPosition) }, { easing: easingMethod, duration: animateSpeed });
					clearInterval(slideTimer);
					return false;
				});
		
			//Pause Slider if User interacting with it
			slideContainer.bind('mouseenter mouseleave', function (event) {
				if (event.type == 'mouseenter') {
					if (typeof slideTimer != 'undefined')
						clearInterval(slideTimer);
				}
		
				if (event.type == 'mouseleave')
					ausunity.slides.startTimer();
			});
			$('.promoDD').live('mouseenter', function () {
				clearInterval(slideTimer);
			 })
			});
		},
		startTimer: function() {
			slideTimer = window.setInterval(function () {
				// Determin new position
				currentPosition = $('#controlList li a.active').attr('id');
				var lastSlide = false;
				if(parseInt(currentPosition)+2 >= numberOfSlides)
					var lastSlide = true;
				
				currentPosition = parseInt(currentPosition) + 1;

				$('#slideInner').animate({ 'marginLeft': -(slideWidth * currentPosition) }, 
					{easing: easingMethod, duration: animateSpeed, complete: 
						function(){
							if(lastSlide){
								$('#slideInner').css({'marginLeft':0});
								currentPosition = 0;
							}
						}
				});
				
				$('#controlList li a.active').removeClass('active');
				if(lastSlide)
					$('#0').addClass('active');
				else
					$('#' + currentPosition).addClass('active');
				
				$('.promoDD').hide();
			}, 8000);
        },
        presentSlideBasedFromKeywords: function (sender) {

            var i = ausunity.slides.getKeywordMatchForIndex(sender);

            $('#controlList ul li .active').removeClass('active');
            $('#controlList ul li:eq(' + i + ') a').addClass('active');
            currentPosition = i;
            $('#slideInner').css({ 'marginLeft': slideWidth * (-currentPosition) });

        },
        getKeywordMatchForIndex: function (sender) {

            var rank;
            var lineRank;
            var index = 0;

            for (i = 0; i < sender.length; i++) {
                rank = 0;
                for (x = 0; x < $keywords.length; x++) {
                    lineRank = 0;
                    for (y = 0; y < $keywords[x].length; y++) {
                        if ($keywords[x][y].toUpperCase() == $.trim(sender[i].toUpperCase())) {
                            lineRank++;
                        }
                    }
                    if (lineRank > rank) {
                        rank = lineRank;
                        index = x;
                    }
                }

            }
            return index;
        }
	},
	dynamicDropDowns: {
		init: function(type) {
		type = typeof(type) != 'undefined' ? type : "normal";
			$(document).ready(function () {				
				if(type == "normal"){
					$('select.replace').each(function () {
						ausunity.dynamicDropDowns.replaceDropDowns($(this));
					});
				}
				else
				{
					$('#promos select').each(function () {
						ausunity.dynamicDropDowns.replaceslide6DD($(this));
					});
				}
			});
		},
		replaceDropDowns: function(sender)
		{
			var selected = sender.find("option[selected]");		
			var options = $("option", sender);
			sender.after('<dl id="repaced_' + sender.attr('id') + '" class="dropdown"></dl>');
			var target = $('#repaced_' + sender.attr('id'));
			target.append('<dt><a href="#">' + selected.text() + '</a></dt>');
			target.append('<dd><ul></ul></dd>');
			sender.hide();
		
			options.each(function (i) {
				if ($(this).text() != selected.text()) {
					var lineDisplay = "block";
				} else {
					var lineDisplay = "none";
				}
				$("dd ul", target).append('<li style="display:'+lineDisplay+';"><a href="#"><span class="text">' + $(this).text() + '</span><span class="value">' + $(this).val() + '</span></a></li>');
			});
		
			$('dt a', target).bind('click', function () {
				$('ul', target).fadeIn(300).css({ 'z-index': 100 });
				return false;
			});
		
			$(target).bind('mouseleave', function () {
				var timoutTarget = $('ul', target);
				var timoutTargetSrc = $('dt a', this);
				dropDowntimer = setTimeout(function () {
					timoutTarget.fadeOut(300).css({ 'z-index': 50 });
				}, 500);
		
			});
		
			$('ul', target).bind('mouseenter', function () {
				if (typeof dropDowntimer != 'undefined')
					clearTimeout(dropDowntimer);
			});
		
		
			$("dd ul li a", target).bind('click', function () {
				var text = $('.text', this).html();
				$("dt a", target).html(text);
				$("dd ul", target).fadeOut(300);
				$("dd ul li", target).show();
				$(this).parent().hide();
				sender.val($(this).find("span.value").html());
				sender.change();
				return false;
			});
		},
		replaceslide6DD: function(sender){
			/*
			*   SLIDE DROP DOWN HANDLER
			*   Unfortuantly neccassry because of a overflow cut off bug with the dynamic drop downs in the slides.
			*/
			var selected = $('option:selected', sender);
			var options = $("option", sender);
			sender.after('<dl id="repaced_' + sender.attr('id') + '" class="dropdown"></dl>');
			var target = $('#repaced_' + sender.attr('id'));
			var truncatedText = selected.text();
			if(truncatedText.length > 35){
				truncatedText = truncatedText.substring(0,35) + '...';
			}
			
			target.append('<dt><a href="#">' + truncatedText + '</a></dt>'); 

			var ddOffset = sender.position();
			var $top = ddOffset.top;

			
			$('#promos').after('<ul id="dropListSpecial_' + sender.attr('id') + '" style="position:absolute" class="promoDD"></ul>');

			var specialList = $('#dropListSpecial_' + sender.attr('id'));
			specialList.css({ top: $top+81, left: 367 });
			
			if(sender.attr('id') == "needs_Pregnancy"){
				specialList.css({ top: ($top+81)-27, left: 367 });
			}
			
			sender.hide();
			
			options.each(function (i) {
				if ($(this).text() != selected.text()) {
					var lineClass = "";
				} else {
					var lineClass = "state-active";
				}
				specialList.append('<li class="' + lineClass + '"><a href="#"><span class="text">' +
							$(this).text() + '</span><span class="value">' +
							$(this).val() + '</span></a></li>');
			});
		
			$('dt a', target).bind('click', function () {
				$(this).focus();
				specialList.fadeIn(300).css({ 'z-index': 100 });
				return false;
			});
			
			$('dt a', target).blur(function(){
    			var timoutTarget = specialList;
				var timoutTargetSrc = $('dt a', target);
				dropDowntimer = setTimeout(function () {
					timoutTarget.fadeOut(300).css({ 'z-index': 50 });
				}, 500);
			});

			$(specialList).bind('mouseleave', function () {
				var timoutTarget = specialList;
				var timoutTargetSrc = $('dt a', target);
				dropDowntimer = setTimeout(function () {
					timoutTarget.fadeOut(300).css({ 'z-index': 50 });
				}, 500);
		
			});
		
			specialList.bind('mouseenter', function () {
				if (typeof dropDowntimer != 'undefined')
					clearTimeout(dropDowntimer);
			});
		
		
			$("a", specialList).bind('click', function () {
				var text = $('.text', this).html();
				if(text.length > 35){
					text = text.substring(0,35) + '...';
				}
				
				$("dt a", target).html(text);
				specialList.fadeOut(300);
				$("li", specialList).show();
				$(this).parent().parent().find('li.state-active').removeClass('state-active');
				$(this).parent().addClass('state-active');
				var svalue = $(this).find("span.value").html();
				
				if(sender.attr('id') == "needs_HospitalCover"){
					if(svalue == "Medium"){
					    $('#quickQuoteForm').css({ 'marginTop': -27 });
					    $('#quickQuoteHeading').css({ 'marginTop': -14 });
						$('#repaced_needs_Pregnancy').show();
					}else{
						$('#quickQuoteForm').css({'marginTop': 0});
						$('#quickQuoteHeading').css({ 'marginTop': 0 });
						$('#repaced_needs_Pregnancy').hide();
					}
				}

				sender.val(svalue);
				sender.change();
				return false;
			});
			
			if (sender.attr('id') == "needs_Pregnancy")
				target.hide();
		},
		quickQuote: {
			showPregnancyQ: function(){
			
			},
			hidePregnancyQ: function(){
			
			}
		}
	}, 
	newsslider: {
        init: function () {
            $(document).ready(function () {
                var news = $('#newsListing');
                newsLength = $('> li', news).length;
                newsIndex = 1;

                if (newsLength > 0) {
                    $('#newsHolder .controls').append('<a href="#" id="newsPrev">Previous</a><a href="#" id="newsNxt">Next</a>');

                    //Top and tail
                    var $firstClone = $('#newsListing > li:first').clone().addClass('cloned');
                    var $lastClone = $('#newsListing > li:last').clone().addClass('cloned');
                    news.append($firstClone);
                    news.prepend($lastClone);
                    news.css({ top: -31 });

                    $('#newsPrev').click(function () {
                        newsIndex--;
                        var pos = newsIndex * 31;
                        news.animate({ top: -pos }, { easing: easingMethod, duration: 500, queue: false, complete: function () {
                            if (newsIndex == 0) {
                                news.css({ top: ((newsLength) * 31) * -1 });
                                newsIndex = newsLength;
                            }
                        }
                        });

                        if (typeof newsTimer != 'undefined') {
                            clearTimeout(newsTimer);
                            ausunity.newsslider.startTimer();
                        }

                        return false;
                    });


                    $('#newsNxt').click(function () {
                        newsIndex++;
                        var pos = newsIndex * 31;
                        news.animate({ top: -pos }, { easing: easingMethod, duration: 500, queue: false, complete: function () {
                            if (newsIndex > newsLength) {
                                news.css({ top: -31 });
                                newsIndex = 1;
                            }
                        }
                        });

                        if (typeof newsTimer != 'undefined') {
                            clearTimeout(newsTimer);
                            ausunity.newsslider.startTimer();
                        }

                        return false;
                    });

                    ausunity.newsslider.startTimer();
                }
            });
        },
        startTimer: function () {
            newsTimer = window.setInterval(function () {
                $('#newsNxt').click();
            }, 9000);
        }
    }
});








