jQuery(document).ready(function($){

	// external links, pops up a new win
	$("a[rel=external]").click(function(){
		window.open(this.href);
		pageTracker._trackEvent('Open Window', 'Open', 'External url: '+this.href);  // google analytics
		return false;
	});
	// ?? JOnas?
	$('#selector').hover(
		function(){
			$(this).addClass('hover');
		},
		function(){
			$(this).removeClass('hover');
		}
	);
        // poup info on flickr thumbs
/*	$(".js_flickrpop").hover(function(){
		$(this).children(".flickrpopup").show();
	},function(){
		$(this).children(".flickrpopup").hide();
	})*/
	 
	// menu (set hover on $("#submenu li") instead if we need deeper levels...)
	// TODO - a bit flaky right now, maybe the a href in li?
	$("#submenu>li").addClass("topmostsub").hover(function(){
		$(this).children("ul").show();
	},function(){
		$(this).children("ul").hide();
	});
	$("#submenu ul").hide();
	 
	// pics on startpage
	$("#moviepres").cycle({
		fx:     'fade',
		timeout: 3000
		//after:   onAfter
	});
	
	$(".bigpict").cycle({
		fx:     'fade',
		timeout: 3000
		//after:   onAfter
	});

	// events page
	$("#eventnav li").not(".year").click(function(){
		$("#eventnav li").removeClass("current");
		$(this).addClass("current");
		var newmon=this.id.replace("js_ev","");
		$(".month").removeClass("current").hide();
		$("#month"+newmon).addClass("current").show();
		if (window.location.href.match(/#[0-9][0-9]/)) {
			window.location.href=window.location.href.replace(/#[0-9][0-9]/,"#"+newmon);
		} else {
			window.location.href+="#"+newmon;
		}
		pageTracker._trackEvent('Calendar Events', 'Change view', 'Change to: '+mon);  // google analytics
		return false;	
	});
	$(".month").not(".current").hide();
        // eventpage test
	if (m=window.location.href.match(/#([0-9][0-9])$/)) { 
		if (m[1]) {
			var mon=m[1];
			$("#js_ev"+mon).click();
			pageTracker._trackEvent('Calendar Events', 'Change view', 'Load page: '+mon);  // google analytics
		} 
	} 
	// newsletter
	jQuery('.signup').hide().bind('submit',storeMailChimpAddress);
	jQuery('.js_openform').click(function(){
		jQuery(this).parent().children("form.signup").toggle();	
	})
	// startpage, movieframe/news headline and facebook box:
	jQuery("#moviepres *").css("display","block");
	// lightbox
	
	Shadowbox.init({
		// let's skip the automatic setup because we don't have any
		// properly configured link elements on the page
		skipSetup: true
	});
	jQuery("a[rel=production]").click(function(){ return false; });
	// set lightboxes and video links:
	updateLinks();	
	// lightbox
 	Shadowbox.init({
// 		// let's skip the automatic setup because we don't have any
// 		// properly configured link elements on the page
 		skipSetup: true
 	});
/*	jQuery("a[rel=shadowbox[production]],a[rel=shadowbox[start]]").each(function(){
		this.href=this.href.replace(/^/,"#");
	});*/
	
		jQuery("#fbbox").html('<iframe src="http://www.facebook.com/plugins/fan.php?id=277247591738&amp;width=230&amp;height=200&amp;connections=4&amp;stream=false&amp;header=false&amp;css=http://filmivasterbotten.com/fb-fanbox.css?23465224" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:230px; height:200px"></iframe>');
});
var shadow=0;
jQuery(window).bind("load",function(){
/*	jQuery("a[rel=shadowbox[production]],a[rel=shadowbox[start]]").each(function(){
		this.href=this.href.replace("#/filmproduktioner/?prod=","/getproduction.php?id=");
	});*/
	// ska vara vit, men har svart för synlighet på knappar!
// 	Shadowbox.setup("a.production",{
// 		content: 	this.href+"&.html",
// 		player:		"html",
// 		title:		"Test",
// 		height:		460,
// 		width:		900,
// 		options: {
// 			displayCounter:	true,
// 			onFinish:testar,
// 			overlayColor: "#000"
// 		} 
// 	});
	//alert("såja!")
// 	jQuery("#fbbox").html('<h3><a href="#">Facebook</a></h3>\
// 		<iframe scrolling="no" frameborder="0"\
// 		src="http://www.facebook.com/connect/connect.php?id=277247591738&stream=0&connections=3&locale=sv_SE&css=http://artopod.se/fb-fanbox.css?23465224" allowtransparency="true" style="border: none; width:220px; height:160px;"></iframe>\
// 		<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US" type="text/javascript"></script>\
// 		<script type="text/javascript">FB.init("71c8ba3df1d5ca79120cbe5fbe3593dd");</script>');



});
// function testar(gal){
// 					// går det få href in hit och göra en get för att få ut inehåll? nu är jag desperat...
// 					alert("galhej")
// 					//jQuery.get(,function(page) {
// 					jQuery("#piccycle").cycle({
// 						fx:     'fade',
// 						timeout: 3000
// 						//after:   onAfter
// 					});
// 					updateLinks();
// 					pageTracker._trackEvent('Production', 'Open popup', 'Finished loading '+d);  // google analytics
// 
// }

function updateLinks(){
	jQuery("a[rel=production],a.production").click(function(){
		//if (shadow==1) {
			this.href=this.href.replace("#/filmproduktioner/?prod=","/getproduction.php?id=");
			function startPopThings(){
				
			}
			var m,d="",reg=/prod=([0-9]+)/;
			if (m=this.search.match(reg)) {
				if (m[1]) {
					d="id="+m[1];
					pageTracker._trackEvent('Production', 'Open popup', 'Load prod: '+d);  // google analytics
				}
			}
			jQuery.get("/getproduction.php?"+d,function(page) {
				Shadowbox.open({
					content:	page,
					player:		"html",
					gallery:	"production",
					title:		"",
					height:		460,
					width:		900,
					options: {
						displayCounter:	false,
						onFinish:function(){
								jQuery("#piccycle").cycle({
									fx:     'fade',
									timeout: 3000
									//after:   onAfter
								});
								updateLinks();
								pageTracker._trackEvent('Production', 'Open popup', 'Finished loading '+d);  // google analytics
						},
						overlayColor: "#fff"
					} 
				});
			});
		//}
		return false;
		
	});
	jQuery("a[rel=video]").click(function(){
		var d=w=h="";
		if (m=this.search.match(/id=([0-9]+)/)) {
			if (m[1]) {
				d="id="+m[1];
			}
		}
		if (m=this.search.match(/width=([0-9]+)/)) {
			if (m[1]) {
				w=m[1];
			}
		}
		if (m=this.search.match(/height=([0-9]+)/)) {
			if (m[1]) {
				h=m[1]*1+50;
			}
		}
		pageTracker._trackEvent('Production', 'Open video', 'Video: '+d);  // google analytics
		$container=jQuery(".videoplay:first");
		$playbutton=jQuery(this);
		jQuery.get(this.href,function(html) {
			jQuery('#piccycle,.bigpict').cycle('stop');
			$playbutton.hide();
			$container.html(html);
			//if (player) player.playVideo();
			updateLinks();
			pageTracker._trackEvent('Production', 'Open video', 'Finished video: '+d);  // google analytics
				
// 			Shadowbox.open({
// 				content:    page,
// 				player:     "html",
// 				gallery: "video",
// 				title:      "",
// 				height:     h,
// 				width:      w,
// 				options: {
// 					displayCounter:	false,
// 					handleOversize: "none",
// 					onFinish:startPopThings,
// 					overlayColor: "#fff"
// 				} 
// 			});
		});
					
		return false;
	});

	jQuery('.type-bug, .resultDiv_gsvb,.js_flickrpop,#ext a').tipsy({fade: true}); //, gravity: 's'
}
$clickobj="";
function productionPopup(init) {
	if (Shadowbox=="undefined") {
		setTimeout("productionPopup(true)",500);
	} else {
		if (init) {
			Shadowbox.init({
				// let's skip the automatic setup because we don't have any
				// properly configured link elements on the page
				skipSetup: true
			});
		}	
		function startPopThings(){
			jQuery("#piccycle").cycle({
				fx:     'fade',
				timeout: 3000
				//after:   onAfter
			});
			updateLinks();
			pageTracker._trackEvent('Production', 'Open popup', 'Finished loading '+d);  // google analytics
		}
		var m,d="",reg=/prod=([0-9]+)/;
		if (m=this.search.match(reg)) {
			if (m[1]) {
				d="id="+m[1];
				pageTracker._trackEvent('Production', 'Open popup', 'Load prod: '+d);  // google analytics
			}
		}
		jQuery.get("/getproduction.php?"+d,function(page) {
			Shadowbox.open({
				content:    page,
				player:     "html",
				gallery: "production",
				title:      "",
				height:     460,
				width:      900,
				options: {
					displayCounter:	false,
					onFinish:startPopThings,
					overlayColor: "#fff"
				} 
			});
		});
	} 
}
					
function showMailChimpForm(ID,formaction) { // something like http://matbiten.us1.list-manage.com/subscribe/post?u=2d853a2fa0ed25ad6fbf1c6bb&amp;id=180ec3f1a5
	var fstr='<div id="mc_embed_signup">\
	<form action="'+formaction+'" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" style="font: normal 100% Arial;font-size: 12px;">\
		<label for="mce-EMAIL" style="display: block;margin: .3em 0;line-height: 1em;font-weight: bold;">E-postadress <strong class="note-required">*</strong></label>\
		<input type="text" value="" name="EMAIL" class="required" id="mce-EMAIL">\
		<input type="submit" value="Prenumerera" name="subscribe" id="mc-embedded-subscribe" class="btn" style="clear: both;width: auto;display: block;margin: 1em 0 1em 5%;">\
			<div id="mce-responses" class="desc">\
				<div class="response" id="mce-error-response" style="display: none;"></div>\
				<div class="response" id="mce-success-response" style="display: none;font-weight: bold;color: #529214;"></div>\
			</div>\
	</form>\
	</div>';
	jQuery("#"+ID).html(fstr);
	return false;
					
}
				
				
// AJAX call sending sign up info to store-address.php
function storeMailChimpAddress(event) {
	// Update user interface
	var $output=jQuery(this).children('.response');
	$output.html('Sparar...').fadeIn();
	// Prepare query string and send AJAX request
	// NOTE: You'll need to define what info your list will accept in your MailChimp account in 
	// Lists > click the desired list > list settings > Merge tags for personalization 
	var list_id=this.elements['list_id'].value;
	var pars = 'ajax=true&email=' + escape(jQuery('#email_'+list_id).val()) + '&list_id=' + escape(list_id);
	jQuery.get('/wp-content/themes/fiv/incl/mailchimp.php?'+pars,function(html) {
		$output.html(html);
	});
	return false;
}




