function prepExternalLinks(target) {
	target = ($defined(target)) ? target : $(document.body);
	// Open external links in new window
	var host = window.location.host;

	target.getElements('a').each(function (link) {
		var href = link.get('href');

		if ($defined(href) && href.contains('http://') && !href.contains(host) && href.charAt(0) !== '#') {
			link.addEvent('click', function (e) {
				e.stop();

				window.open(href);
			});
		}
	});
}

function show_item(heading, body_text, caption, link_url, img_path) {
	$$('div.copy h3')[0].set('text',heading);
	$$('div.copy p')[0].set('html',body_text);
	$$('div.media img')[0].set('src',img_path);
	$$('div.media p')[0].set('text',caption);
	
	//creates ul element
	var ul = new Element('ul');
	if ($$('div.copy ul').length > 0) {
		ul = $$('div.copy ul')[0];
	} else  {
		ul.inject($$('div.copy')[0], 'bottom');
	}

	if ($defined(link_url)) {
		link_url = JSON.decode(link_url);

		ul.empty();

		link_url.each(function (link) {
			ul.adopt(
				new Element('li').adopt(
					new Element('a', {
						href: link.link,
						text: link.text
					})
				)
			);
		});
	}

	prepExternalLinks(ul);
};

function change_contact_reason(type)
{
	if (type == 'complaints') { 
		$$('li.complaints-field').show(); 
	} else { 
		$$('li.complaints-field').hide(); 
	}
}

window.addEvent('domready', function () {	
	//EXPORT SECTION
	var map = $('map');
	if ($defined(map) && Browser.Plugins.Flash.version > 7) {
		var exportSwf = new Swiff(BASE_URL+'assets/swf/map.swf?_version=<?php echo(time());?>', {
			width: 813,
			height: 360,
			id: 'map_flash',
			name: 'map_flash',
			params: {
				menu: false,
				wmode: 'transparent',
				allowScriptAccess: 'always'
			},
			vars: {
				xml_url: 'assets/xml/dataExport.xml'
			}
		});

		map.empty();
		exportSwf.inject(map);
	}


	//HOMEAGE SECTION
	var cupboard = $('cupboard');
	if ($defined(cupboard) && Browser.Plugins.Flash.version > 7) {

		// Set defaults
		var xml = null;
		var thegap = '20';

		// Let's work out what XML file we're calling
		xml = BASE_URL + (($defined(CUPBOARD_PAGE) && CUPBOARD_PAGE != false) ? 'xml/brands/' + CUPBOARD_PAGE : 'xml/brands') + '.xml';

		var homeSwf = new Swiff(BASE_URL + 'assets/swf/cupboard.swf', {
			width: 980,
			height: 360,
			params: {
				menu: false,
				wmode: 'transparent'
			},
			vars: {
				xml_url: xml,
				gap: thegap,
				base_path: BASE_URL,
				page: CUPBOARD_PAGE
			}
		});
		
		cupboard.empty();
		homeSwf.inject(cupboard);

		var cols = $$('.col');
		cols.addClass('js');

		cols.addEvent('click', function (e) {
			e.stop();
			var link = this.getElement('a').get('href');
			window.location = link;
		});
	}
	
	
	//ABOUT SECTION
	var timeline = $('timeline');
	if ($defined(timeline) && Browser.Plugins.Flash.version > 7) {
		var timelineSwf = new Swiff(BASE_URL+'assets/swf/about.swf?_version=<?php echo(time());?>', {
			width: 798,
			height: 200,
			params: {
				menu: false,
				bgcolor: "ffffff"
			},
			vars: {
				xml_url: BASE_URL+'assets/xml/dataAbout.xml?v=1',
				base_path: BASE_URL
			}
		});
		
		timeline.empty();
		timelineSwf.inject(timeline);
	}


	//FOOTER FLASH
	var footerFlash = $('footer').getElement('.flash');
	if ($defined(footerFlash)) {
		footerFlash.empty();

		var footSwf = new Swiff(BASE_URL+'assets/swf/footer.swf', {
			width: 958,
			height: 93,
			vars: {
				xml_url: BASE_URL+'xml/brands_footer.xml',
				base_path: BASE_URL
			}
		});

		footSwf.inject(footerFlash);
	}


	//NEWS SECTION
	if ($(document.body).get('id') === 'news-press') {
		var newsLists = $$('.newsList');
		newsLists.addClass('js');

		newsLists.each(function (newsList) {
			var inner = newsList.getElement('.inner');
			var slide = newsList.getElement('.slide');
			var slideWidth = 0;
			var slideFx = new Fx.Morph(slide, {duartion: 350, transition: Fx.Transitions.easeInOut});
			var items = newsList.getElements('.item');

			// Calculate slide width
			items.each(function (item) {
				slideWidth+= item.getSize().x+item.getStyle('margin-left').toInt()+item.getStyle('margin-right').toInt();
			});
			slide.setStyle('width', slideWidth);

			var pages = Math.ceil(items.length/4);
			var curPage = 1;


			// Setup controls
			var newer = new Element('a', {text: 'Newer', 'class': 'newer', href: 'javascript:void()'});
			newer.inject(inner, 'before');
			newer.addClass('hide');
			newer.addEvent('click', function (e) {
				e.stop();

				var newPage = curPage-1;

				if (newPage > 0) {
					if (newPage === 1) {
						newer.addClass('hide');
					} else {
						older.removeClass('hide');
						newer.removeClass('hide');
					}
					
					var itemCount = (newPage-1)*4;

					if (curPage === pages) {
						itemCount -= (pages % 4);
					}

					pos = -(itemCount*170);
					
					slideFx.start({
						left: pos
					});

					curPage = newPage;
				}
			});



			var older = new Element('a', {text: 'Older', 'class': 'older', href: 'javascript:void()'});
			older.inject(inner, 'after');
			older.addEvent('click', function (e) {
				e.stop();

				var pos = null;
				var newPage = curPage+1;
				
				if (newPage <= pages) {
					if (newPage === pages) {
						older.addClass('hide');
					} else {
						newer.removeClass('hide');
						older.removeClass('hide');
					}

					var itemCount = (newPage-1)*4;

					if (newPage === pages) {
						itemCount -= (pages % 4);
					}
					
					pos = -(itemCount*170);
					
					slideFx.start({
						left: pos
					});

					curPage = newPage;
				}
			});
		});
	}

	prepExternalLinks();

	if ($defined($('others'))) {
		var tree = $('others').getElements('ol');
		if($defined(tree)) {
			new Tree(tree);
		}
	}

	//time to implement basic show / hide
	Element.implement({
		//implement show
		show: function() {
			this.setStyle('display','');
		},
		//implement hide
		hide: function() {
			this.setStyle('display','none');
		}
	});
});//when the dom is ready...

