function popupPrep(){
	// Create holder for popup div
	var holderdiv = document.createElement("div");
	$(holderdiv).attr("id", "popupholder");
	$("#wrapper").append(holderdiv);
	// Create overlay div to darken background
	var overlaydiv = document.createElement("div")
	$(overlaydiv).attr("id", "overlay")
	$("body").append(overlaydiv)
	overlaydiv = $(overlaydiv);
	overlaydiv.hide();
	overlaydiv.fadeTo(10, 0.75);
	overlaydiv.width($("body").width());
	overlaydiv.height($("body").height());
}     

function popupClose(){
	$("#popupholder").empty();
	$("#overlay").hide();
	$("body").removeClass("csprint");
	return false;
}

function howPopups(){
	popupPrep();
	$("a.howpopup").bind("click", howPopup)
}

     
function networkPopups(){
	popupPrep();
	$("#intro .networkpopup").bind("click", function(){
		networkPopup($(this).attr("href"))
		return false;
	})
}

function networkPopup(url){

	window.location.href = url;
	return false;
}

function howPopup(){
	return false;
}

function clientPopups(){
}
function sidebarClientPopups(){
} 

function clientPopup(){
	return false;
} 



function sidebarRotator(div, path, callback){
	var entrydiv = div.children(".entry")
	var controldiv = div.children(".controls")
	var pcount = controldiv.children("span")

	var id = "sbr"+new Date().getTime()
	div.attr("id", id);

	currphoto[id] = 1;
	entrydiv.hide()
	var totalphoto = 0;
	entrydiv.load(path, "", function(s){
		if(typeof callback == "function")
		callback.call();
		// Show controls
		controldiv.show()
		
		// Count children
		totalphoto = entrydiv.children().length;
		
		// Display first entry
		entrydiv.children("div:not(:first)").hide()
		entrydiv.show();
	}) 
	
	function updateCount(currphoto){
		pcount.attr("class", "")
		pcount.addClass("num"+currphoto+"of"+totalphoto)
	} 
	
	controldiv.children(".prev, .next").bind("click", {itemstring: "#"+id+" .entry div", updatecount: updateCount, controldiv: "#"+id+" .controls", cp: id, cycle: true}, doRotate)
}

var currphoto = new Object()
function doRotate(e){
	var cycle = typeof e.data.cycle == "boolean" ? true : false;
	if($(this).parent().hasClass("prev") || $(this).hasClass("prev"))
	prev = true;
	else
	prev = false;	
	var cont = prev ? $(e.data.itemstring+":visible").prev().length : $(e.data.itemstring+":visible").next().length;
	if(cont){
		currphoto[e.data.cp] = prev ? --currphoto[e.data.cp] : ++currphoto[e.data.cp];
		var np = prev ? $(e.data.itemstring+":visible").prev() : $(e.data.itemstring+":visible").next();
		$(e.data.itemstring+":visible").hide();
		np.show();   
		
		// Code for managing active state of buttons (in no-cycle mode)
		if(!cycle){
			// Disable clicked button if needs be
			var disable = prev ? $(e.data.itemstring+":visible").prev().length : $(e.data.itemstring+":visible").next().length;
			// check disable == false (i.e, no more photos in this direction)
			if(!disable)
			$(this).addClass("disabled");	
			// enable button in opposite direction
			if(prev)
			$(e.data.controldiv+" .next a, "+e.data.controldiv+" .next").removeClass("disabled");
			else
			$(e.data.controldiv+" .prev a, "+e.data.controldiv+" .prev").removeClass("disabled");
		}		
		e.data.updatecount.call(null, currphoto[e.data.cp])
		return false; 
	} else if(cycle) {
		$(e.data.itemstring+":visible").hide();
		if (prev){
			var len = $(e.data.itemstring).length;
			currphoto[e.data.cp] = len;
			$($(e.data.itemstring)[len-1]).show();
		} else {
			currphoto[e.data.cp] = 1;
			$($(e.data.itemstring)[0]).show();
		}
		e.data.updatecount.call(null, currphoto[e.data.cp])
	}
	return false;	
}      

function solutionPathways(div){    
	div.children().children("span").css({display: "block"}).hide().fadeTo(0, 0.5);
	div.children("div").bind("click", function(){
		document.location = $(this).children("a").attr("href");
	})
	div.children("div").bind("mouseover", function(){
		$(this).children("span").show();
	}) 
	div.children("div").bind("mouseout", function(){
		$(this).children("span").hide()
	})	
}

function ajaxTabs(tabs, holder){
//	console.log(tabs.children("li").children("a"))

	tabs.children("li").children("a").bind("click", function(){
		tabs.children("li").removeClass("selected")
		$(this).parent("li").addClass("selected")
		var src = $(this).attr("href")+"?ajax";
		holder.load(src, "", function(s){
			popupUtil();
		})
		return false;
		
	})
}


function scrollList(){
	if($(this).hasClass("disabled"))
	return false;
	var a = $(this)
	var moveto = a.parent().hasClass("prev") ? "0" : "-870px";
	$("#paths ul").animate({
		marginLeft: moveto
	}, 1000, "", function(){
		$("#controls .disabled").removeClass("disabled");
		a.addClass("disabled");
	});
	return false;
}    

function logoFilter(){
	$("#clistbar a").bind("click", function(){
		classarray = $(this).attr("href").split("=");		
		if(classarray.length == 1){
		   $("#clogos").attr("class", ""); 
		} else {
			classname = classarray[classarray.length-1]
			$("#clogos").attr("class", "filter "+classname);	
		}
		
		return false;
	})
}

function pageTools(){

	$("#pagetools .print").bind("click", function(){
		window.print();
		return false;
	})
	$("#pagetools .email").bind("click", function(){
		var cr = String.fromCharCode(13)
		var mail_str = "mailto:?subject=Link to website of Qwikker - (http://qwikker.com)"
		mail_str += "&body=Below is a link to the Qwikker website that I wanted to share with you.";
        mail_str += "%0A%0ATo view the page, visit: "+location.href;
  		mail_str += "%0A%0A"
		mail_str += "-------------------------------------------"
		mail_str += "%0AAbout Qwikker"
		mail_str += "%0AQwikker is the leader in location-based content distribution for mobile devices. Qwikker powers the world's largest network of interactive mobile content hotspots and enables consumers to download music, videos, games, coupons, news and more to their mobile devices free of charge, and without requiring a data plan. This service provides brands with the ability to connect with people and deliver tailored on-demand content to consumers who choose to opt-in at a location."
		location.href = mail_str;
		return false;
	})
}

function fc_popNetwork(url){
	//console.log(url)
	networkPopup(url);
}  

function popupUtil(){
	$("#content a.popup, #footer a.popup, #globalnav a.popup").attr("target", "_blank");
}


function requestInfo(anchorstring){
   $(anchorstring).bind("click", function(){
		var mywin=open(this.href, 'Qwikker', "width=470,height=400,toolbar=no,copyhistory=now,scrollbars=no,resizable=no,directories=no,menubar=no,status=no,location=no");
		return false;
	}) 
}

function validateEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false)
	return false;
	return true;
}

function emailUpdateValidate(){
	$("#emailupdateform").bind("submit", function(){
		if(!validateEmail($("#emailaddress").val())){
			$("#emailaddress").addClass("error");
			$("#emailaddress").focus();
			return false;
		}
		return true;
	})
}

var flashparams = {
	wmode: "transparent",
	menu: "false",
	allowscriptaccess: "network"
}

$(document).ready(function(){
	if($("#networkmap").length){
	networkPopups();
	swfobject.embedSWF(root+"swfs/map.swf", "networkmap", "434", "225", "8", "", {
		uslink: root+"network/unitedstates.php",
		europelink: root+"network/europe.php",
		auslink: root+"network/australia.php"}, flashparams);	
	}
	
	if($("#solutionsflash").length)
	swfobject.embedSWF(root+"swfs/gradtext.swf", "solutionsflash", "235", "123", "8", "", {}, {menu: "false"});	

	if($( ".network #flashrotator").length)
	swfobject.embedSWF(root+"swfs/imgrotator.swf", "flashrotator", "180", "135", "8", "", {xmlstring: root+"swfs/partners.xml", imgpause:2000}, flashparams)
	
	if($( ".about #flashrotator").length)
	swfobject.embedSWF(root+"swfs/imgrotator.swf", "flashrotator", "180", "135", "8", "", {xmlstring: root+"swfs/clients.xml", imgpause:2000}, flashparams)
	
	if($(".solutions #intro #flashbanner").length){
		section = $("#intro").attr("class")
		swfobject.embedSWF(root+"swfs/solutionsbanner.swf", "flashbanner", "434", "225", "8", "", {xmlstring: root+"swfs/"+section+".xml", imgpause:3000, solution:section}, flashparams)
	}
	
	if($("#clogos").length)
	//clientPopups();
	
	if($("#clistbar").length)
	logoFilter();

	if($("#sidebar .campaign").length)
	sidebarRotator($("#sidebar .campaign"), root+"ajaxincludes/sidebarcampaign.php", sidebarClientPopups);
	//
	
	if($("#sidebar .highlights").length)
	sidebarRotator($("#sidebar .highlights"), root+"ajaxincludes/sidebarhighlights.php");
	
	if($("#intro.pathways").length)
	solutionPathways($("#intro.pathways"))
	
	if($("#tabs").length)
	//ajaxTabs($("#tabs"), $("#tabcontent"))
	
	if($("#pagetools").length)
		alert();
	pageTools();
	
	popupUtil();
	
	requestInfo("a.requestinfo")
	
	if($("#emailupdateform").length)
	emailUpdateValidate();

	if($("#flashcontent").length)
	swfobject.embedSWF("http://blip.tv/play/AZ+Tdw", "flashcontent", "240", "180", "8", "", {}, flashparams)
	                                                                                             
	
	
	if($("#homepageflash").length){
	    swfobject.embedSWF(root+"swfs/homepage.swf", "homepageflash", "941", "364", "8", "", {
		featureLink_1: root+"solutions/outofhome/",
		featureLink_2: root+"solutions/retail/",
		featureLink_3: root+"solutions/events/",
		arrowLink_1: root+"solutions/outofhome/",
		arrowLink_2: root+"solutions/retail/",
		arrowLink_3: root+"solutions/events/"
	}, {menu: "false", bgcolor: "#000000", wmode: "transparent"});	
		$("#paths li .overlay").fadeTo(10, 0.5).css("display", "block");
		$("#controls").css("display", "block");
	
		$("#paths li").bind("mouseenter", function(){
			$(this).children(".overlay").addClass("over");
		}).bind("mouseleave", function(){
			$(this).children(".overlay").removeClass("over");
		}).bind("click", function(){
		  document.location.href = $(this).children("a").attr("href");
		})
    
		$("#controls .prev a, #controls .next a").bind("click", scrollList);
	}  
})  

// Variable for add this widget

var addthis_pub = 'addthis-qwikker';

