function hideWelcomeBar(){
    $('#welcome_bar').fadeOut('slow');
    $.cookie('firstTime', 0, {expires: 365});
    return false;
}

$(document).ready(function(){
	var c = $.cookie('style');
	if (c) switchStylestyle(c);

	/**
	* Alternate Row colours - anything with .alternate
	**/
	$("td.alternate:odd").addClass("alternate-even");

	if ($.cookie('style') != "textonly") {

	ddsmoothmenu.init({
		mainmenuid: "top-menu", //menu DIV id
		orientation: 'h', //Horizontal or vertical menu: Set to "h" or "v"
		classname: 'ddsmoothmenu', //class added to menus outer DIV
		contentsource: "markup" //"markup" or ["container_id", "path_to_menu_file"]
	});
	}

	// modal windows init
	// move to top of page
		$('#modalLogoutWarning').appendTo('#container');
	  $('#modalLogoutWarning').jqm({
	    modal: true,
	    overlay: 75, /* 0-100 (int) : 0 is off/transparent, 100 is opaque */
	    onHide: function(h) {
        /* callback executed on window hide. Hide notice, overlay. */
        h.w.fadeOut("slow",function() { if(h.o) h.o.remove(); }); } 
  })

	// welcome bar - new visitor 

	$(document).ready(function(){
		var c1 = $.cookie('firstTime');
		if (!c1 || c1==1) {
		     $.cookie('firstTime',1, {expires: 365});
		     $('body').prepend('<div id="welcome_bar"><span class="welcome_left">Robust Details has been updated. <a href="/index.php?id=13030">Tell us what you think.</a></span><span onClick="hideWelcomeBar()" id="welcome_bar_hide"><a href="#">close X</a></span></div>');
		     showWelcomeBar = function(){$('#welcome_bar').fadeIn('slow')}
		     setTimeout("showWelcomeBar()",1000);
		}
	});


});

/**
* Styleswitch stylesheet switcher built on jQuery
**/
function switchStylestyle(styleName) {
	//$('link[@rel*=style][@title]').each(function(i) { 
	$('link[rel*=style][title]').each(function(i) {
		this.disabled = true;
		if (this.getAttribute('title') == styleName) {this.disabled = false;}
	});
	$.cookie('style',styleName, {expires: 365});
}

function bookmarkSite() {
	if ($.browser.msie) {
		window.external.AddFavorite("http://www.robustdetails.com', 'Robust Details Limited - the building industry's alternative to pre-completion sound testing");
	}
	else {
		alert("Sorry the bookmark function only works with Internet Explorer");
	}
return false;
}

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars(){
      var vars = [], hash;
      var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
       
      for(var i = 0; i < hashes.length; i++)
      {
	hash = hashes[i].split('=');
	vars.push(hash[0]);
	vars[hash[0]] = hash[1];
      }
       
      return vars;
}

/**
* Log out function
**/
function logMeOut(){
	$('#loggingOutMessage').html('Logging Out <img src="/fileadmin/template/main/rdl-v2.0/imgs/mini-ajax-loader.gif" />');
	$.post('/index.php?id=12581',{
		pid: '10224',
		redirect_url: '',
		submit: 'logout',
		logintype: 'logout'
		}, function() {
			//window.location.reload(true);
			window.location = "/";
	});
	return false;	
}


// Member login
function memberLogin(button) {
	$(button).attr("onClick","");
	$('#member_login').load('/index.php?id=12592', function() {
		$('#colRight .tx-newloginbox-pi1-forgotP').hide();
		//$('#ajax_login_wrap #tx-newloginbox-pi1-login_submit').click(memberLoginSubmit);
		$('#ajax_login_wrap .tx-newloginbox-pi1 form').submit(memberLoginSubmit);
		$('#member_login').fadeIn('fast');
		$('#ajax_login_wrap .tx-newloginbox-pi1 form input#tx-newloginbox-pi1-user').focus();
	});
	return false;
}

function hideShowMemberLogin() {
	$('#ajax_login_wrap').hide();
	$('#login_banner a').click(
	    function () {
		$('#ajax_login_wrap').fadeIn('fast');
		$('#login_banner a').click(function (){
		    document.location = '/index.php?id=12509';
		});
		return false;
	    });
	    return false;
}

function memberLoginSubmit() {
	jname = $("#colRight #tx-newloginbox-pi1-user").val();
	jpass = $("#ajax_login #tx-newloginbox-pi1-pass").val();
	$('#ajax_login_wrap .tx-newloginbox-pi1 form').replaceWith("<img src='/fileadmin/template/main/rdl-v2.0/imgs/mini-throbber.gif' /> Logging in");
	$.post('/index.php?id=12592',{
		user: jname,
		pass: jpass,
		pid: '10224',
		redirect_url: '',
		submit: 'login',
		logintype: 'login'
		}, function(data) {
			$('#member_login').html(data);
			$('#ajax_login_wrap .tx-newloginbox-pi1 form').submit(memberLoginSubmit);
			// #TODO if success take to member lounge
			if( data.match("Success") ) {
				window.location = '/index.php?id=12509';
			}
	}, "html");
	return false;
}

/**
* ONQ Help Tags / Tooltips
**/
$(document).ready( function() {
     $('.tx-onqform-pi1 div.help_text').hide().wrapInner('<div class="help_text_top"></div>').append('<div class="help_text_bottom"></div>');
     $('.tx-onqform-pi1 div.help_text').addClass('help_text_hover');
     $('.tx-onqform-pi1 input').focus( function(){
          $('.tx-onqform-pi1 input').removeClass('inputHasFocus');
          $(this).addClass('inputHasFocus');
          $('div.help_text').fadeOut();
	  $('#colNormal').css('z-index','101');
	  $('#colRight').css('z-index','100');
          $(this).parent().find('div.help_text').fadeIn();
     });
     //$('li.help_text').prepend('<div class="help_marker"><img src="/fileadmin/template/main/os/img/help_icon.gif" width="15" height="15" /></div>');
     $('li.help_text').each( function(){
         if( $(this).find('.onqform_errors').length > 0 ) {
             $(this).find('.onqform_errors').after('<div class="help_marker"><img src="/fileadmin/template/main/rdl-v2.0/imgs/onq-help-icon.gif" width="19" height="19" /></div>');
         } else {
             $(this).prepend('<div class="help_marker"><img src="/fileadmin/template/main/rdl-v2.0/imgs/onq-help-icon.gif" width="19" height="19" /></div>');
         }
     });
     $('div.help_marker').hover(
          function() {
             if( $(this).parent().find('.inputHasFocus').length == 0) {
		 $('#colNormal').css('z-index','101');
		 $('#colRight').css('z-index','100');
                 $(this).parent().find('div.help_text').show();
                 $('.tx-onqform-pi1 .inputHasFocus').parent().find('.help_text').fadeOut();
             }
         }, function() {
             if( $(this).parent().find('.inputHasFocus').length == 0) {
                 $('.tx-onqform-pi1 .inputHasFocus').parent().find('.help_text').fadeIn();
                 $(this).parent().find('div.help_text').hide();
		 $('#colNormal').css('z-index','100');
		 $('#colRight').css('z-index','101');
             }
     });
});


// TODO - fix tilde selection
// Concertina and toggle Function

$(document).ready(function(){
	
        $(".csc-frame-07 h2").siblings().hide();


        $(".csc-frame-07 h2").click(function(){
            $(".csc-frame-07 h2").siblings().slideUp("fast");
            $(".concertina-open").removeClass("concertina-open");
            $(this).parent().addClass("concertina-open");
            $(this).siblings().slideDown("fast",function(){});
	    // set window hash to number of frame open
            openFrame = $('div.concertina-open')[0]; // find open frame
            allFrames = $('.csc-frame-07'); // all frames
            for ( x = 0 ; x < allFrames.length ; x++ ) {
		if ( allFrames[x] == openFrame ) { window.location.hash = 'frame-'+x; }
	    } 
    	});
	
        $(".csc-frame-08 h2").siblings().hide();
        $(".csc-frame-08 h2").click(function(){
            $(this).siblings().slideToggle("fast");
	    if( $(this).parent().hasClass("toggle-open") ){
                $(this).parent().removeClass("toggle-open");
		if(window.location.hash.split('#')[1] == $(this).parent().prev().attr('name')){window.location.hash = ""}
	    } else {
                $(this).parent().addClass("toggle-open");
		window.location.hash = "#"+$(this).parent().prev().attr('name');
	    }
    	});

	// check if we have a frame hash in url, if so then open and scrollto
	if (isNaN(window.location.hash.split('#')[1])) {
		//do nothing
	} else {
		$('a[name='+window.location.hash.split('#')[1]+']').next().find('h2').siblings().show();
		//$('.csc-frame-08:eq('+window.location.hash.split('#')[1]+' h2 ~)').show(); // open frame with correct index
		//$('.csc-frame-07:eq('+window.location.hash.split('-')[1]+')').attr('id',window.location.hash.split('#')[1]); // open frame with correct index
		
		//pScroll = $('a[name='+window.location.hash.split('#')[1]+']').offset().top();
		//$('html,body').animate({scrollTop: '+=' + pScroll + 'px'}, 1000);
	}
	
});

    

