//Fix for fade in IE - stops text screwing up
jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
//---------------------


jQuery.fn.swap = function(b) {
    b = jQuery(b)[0];
    var a = this[0],
        a2 = a.cloneNode(true),
        b2 = b.cloneNode(true),
        stack = this;

    a.parentNode.replaceChild(b2, a);
    b.parentNode.replaceChild(a2, b);

    stack[0] = a2;
    return this.pushStack( stack );
};


function openFacebookSetup(){
	$('.panel_popup').hide();							 
		$('#facebookOptionsMenu').slideDown('slow');
		$('#facebookOptionsMenu').addClass('edit_panel_popup');
		UnTip();
}

$(document).ready(function(){
						   
	//$(document).pngFix();
	$(".privacyPopUp").colorbox({width:"480px",  height:"400px",  opacity:0.1, inline:true, href:"#privacyPolicy"});
	$(".termsPopUp").colorbox({width:"480px", height:"400px",  opacity:0.1, inline:true, href:"#terms"});

	$(".AR_branding").fadeTo(700,0.5)
	$(".AR_branding").mouseover(function() { $(this).fadeTo(0,1) })
	$(".AR_branding").mouseout(function() { $(this).fadeTo(0,0.5) })
	$(".topnav>li.usernav").fadeTo(700,0.4)
	$(".topnav>li.usernav").mouseover(function() { $(this).fadeTo(0,1) })
	$(".topnav>li.usernav").mouseout(function() { $(this).fadeTo(0,0.4) })
	//$("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled - Adds empty span tag after ul.subnav
	$("ul.topnav li span").mouseover(function() { //When trigger is clicked...
		
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('slow').show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
		});
		

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() { 
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});
	
	
	$(".closeEdit").click(function(){
			$('.panel_popup').hide();	
			return false;
	});
	
	//$(".loginButton").click(function(){loginPopUp()});
	$(".loginButton").click(function(){
		$('.panel_popup').hide();							 
		$('#loginBox').slideDown('slow');
		$('#loginBox').addClass('edit_panel_popup');
		UnTip();
}).mouseover(function(){ToolTip('Log In');}).mouseout(function(){UnTip();});
	
	$(".contactButton").live('click',function(){
		//SubmitCallToAction('email','j@creativej.co.uk','Email via Contact Button','Email contact button clicked','','<strong>Thanks for sending us a message.</strong><br /> We will respond as soon as possible if required.','',0);
		contactAction();
		UnTip();
}).live('mouseover',function(){ToolTip('Click to send us a message');}).live('mouseout',function(){UnTip();});
	/*
	$(".shareLinksMenu").live('click',function(){
		$('.panel_popup').hide();	
		$.colorbox({width:"500px", opacity:0.1, inline:true, href:"#shareLinksMenu"});
		UnTip();
}).live('mouseover',function(){ToolTip('Share This Page');}).live('mouseout',function(){UnTip();});
	*/
	$(".shareLinksMenu").click(function(){
		$('.panel_popup').hide();							 
		$('#shareLinksMenu').slideDown('slow');
		$('#shareLinksMenu').addClass('edit_panel_popup');
		UnTip();
}).mouseover(function(){ToolTip('Share This Page');}).mouseout(function(){UnTip();});

	
	$(".facebookOptionsMenu").click(function(){
		openFacebookSetup();
}).mouseover(function(){ToolTip('Facebook Page Options');}).mouseout(function(){UnTip();});
	
	$(".accountOptionsMenu").click(function(){
		$('.panel_popup').hide();							 
		$('#accountOptionsMenu').slideDown('slow');
		$('#accountOptionsMenu').addClass('edit_panel_popup');
		UnTip();
}).mouseover(function(){ToolTip('Your Account Details');}).mouseout(function(){UnTip();});
	
	$(".referMenu").live('click',function(){
		referPopup();
		UnTip();
}).live('mouseover',function(){ToolTip('Share This Page');}).live('mouseout',function(){UnTip();});
	
	function referPopup(){
			
		if(fb_tab!=1){ 
		$('.panel_popup').hide();	
		$.colorbox({width:"500px", opacity:0.1, inline:true, href:"#referMenu"});
		} else { 
		wopen ("facebook_tab/refer_popup.php?fb_popup&url="+shareURL,"referwindow",450,400);
		} 
	}
	
	//Set fade on panels which are turned off
	$(".unchecked").fadeTo(500,0.3)
	$(".unchecked>div.panelContent").slideUp(); 
	//alert($(".unchecked>.editButton").attr('id'))
	$(".unchecked>*>.editButton").fadeOut(); 
	//$(".editButton").fadeOut(); 
	
	
	
	
	$("tr.personRow").mouseout(function () {
		$(this).removeClass("highlight");
	});
	
	$("tr.personRow").mouseover(function () {
		$(this).addClass("highlight");
		
		
	});	
	
	// When a link is clicked
	$("a.tab").click(function () {
		changePageContent(this)
	});
	$("a.pageLinks").live('click',function () {
		changePageContent(this);
		//window.parent.scrollTo(0,0);
	});
function changePageContent(obj){
	var content_show = $(obj).attr("rel");
		var content_title = $(obj).text();
		document.title = pageTitle+' > '+content_title;
		if(content_show != ''){
		// switch all tabs off
		//$(".active").removeClass("active");
		
		// switch this tab on
		//$(obj).addClass("active");
		
			var content_show = $(obj).attr("rel");
			var content_id = $(obj).attr("id");
			// slide all content up
			if(content_show != 'ajax'){
				$(".content").slideUp();
			}  else {
				//$(".content").hide();	
				//$("#loadingPage").fadeIn();	
			}
			// slide this content up
			$("#"+content_show).slideDown();
			//alert()
			$(".active").removeClass("active");
			$("#"+content_id).addClass("active");
			
			var url =  $(obj).attr("href");
			if(document.getElementById('shareURL')){
			document.getElementById('shareURL').value=url;
			}
			
		}
		
		
}
					   
});

function togglePanel(id){
	var content_show = 'content_'+id;
	//$('element').length == 0; // no element found
	if($('#'+content_show).length != 0){
		// switch all tabs off
		$(".active").removeClass("active");
		
		$(".content").slideUp();
		
		// switch this tab on
		$('#tab_'+content_show).addClass("active");
		
		///var content_show = $(this).attr("rel");
		// slide all content up
		// slide this content up
		$("#"+content_show).slideDown();
		$("#"+content_show+'_tab').addClass("active");
		}
		
}

function showBizCast(id){
	$.colorbox({width:"650px", maxHeight:"80%", opacity:0.1, inline:true, href:"#"+id, overlayClose: false, onComplete: $.colorbox.resize()});
	//$('#cboxClose').css('display','none'); 
}

function uncheck(){
	$(".unchecked").fadeTo(500,0.3)
	$(".unchecked>div.panelContent").slideUp(); 
	$(".unchecked>*>.editButton").fadeOut(); 
 }
 
 function loginPopUp(){
		//$.colorbox({width:"600px", height:"450px", opacity:0.5, inline:true, href:"#loginBox", onComplete: $.colorbox.resize()});
		$('.panel_popup').hide();							 
		$('#loginBox').slideDown('slow');
		$('#loginBox').addClass('edit_panel_popup');
	}

function changeUserField(){
	InsertContent('saveUserDetails');
}


//var curPage = pageNumber;
var	shareURL = pageShareURL;		
function changePage(number){
	if(fb_tab == 1){
		FB.Canvas.setSize({ width: 520, height: 800 });	
		//FB.Canvas.setAutoResize();
		//FB.init({appId: facebook_appID, status: true, cookie: true, xfbml: true});
	}
	
	pageNumber = '0';
	shareURL = pageShareURL;
	var $data = $('#content_1');
	$('#loadingPage').fadeIn('fast');
	$(".content").hide();
			url = currentContentURL+'&ajax';	
			if(number){
				url += '&page='+number;
				pageNumber = number;
				shareURL = pageShareURL+'/'+pageNumber;
			} 
			//curPage = pageNumber;
			
			if(fb_tab==1){
				url += '&fb_tab';
			}
		// switch all tabs off
		//alert('#tab'+pageNumber);
		$(".active").removeClass("active");
		$('#tab'+pageNumber).addClass("active");	
		
		if(edit_access == 1){
			var page = '';
			if(number>0){
			 page = '/'+number;
			}
		document.location='./'+eventkey+page;
		}else{
		$data.load(url,  function(response, status, xhr) {
			//$data.fadeOut('slow'); 					  
		  if (status == "error") {
			//var msg = "Sorry but there was an error: ";
			//$("#error").html(msg + xhr.status + " " + xhr.statusText);
			if(FB){
				FB.Canvas.setSize({ width: 520, height: 5000 });
				//FB.Canvas.setAutoResize();
			}
		  } else {
			$('#loadingPage').hide();	
		    $data.fadeIn('slow'); 	
			sizeChangeCallback();
			if(FB){
				FB.Canvas.setSize({ width: 520, height: 5000 });
				//FB.Canvas.setAutoResize();
			}
			//$data.slideDown('slow'); 
			
			if(analytics == 1){
				var content_title = '';
				
				if(number>0){
					content_title = document.getElementById('tab'+number).innerHTML;
					content_title=content_title.replace(' ','_');
					//content_title=content_title.toLowerCase();
					
					//alert('/page/'+number+'#'+content_title)
					_gaq.push(['_trackPageview', '/page/'+number+'#'+content_title]);
				} else {
					//alert('/home')
					_gaq.push(['_trackPageview', '/home']);
				}
				
				//alert(content_title);
			}
		  }
		});
		}
		
	
}

function openShareMenu(){
	$('.panel_popup').hide();	
	$.colorbox({width:"500px", opacity:0.1, inline:true, href:"#shareLinksMenu"});	
}

var picked = 0;
function changeAutoHeaderImage(){
	var r_text = new Array ();
	var r_color = new Array ();
	r_text[0] = "http://www.angelpages.biz/wizard/images/headers/gradient_black_5f6365.png";
	r_color[0] = "5f6365";
	r_text[1] = "http://www.angelpages.biz/wizard/images/headers/gradient_grey_c9d8dd.png";
	r_color[1] = "c9d8dd";
	r_text[2] = "http://www.angelpages.biz/wizard/images/headers/gradient_red_fd1900.png";
	r_color[2] = "fd1900";
	r_text[3] = "http://www.angelpages.biz/wizard/images/headers/gradient_orange_ffa600.png";
	r_color[3] = "ffa600";
	r_text[4] = "http://www.angelpages.biz/wizard/images/headers/gradient_yellow_ffc500.png";
	r_color[4] = "ffc500";
	r_text[5] = "http://www.angelpages.biz/wizard/images/headers/gradient_green_b4de26.png";
	r_color[5] = "b4de26";
	r_text[6] = "http://www.angelpages.biz/wizard/images/headers/gradient_blue_5ed1f7.png";
	r_color[6] = "5ed1f7";
	r_text[7] = "http://www.angelpages.biz/wizard/images/headers/gradient_purple_e36ee5.png";
	r_color[7] = "e36ee5";
	r_text[8] = "http://www.angelpages.biz/wizard/images/headers/gradient_pink_fe5aaf.png";
	r_color[8] = "fe5aaf";
	//alert(r_text.length)
	
	//SELECT CONSECUTIVE
	var i = picked;
	
	if(i >= r_text.length){
		picked = 0;
		i = 0;
	} else {
		picked++;
	}
	
	//SELECT RANDOM
	//var i = Math.floor(r_text.length*Math.random())
	//if(i == picked){
	//	changeAutoHeaderImage();
	//	return false;
	//}
	//picked++;
	
	//document.write(r_text[i]);
	 document.getElementById('headerBackgroundColor').value=r_color[i];
	 document.getElementById('headerBackgroundColor').style.backgroundColor='#'+r_color[i];
	 document.getElementById('headerBackground').value=r_text[i];
	 document.getElementById('headerBackgroundPosition').value='bottom left';
	 document.getElementById('headerBackgroundRepeat').value='repeat-x';
	//document.getElementById('eventOrganiserLogo').value=r_text[i];
	changeHeaderBG();
	
	
	
}



var pickedLogo = 0;
function changeAutoLogoImage(){
	var r_text = new Array ();
	r_text[0] = "http://www.angelpages.biz/wizard/images/logos/ball_logo_white.png";
	r_text[1] = "http://www.angelpages.biz/wizard/images/logos/ball_logo_black.png";
	r_text[2] = "http://www.angelpages.biz/wizard/images/logos/ball_logo_red.png";
	r_text[3] = "http://www.angelpages.biz/wizard/images/logos/ball_logo_orange.png";
	r_text[4] = "http://www.angelpages.biz/wizard/images/logos/ball_logo_yellow.png";
	r_text[5] = "http://www.angelpages.biz/wizard/images/logos/ball_logo_green.png";
	r_text[6] = "http://www.angelpages.biz/wizard/images/logos/ball_logo_blue1.png";
	r_text[7] = "http://www.angelpages.biz/wizard/images/logos/ball_logo_blue2.png";
	r_text[8] = "http://www.angelpages.biz/wizard/images/logos/ball_logo_pink.png";
	r_text[9] = "http://www.angelpages.biz/wizard/images/logos/ball_logo_brown.png";
	r_text[10] = "http://www.angelpages.biz/wizard/images/logos/star_logo_gold.png";
	r_text[11] = "http://www.angelpages.biz/wizard/images/logos/star_logo_silver.png";
	
	
	//SELECT CONSECUTIVE
	var i = pickedLogo;
	
	if(i >= r_text.length){
		pickedLogo = 0;
		i = 0;
	} else {
		pickedLogo++;
	}
	//alert(i)
	//SELECT RANDOM
	//var i = Math.floor(r_text.length*Math.random())
	//if(i == picked){
	//	changeAutoLogoImage();
	//	return false;
	//}
	//picked++;
	
//document.write(r_text[i]);
	logoShowCheck = document.getElementById('headerLogoShow');
	logoShowCheck.checked=true;
	document.getElementById('eventOrganiserLogo').value=r_text[i];
	changeLogoImage()
}
