$(function()
{
	$('#abouts div div').equalizeCols();
	
	//$('input[type="text"]').filter(':first').focus();
	
	PartnerAdmin.init();
	
	QuickFilter.init();
	
	Magpie.init();
	
	TypeFilter.init();
	
	FancyBox.init();
	
	SiteSearch.init();
	
	Forum.init();
	
	NewWindow.init();
	
	WebshopLogin.init();
	
	ForgotLogin.init();
	
	Events.init();
	
	$('#engage .twitter div').tweetable({
		username: 'energieff',
		limit: 1,
		replies: false
	});
	
	
});

$.expr[':'].icontains = function(obj, index, meta, stack)
{
	return (obj.textContent || obj.innerText || jQuery(obj).text() || '').toLowerCase().indexOf(meta[3].toLowerCase()) >= 0;
};

var Events =
{
	init:function()
	{
		this.init_info();
	},
	
	init_info:function()
	{
		$('#event .listing ul a').click(function()
		{
			var $this = $(this);
			
			var header = $('#event h2').text();
			
			var date = $this.find('em').text();
			var title = $this.find('span').text();
			var pdf = $this.attr('href'); 
			
			$.fancybox(
					'<div id="doc-download"><h1>' + header + '</h1><p>' + date + ', ' + title + '</p><p class="download"><a href="' + pdf + '">Ladda ner PDF</a></p></div>'
				);
			return false;
		});
	}
}

var WebshopLogin =
{
	init:function()
	{
		var $form = $('#abouts div.webshop form');
		
		$form.submit(function()
		{
			var username = $('#username').val();
			var password =  $('#password').val();

			WebshopLogin.popup(username, password,'authenticate','');
			return false;
		});
	},
	
	popup:function(username, password, actiontype, webshopid)
	{
		var tScreenResolution = "";
		var popupWidth;
		var popupHeight;

		if(screen.availWidth > 1200 && screen.availHeight > 850) {
			tScreenResolution = "1280x1024";
			popupWidth = "1270";
			popupHeight = "946";
		} else {
			tScreenResolution = "1024x768";
			popupWidth = "1014";
			popupHeight = "730";
		}

		//alert("screen.availWidth:"+screen.availWidth);
		//alert("screen.availHeight: " +screen.availHeight);

		try {
			var url = 'https://webshop.ahlsell.com/vivp/Login.do?SCM=A209381B421948E218F12984A1239487E12439789123789FF23FFAACCEASd981241CCAE209841FAAECAE986712873161ADEFCEAF2498167249867239478ADE&userName=' + username + '&password=' + password + "&actionType="+actiontype+"&webshopid="+webshopid+"&screenResolution="+tScreenResolution+"&failureURL=/vivp/index.jsp";
			var myWindow = window.open(url, 'mywindow', 'status=no,titlebar=no,width='+popupWidth+',height='+popupHeight+',left=0,top=0,scrollbars=yes,resizable=yes,screenX=0,screenY=0')
			myWindow.focus();
		}
		catch(err) {
			alert("LOGIN. Error in function invokeAjaxAction.\n Error: " + err);
		}
	}
};

var NewWindow =
{
	init:function()
	{
		var $links = $('a[href^="http://"], a[href^="https://"]');
		
		$links.click(function()
		{	
			var $this = $(this);
			var href = $this.attr('href');
			
			if (href.indexOf('energieffektiv.com') == -1)
			{
				window.open(href);
				return false;
			}
		});
	}
};

var SiteSearch =
{
	init:function()
	{
		$('#nav li.search a').toggle(function()
		{
			var $this = $(this).parent('li');
			
			$this.animate(
			{width: '300'}, 250, function()
			{
			    $this.find('form').fadeIn('fast',function()
				{
					$this.find('input').focus();
				});
			});
			
			return false;
		},
		function()
		{
			var $this = $(this).parent('li');
			
			$this.removeAttr('style').find('form').hide();
			
			return false;
		});
		
		$('a.trigger-search').click(function()
		{
			$('#nav li.search a').trigger('click');
			return false;
		});
	}
};

var TypeFilter =
{
	init:function()
	{
		var $container = $('div.type-filter');
		
		var $filter_items = $('#nav_cat_archive li li');
		
		var $quickfilter_field = $('#quick-filter input:text');
		
		$container.find('a').click(function()
		{
			$quickfilter_field.val('').trigger('keyup');
			
			var $this = $(this);
			
			var target_type = $this.attr('href');
			
			target_type = target_type.substr(1);
			
			$this.parent('li').siblings().removeClass('sel').end().addClass('sel');
			
			$filter_items.show();
			
			if (target_type != 'all')
			{
				$filter_items.not('.' + target_type).hide();
			}
					
			return false;
		});
	}
};

var FancyBox =
{
	init:function()
	{
		$('#doc-archive .listing li.doc a').fancybox
		(
			{
				'padding': 0,
				'margin': 0,
				'width' : 450,
				'autoDimensions' : true,
				'overlayOpacity' : 0.65
			}
		);

		$('a.zoom').fancybox({'overlayOpacity' : 0.75});
	}
};

var Magpie =
{
	init:function()
	{		
		if (jQuery.browser.msie && jQuery.browser.version.substr(0,1) < '8')
		{
			return false;
		}
		
		$('ul.mp li').each(function(i)
		{
			var $this = $(this);
			
			var id = 'ni-' + i; // generate id
			
			var $summary = $this.find('div.news-summary');
			
			$summary.attr('id',id); // get id from summary div
			
			var $link = $this.find('.title a');
			
			var link = $link.attr('href'); // get link
			
			$link.attr('href','#' + id); // set anchor link that fires of fancy box
			
			var header_text = $link.text(); // get header
			
			$('<h3>' + header_text + '</h3>').prependTo($summary); // prepend header to summary
			
			$('<p><a class="external" target="_blank" href="' + link + '">Läs hela artikeln</a></p>').appendTo($summary); // prepend link to summary
				
		});
		
		$('ul.mp li .title a').fancybox
		(
			{
				'padding': 0,
				'margin': 0,
				'width' : 450,
				'autoDimensions' : true,
				'overlayOpacity' : 0.65
			}
		);
	}
};

var QuickFilter =
{
	init:function()
	{
		var $container = $('#quick-filter');
		
		$container.submit(function()
		{
			return false;
		});
		
		$container.find('label').append('<a class="empty"></a>').find('a.empty').hide().click(function()
		{
			$container.find('input:text').val('').trigger('keyup').focus();
			
			return false;
		});
		
		var $empty_button = $container.find('a.empty');
		
		var $items = $('.listing li');
		
		$container.find('input').keyup(function()
		{
			var $this = $(this);
			
			var value = $this.val();
			
			$items.removeClass('hit');
			
			if (value != '')
			{
				var $hits = $items.filter(':icontains(' + value + ')');
				
				$hits.addClass('hit');
				
				if ($hits.size())
				{
					$items.css('opacity',0.25);

					$items.filter(':icontains(' + value + ')').removeAttr('style');
				}
				
				$empty_button.show();
				
			}			
			
			if (value == '')
			{
				$items.removeAttr('style');
				
				$empty_button.hide();
			}
			
		});
		
	}
};

var ForgotLogin =
{
	init:function()
	{
		var $container = $('#forgot-login');
		
		$container.find('form:last').hide();
		
		$container.find('p.toggle a').click(function()
		{
			var $this = $(this);
			
			$container.find('form:hidden').show();
			
			$this.parents('form').hide();
			
			$container.find('form:visible input[type="text"]').focus();
			
			return false;
		});
	}
};

var PartnerAdmin =
{
	init:function()
	{
		if ( ! $('#partner-admin').size())
		{
			return false;
		}
		
		this.init_delete_file();
	},
	
	init_delete_file:function()
	{
		var $filename = $('input#file_name');
		var $uploaded_file = $('#uploaded_file');
		
		$('input#delete_file').change(function()
		{
			if ($(this).is(':checked'))
			{
				$filename.val('');
				//$uploaded_file.hide();
			}
			else
			{
				$filename.val(filename);
				//$uploaded_file.show();
			}
		});
		
		$('input#file').change(function()
		{
			$filename.val('');
		})
	}
};

var Forum =
{
	init:function()
	{
		$('#login-reg a.login').click(function()
		{
			$('#login-reg').fadeOut(function()
			{
				$('#login').show().find('input:first').focus();
			});
			return false;
		});
	}
};

/*
 * tweetable 1.6 - jQuery twitter feed generator plugin
 *
 * Copyright (c) 2009 Philip Beel (http://www.theodin.co.uk/)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * With modifications from Philipp Robbel (http://www.robbel.com/) and Patrick DW (stackoverflow)
 * for IE compatibility.
 *
 * Revision: $Id: jquery.tweetable.js 2011-01-06 $ 
 *
 */
(function(a){a.fn.tweetable=function(b){var c={limit:5,username:"philipbeel",time:false,replies:false,position:"append"};var b=a.extend(c,b);return this.each(function(e){var d=a(this);var j;var i="";var f=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];var g="http://api.twitter.com/1/statuses/user_timeline.json?screen_name=";var h="&count=";a.getJSON(g+c.username+h+c.limit+"&callback=?",d,function(k){a.each(k,function(m,n){if(m==0){j=a('<ul class="tweetList">')[c.position.toLowerCase()+"To"](d)}if(c.replies===false){if(n.in_reply_to_status_id===null){j.append('<li class="tweet_content_'+m+'"><p class="tweet_link_'+m+'">'+n.text.replace(/#(.*?)(\s|$)/g,'<span class="hash">#$1 </span>').replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,'<a href="$&">$&</a> ').replace(/@(.*?)(\s|\(|\)|$)/g,'<a href="http://twitter.com/$1">@$1 </a>$2')+"</p></li>")}}else{j.append('<li class="tweet_content_'+m+'"><p class="tweet_link_'+m+'">'+n.text.replace(/#(.*?)(\s|$)/g,'<span class="hash">#$1 </span>').replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,'<a href="$&">$&</a> ').replace(/@(.*?)(\s|\(|\)|$)/g,'<a href="http://twitter.com/$1">@$1 </a>$2')+"</p></li>")}if(c.time==true){for(var l=0;l<=12;l++){if(f[l]==n.created_at.substr(4,3)){i=l+1;if(i<10){i="0"+i}}}}})})})}})(jQuery);

