/**
 * native.js
 */

var mynative = {
	
	 videoloaded: false
	
	,init: function() {
		
		mynative.init_header();
		mynative.init_place_images();
		mynative.init_tarifftable();
		mynative.init_comparisontable();
		mynative.init_dynamic_flv_movies();
		mynative.init_newwindow_links();
		mynative.preload_thumbnail_large_images();
		mynative.init_specialised_slideshows();
		mynative.init_badges();
		
		var href = $('.offers-block').find('a:first').attr('href');
		if( href ) {
			$('.offers-block').css('cursor', 'pointer').click(function(e) {
				if( href.match('pdf') ) {
					window.open(href);
				} else {
					window.location = href;
				}
			});
		}
		
		$('.advert-blocks a:eq(0), .advert-blocks a:eq(1), .advert-blocks a:eq(3), .advert-blocks a:eq(4)').addClass('block');
		
		$('a.hoverme').hover(function() {
			var rel = $(this).attr('rel');
			var orig = $(this).find('img').attr('src');
			$(this).attr('rel', orig);
			$(this).find('img').attr('src', rel);
		}, function() {
			var orig = $(this).attr('rel');
			var rel = $(this).find('img').attr('src');
			$(this).attr('rel', rel);
			$(this).find('img').attr('src', orig);
		});
		
		$('.accommodation-page .tarifftable td, .accommodation-page .tarifftable th').not('.tablethleft, .highlighted-cell').css('opacity', 0.5);
		$('.accommodation-page .comparisonchart td').not('.highlighted-cell').css('opacity', 0.3);
		
		$("img[src*='feadonfarmlogo']").parent().hover(function() {
			$(this).find('img').attr('src', '/assets/images/feadonfarmlogo-over.png');
		}, function() {
			$(this).find('img').attr('src', '/assets/images/feadonfarmlogo.png');
		});
		
		$('.zone-white').parents('.images-zone-v2').addClass('zone-white');
		
	}
	
	,init_badges: function() {

		$("img[src*='flip_restaurant']").parent().hover(function() {
			$(this).find('img').attr('src', '/assets/images/badges/flip_restaurant_hover.png');
		}, function() {
			$(this).find('img').attr('src', '/assets/images/badges/flip_restaurant.png');
		});

		$("img[src*='restaurant_menu']").parent().hover(function() {
			$(this).find('img').attr('src', '/assets/images/badges/restaurant_menu_hover.png');
		}, function() {
			$(this).find('img').attr('src', '/assets/images/badges/restaurant_menu.png');
		});

		$("img[src*='treatment_menu']").parent().hover(function() {
			$(this).find('img').attr('src', '/assets/images/badges/treatment_menu_hover.png');
		}, function() {
			$(this).find('img').attr('src', '/assets/images/badges/treatment_menu.png');
		});

		$("img[src*='flip_treatment']").parent().hover(function() {
			$(this).find('img').attr('src', '/assets/images/badges/flip_treatment_hover.png');
		}, function() {
			$(this).find('img').attr('src', '/assets/images/badges/flip_treatment.png');
		});

		$("img[src*='download-range']").parent().hover(function() {
			$(this).find('img').attr('src', '/assets/images/cycling/download-range-hover.png');
		}, function() {
			$(this).find('img').attr('src', '/assets/images/cycling/download-range.png');
		});

		$("img[src*='download-faqs']").parent().hover(function() {
			$(this).find('img').attr('src', '/assets/images/cycling/download-faqs-hover.png');
		}, function() {
			$(this).find('img').attr('src', '/assets/images/cycling/download-faqs.png');
		});

		$("img[src*='download-info']").parent().hover(function() {
			$(this).find('img').attr('src', '/assets/images/cycling/download-info-hover.png');
		}, function() {
			$(this).find('img').attr('src', '/assets/images/cycling/download-info.png');
		});

		$("img[src*='button-routes']").parent().hover(function() {
			$(this).find('img').attr('src', '/assets/images/cycling/button-routes-hover.png');
		}, function() {
			$(this).find('img').attr('src', '/assets/images/cycling/button-routes.png');
		});

		$("img[src*='button-bikes']").parent().hover(function() {
			$(this).find('img').attr('src', '/assets/images/cycling/button-bikes-hover.png');
		}, function() {
			$(this).find('img').attr('src', '/assets/images/cycling/button-bikes.png');
		});

		$("img[src*='residence-brochure']").parent().hover(function() {
			$(this).find('img').attr('src', '/assets/images/badges/residence-brochure-hover.png');
		}, function() {
			$(this).find('img').attr('src', '/assets/images/badges/residence-brochure.png');
		});

		$("img[src*='residence-website']").parent().hover(function() {
			$(this).find('img').attr('src', '/assets/images/badges/residence-website-hover.png');
		}, function() {
			$(this).find('img').attr('src', '/assets/images/badges/residence-website.png');
		});

	}
	
	,init_newwindow_links: function() {
		
		$("a._blank, .navigation a[href^='http']").click(function() {
			
			window.open( $(this).attr('href') );
			return false;
			
		});
		
	}
	
	,preload_thumbnail_large_images: function() {
		
		var cache = [];
		var args_len = $('.thumbnails a').length;
		
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = $('.thumbnails a').eq(i).attr('href');
			cache.push(cacheImage);
		}
		
	}
	
	,init_header: function() {
		
		if( is_mobile != 1 ) {
			setTimeout(function() {
				// flashvars = { 'xmlFile' : '/assets/swf/header/images.xml?' + randomString() };
				flashvars = { 'xmlFile' : '/assets/swf/header/images.xml?url=' + window.location.pathname };
				params    = { 'wmode' : 'transparent' };

				swfobject.embedSWF("/assets/swf/header/header.swf", "header-graphic", "960", "403", "9.0.0", "/assets/js/swfobject/expressInstall.swf", flashvars, params);
			}, 2000);
		}
		
	}
	
	,init_place_images: function() {
		
		$('.thumbnails a').mouseover(function() {
			if( mynative.videoloaded == true ) {
				return false;
			}
			
			var bigimage = $(this).attr('href');
			
			if( !$('#main-image img').attr('src') ) {
				$('#main-image').html('<img src="' + bigimage + '" alt="" />');
			}
			
			$('#main-image img').fadeOut('fast', function() {
				$(this).attr('src', bigimage).load(function() {
					$(this).fadeIn('fast');
				});
			});
			
			return false;
		}).click(function() {
			
			if( mynative.videoloaded == true ) {
				mynative.videoloaded = false;
				
				var bigimage = $(this).attr('href');
				
				if( !$('#main-image img').attr('src') ) {
					$('#main-image').html('<img src="' + bigimage + '" alt="" />');
				}
				
				$('#main-image img').fadeOut('fast', function() {
					$(this).attr('src', bigimage).load(function() {
						$(this).fadeIn('fast');
					});
				});
			}

			return false;
			
		});
		
	}
	
	,init_tarifftable: function() {
		
		$('.tarifftable tr:odd').addClass('alt');
		
	}
	
	,init_comparisontable: function() {
		
		$('.comparisonchart table tbody tr:odd').addClass('alt');
		
		$('.comparisonchart tbody td').each(function() {
			if( $(this).html() == 'Y' ) {
				$(this).html('<img src="/assets/images/dot.png" alt="Yes" />');
			}
		});
		  
	}
	
	,init_dynamic_flv_movies: function() {
		
		$('.showvideo').click(function() {
			var streamName = $(this).attr('rel');
			if(streamName) {
				$('#main-image img').fadeOut('fast', function() {
					$('#main-image').load('/ajax/dynamicflvloader.php?streamName='+streamName);
					mynative.videoloaded = true;
				});
			}
			return false;
		});
		
		$('.showvideo img').hover(function() {
			$(this).attr('src', '/assets/images/playvideo_over.png');
		}, function() {
			$(this).attr('src', '/assets/images/playvideo.png');
		});
		
		//$('.showvideo').trigger('click');
		
	}
	
	,start_iframe_booking_tracking: function() {
		
		$('#myiframe').after('<div id="debugoutput" style="font-size:10px;" />');
		$('#myiframe').load(function() {
			//var src = document.getElementById('myiframe').contentWindow.location.href;
			//$('#debugoutput').append('<p style="margin:0; border-bottom:1px solid black;">' + src + '</p>');
		});
		
	}
	
	,init_specialised_slideshows: function() {
		
		$('.slideshow img:last').addClass('active');
		$(window).load(function() {
			setInterval( "mynative.specialised_slideshows_cycle_images()", 3000 );
		});
		
	}

	,specialised_slideshows_cycle_images: function() {
		var $active = $('.slideshow img.active');

		if ( $active.length == 0 ) {
			$active = $('.slideshow img:last');
		}
		
		var $next =  $active.next().length ? $active.next() : $('.slideshow img:first');
		
		$active.addClass('last-active');
		
		$next.css({opacity: 0.0})
			 .addClass('active')
			 .animate({opacity: 1.0}, 2000, function() {
			     $active.removeClass('active last-active');
			 });

	}

};


$(document).ready(mynative.init);


function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 8;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

