/* IE6 Drop down menu fix */
var sfHover = function() {
	
	addHoverClass("main-navigation", "li");
	addHoverClass("controls", "a");
	addHoverClass("controls", "li");
}

if (window.attachEvent) window.attachEvent("onload", sfHover);


function addHoverClass(parentId, elementType)
{
	if(document.getElementById(parentId)){
		var sfEls = document.getElementById(parentId).getElementsByTagName(elementType);
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" hover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" hover\\b"), "");
			}
		}
	}
}


/* Input click clear */
var clickClear = function(i, value){
	var currentValue = value || input.val();
	i.click(function(){
		if(i.val() == currentValue){
			i.val('');		
			$(this).removeClass('example-text');
		}
	});
	i.blur(function(){
		if(i.val() == ''){
			i.val(currentValue);
			$(this).addClass("example-text");
		}
	});
}

/* Password field changer */
var changePassType = function(){
	var rpass = jQuery('#MemberLoginForm_LoginForm_Password'), //real password field
		rpass_div = jQuery('#MemberLoginForm_LoginForm #Password'),	
		fpass = jQuery('#MemberLoginForm_LoginForm_password-hint'); //fake password field
		//fpass_div = jQuery('#password-hint');
		
		rpass.css('display', 'none');		
		fpass.css('display', 'block').attr('value', 'Password');		
		fpass.addClass("example-text");
		
		fpass.focus(function(){			
			fpass.css('display', 'none');			
			rpass.css('display', 'block').attr('value', '').focus();
		});
		
		rpass.blur(function(){			
			if(rpass.val() == ''){
				rpass.css('display', 'none');				
				fpass.css('display', 'block').attr('value', 'Password');
			}			
		});					
}

/*-----------------------------------
	More or Less Links
---------------------------------- */
function moreLessLinks()
{
		
		jQuery('.more').css('display', 'none');
	
		jQuery('.more-less-link').click(function(e){
	
			e.preventDefault();	
			
			var more_rel = $(this).parent().children('.plain').children('.more');
		
			if(more_rel.css('display') == 'none')
			{		
				more_rel.slideDown();	
				$(this).html('Less');					
			} 
			else 
			{		
				more_rel.slideUp();	
				$(this).html('More&hellip;');			
			}
		
		});

}

/*-----------------------------------
	Doc ready
---------------------------------- */
jQuery(document).ready(function(){
	
	moreLessLinks();
	
	//Image Gallery
	$('#photos').galleryView({
		panel_width: 650,
		panel_height: 300,
		fade_panels: true,
		frame_width: 80,
		frame_height: 100,	
		transition_speed: 600,
		transition_interval: 99999999,		
		background_color: 'white',
		nav_theme: 'dark',	
		overlay_height: 20,
		overlay_opacity: 1.0,
		overlay_position: 'top',
		easing: 'easeInOutSine'
	});
	
	//Piro Box
	$().piroBox({
				  my_speed: 500, //animation speed
				  bg_alpha: 0.8, //background opacity
				  radius: 4, //caption rounded corner
				  scrollImage : false, // true == image follows the page _|_ false == image remains in the same open position
									   // in some cases of very large images or long description could be useful.
				  slideShow : 'false', // true == slideshow on, false == slideshow off
				  slideSpeed : 0, //slideshow
				  pirobox_next : 'piro_next', // Nav buttons -> piro_next == inside piroBox , piro_next_out == outside piroBox
				  pirobox_prev : 'piro_prev', // Nav buttons -> piro_prev == inside piroBox , piro_prev_out == outside piroBox
				  close_all : '.piro_close' // add class .piro_overlay(with comma)if you want overlay click close piroBox
		  });
	
	
	//Vimeo Video
	jQuery('#moogaloop').ready( function(){
										 	//front page											
											var parent = $('#moogaloop').parent();
											
											if(parent.attr('class') == "video") 
											{
												 embedVideo( $('#moogaloop').attr('title'), '480', '270' );
											}
											else
											{
												//video-page
												 embedVideo( $('#moogaloop').attr('title'), '650', '350' );
											}
										}
								);
	
	//Cost Calculator
	jQuery('.cc-submit').click( function () 
										  {											  
											  //try get parent object then get the associated text box
											  var inputValue = $(this).parent().children('.cc-input');											  
											  var turnAround = inputValue.val();
											  var cost = CostCalculation(turnAround);		
											
											 $(this).parent().parent().children('.calc-cost').children('span').html(cost);
										  }
							  );
	
	
	
	jQuery('#photos-nojs').css('display', 'none');
	jQuery('#photos-js').css('display', 'block');
	jQuery('#licencees-nojs').css('display', 'none');
	jQuery('#licencees').css('display', 'block');
	
	//Licensee Carousel
	$(function() {
		$("#licencees").jCarouselLite({
				auto: 6000,
				speed: 500,	
				visible: 6,
				easing: 'jswing'
		});
	});
	
	//Search
	if(jQuery('#Form_SearchForm_Search')){
		jQuery('#Form_SearchForm_Search').addClass("example-text");
		jQuery('#Form_SearchForm_Search').val('Type product or business name');
		new clickClear(jQuery('#Form_SearchForm_Search'), 'Type product or business name');
	}
	
	//Email
	if(jQuery('#MemberLoginForm_LoginForm_email')){
		jQuery('#MemberLoginForm_LoginForm_Email').addClass("example-text");
		jQuery('#MemberLoginForm_LoginForm_Email').val("Email");
		new clickClear(jQuery('#MemberLoginForm_LoginForm_Email'), 'Email');
	}
	
	//Password
	if(jQuery('#login-password')) {
		changePassType();	
	}	
	
	if(jQuery('#MemberLoginForm_LoginForm_password-hint'))
	{
		jQuery('#MemberLoginForm_LoginForm_password-hint').val('Password');
	}
	
	//Promotions
	if(jQuery('.ts-text')){
		jQuery('.ts-text').val('$10,000');
		jQuery('.ts-text').addClass("example-text");
		new clickClear(jQuery('.ts-text'), '$10,000');
	}

	jQuery('#print-control').css('display', 'block');
	jQuery('.calc-cost').css('display', 'block');
	
	
	
	//max word
	if($('#EntryText-CharCnt')){
		$('#EntryText-CharCnt').css('display', 'block');
		$('#EntryText-CharCnt span').html('25');
		jQuery('#EntryText textarea').addClass("example-text");
		jQuery('#EntryText textarea').val('Type Here...');
		new clickClear(jQuery('#EntryText textarea'), 'Type Here...');
	}
	
	var EntryText = {};	
	EntryText.value = '';
	
	$('#EntryText textarea').keyup(function(e){		
		
		var wordCnt = $(this).val(),
			maxWords = 25;
			
		wordCnt = wordCnt.split(' ').length - 1;
		
		if(wordCnt > maxWords){
			$(this).val(EntryText.value);				
		}	
		
		$('#EntryText-CharCnt span').html(maxWords - wordCnt);
		if(parseInt($('#EntryText-CharCnt span').html()) < 0){
			$('#EntryText-CharCnt span').html('0');
		}
		EntryText.value = $(this).val();
		
	});

	
});

/*-----------------------------------
	Cost Calculator 
---------------------------------- */
function CostCalculation(turnover)
{	
	var base_str = "$%s (incl. GST)";
	//strip dollar and comma's 
	turnover = turnover.replace(/$/g, "");
	turnover = turnover.replace(/,/g, "");
	
	//if still not a number, try strip all alpha chars
	if(isNaN(turnover))
	{		
		return "Invalid Amount";
	}
	
	if(turnover <= 249999)
	{
		return base_str.replace("%s", AddGST(250));
	}
	else if((turnover >= 250000) && (turnover <= 19999999))
	{
		return base_str.replace("%s", Math.round( AddGST(turnover / 1000) ));
	}
	else if(turnover >= 20000000)
	{
		return base_str.replace ("%s",AddGST(20000));
	}
	else 
	{
		return "Invalid Amount";
	}
}

function AddGST(_value)
{
	return _value + (_value * 0.1);
}

/*function stripAlphaChars(pstrSource) 
{ 
	var m_strOut = new String(pstrSource); 
    m_strOut = m_strOut.replace(/[^0-9]/g, ''); 

    return m_strOut; 
}*/



/*-----------------------------------
	Vimeo Embedding
---------------------------------- */
function embedVideo(video_id, width, height)
{
	var swf_id = 'moogaloop';
	
	var flashvars = {
		clip_id: video_id,
		show_portrait: 0,
		show_byline: 0,
		show_title: 0,
		js_api: 1, // required in order to use the Javascript API
		//js_onLoad: 'vimeo_player_loaded', // moogaloop will call this JS function when it's done loading (optional)
		js_swf_id: 'moogaloop' // this will be passed into all event methods so you can keep track of multiple moogaloops (optional)
	};
	
	var params = {
		allowscriptaccess: 'always',
		allowfullscreen: 'true',
		wmode: 'transparent'
	};
	
	var attributes = {};
		
	swfobject.embedSWF("http://vimeo.com/moogaloop.swf", swf_id, width, height, "9.0.0","expressInstall.swf", flashvars, params, attributes);
}