var intId;
$(document).ready(function(){
	
	intId = setInterval( "slideSwitch()", 5000 );
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		padding: 40, /* padding for each side of the picture */
		opacity: 0.35, /* Value betwee 0 and 1 */
		showTitle: true, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'light_square' /* light_rounded / dark_rounded / light_square / dark_square */
	 });
	if ($('#map_holder').length){
		initializeContactMap();
	}
});

function slideSwitch() {
	var $active = $('#ve-content-slideshow DIV.active');
	
	if ( $active.length == 0 ) $active = $('#ve-content-slideshow DIV:last');
	
	// use this to pull the divs in the order they appear in the markup
	var $next = $active.next().length ? $active.next() : $('#ve-content-slideshow DIV:first');

	$active.addClass('last-active');

	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active');
	});
}

function RubyMsXMLLoader(){
	var resultParser;
}

RubyMsXMLLoader.prototype.loadXML = function(xmlFile, resultParser){
	this.resultParser = resultParser;
	$.ajax({
	    type: "GET",
	    url: xmlFile + "?cId" + (new Date()).getTime(),
	    dataType: "xml",
	    success: resultParser
	});	 
}

var ruby_ms_loader = new RubyMsXMLLoader();

function golfImagesRenderer(xml){
	var i = 0;
	var photoDiv;
	$(xml).find("photoItem").each(function()
	{
		i++;
		photoDiv = EL('div', {'class':'photo_item' + ((i % 4 == 0) ? ' last' : '')}, 
						EL('a', {title: $(this).find('title').text(), rel: 'prettyPhoto[golf_photo]', href: $(this).find('largeImg').text()}, 
							EL('img', {src: $(this).find('thumbImg').text(), alt: $(this).find('title').text() + " " + $(this).find('subtitle').text()})),
						EL('div', {'class':'clear'}),
						EL('strong', null, TEXT($(this).find('title').text())),
						EL('br', null),
						TEXT($(this).find('subtitle').text())
				   );
		$("#photos-tab").append(photoDiv);
		if (i % 4 == 0){
			$("#photos-tab").append(EL('div', {'class':'clear'}));
		}
	});
	enablePrettyPhoto();
}

function golfVideosRenderer(xml){
	//create video item for every photoItem element
	var i = 0;
	var photoDiv;
	$(xml).find("videoItem").each(function()
	{
		i++;
		photoDiv = EL('div', {'class':'video_item' + ((i % 3 == 0) ? ' last' : '')}, 
				 		EL('a', {title: $(this).find('title').text(), rel: 'prettyPhoto[golf_video]', href: $(this).find('videoUrl').text()}, 
				 			EL('img', {src: $(this).find('thumbImg').text(), alt: $(this).find('title').text() + " " + $(this).find('subtitle').text()})),
				 		EL('div', {'class':'clear'}),
				 		EL('strong', null, TEXT($(this).find('title').text())),
				 		EL('br', null),
				 		TEXT($(this).find('subtitle').text())
				   );
		$("#videos-tab").append(photoDiv);
		if (i % 3 == 0){
			$("#videos-tab").append(EL('div', {'class':'clear'}));
		}
	});
	enablePrettyPhoto();
}

function eventsRenderer(xml){
	
	var voucherItem;
	var voucherBody;
	var sportLabel;
	var i = 0;
	$(xml).find("events").each(function()
	{
		voucherItem = EL('div', {'class':'side_item event_voucher_item'});
		voucherItem.appendChild(EL('div', {'class':'voucher_top'}, EL('img', {src: 'images/sport_'+$(this).attr('sportId')+'.png'})));
		i = 0;
		sportLabel = getSportLabel($(this).attr('sportId'));
		voucherBody = EL('div', {'class': 'voucher_body'});
		voucherBody.appendChild(EL('h1', null, TEXT(sportLabel)));
		$(this).find("event").each(function()
		{
			if (i > 0){
				voucherBody.appendChild(EL('div', {'class':'separator'}));
			}
			voucherBody.appendChild(EL('div', null, 
										EL('strong', null, TEXT($(this).find('date').text())),
										EL('br', null),
										TEXT($(this).find("description").text())
									  )
							   );
			i++;
		});
		voucherItem.appendChild(voucherBody);
		voucherItem.appendChild(EL('div', {'class':'voucher_bottom'}));
		$("#events_holder").append(voucherItem);
	});
	
	sIFR.replace(rockwell, {
	    selector: 'h1'
	    ,css: [
	      '.sIFR-root {font-size:18px; text-align: left; font-weight: bold; background-color: #6D0B1A;color: #FFF2AF}'
	    ]
	  });
}
function renderVoucher(xml, sportLabel){
	var elem = EL('a', {href: ''}, 
				EL('h1', null, TEXT(sportLabel)),
				EL('strong', null, TEXT($(xml).find('date').text())),
				EL('br', null),
				TEXT($(xml).find("description").text())
				)
	return elem;
}

function enablePrettyPhoto(){
	// apply prettyPhoto
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		padding: 40, /* padding for each side of the picture */
		opacity: 0.35, /* Value betwee 0 and 1 */
		showTitle: true, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'light_square' /* light_rounded / dark_rounded / light_square / dark_square */
	 });
}

function openNiceUrl(url, title, description){
	$.prettyPhoto.open(url, title, description);
}

function TEXT(str){
    return document.createTextNode(str);
}

EL = ( function() {
	var CUSTOM_ATTRIBUTES = (!window.document.documentElement.hasAttribute) ? {
		'for' :'htmlFor',
		'class' :'className'
	} : { // w3c
		'htmlFor' :'for',
		'className' :'class'
	};

	return function EL(type, attributes) {
		var node = document.createElement(type);
		for ( var i = 2; i < arguments.length; i++) {
			var child = arguments[i];
			if (child) {
				if (typeof (child) == 'string') {
					node.appendChild(TEXT(child));
				} else {
					node.appendChild(child);
				}
			}
		}
		if (attributes)
			for ( var attr in attributes)
				if (attributes.hasOwnProperty(attr)) {
					if (attr.slice(0, 2) == 'on') {
						if (window.addEventListener) {
							node.addEventListener(attr.slice(2),
									attributes[attr], false);
						} else {
							node.attachEvent(attr, function() {
								attributes[attr](window.event)
							});
						}
					} else {
						setattr = CUSTOM_ATTRIBUTES[attr] || attr;
						node.setAttribute(setattr, attributes[attr]);
					}
				}
		return node;
	}
})()

function toggle(parent, list){
	if ($(list).css("display") == "none"){
		$(list).css("display", "block");
		$(parent).addClass("strong");
	}else{ 
		$(list).css("display", "none");
		$(parent).removeClass("strong");
	 }
} 

var left_nav_selected_item = "golf_locations";

function changePanels(menuItem, section){
	$("#" + left_nav_selected_item).removeClass('selected');
	$("#" + menuItem).addClass('selected');
	left_nav_selected_item = menuItem;
	
	var centerContent;
	var	rightContent;
	
	switch(section)
	{
		case 'golf_locations': { 
			centerContent = "golf_locations_center.html"; 
			rightContent = "golf_location_right.html";
			break;
		}
		case 'golf_leaderboard': {
			centerContent = "golf_leaderboard_center.html"; 
			rightContent = "golf_leaderboard_right.html";
			break;
		}
		case 'golf_media': {
			centerContent = "golf_media_center.html"; 
			rightContent = "golf_media_right.html";
			break;
		}
		case 'football_interests': {
			centerContent = "football_interests_center.html";
			rightContent = "football_interests_right.html";
			break;
		}
		default: 
	}
	$('#center_panel').load(centerContent);
	$('#right_panel').load(rightContent);
}

var rockwell = {
  src: 'rockwell.swf'
  ,ratios: [6,1.41,9,1.35,15,1.29,21,1.25,22,1.22,27,1.24,29,1.21,34,1.22,41,1.21,45,1.2,46,1.21,59,1.2,68,1.19,69,1.2,96,1.19,97,1.18,102,1.19,103,1.18,107,1.19,108,1.18,112,1.19,114,1.18,116,1.19,120,1.18,121,1.19,1.18]
};

sIFR.useStyleCheck = true;
sIFR.activate(rockwell);

sIFR.replace(rockwell, {
    selector: 'h1'
    ,css: [
      '.sIFR-root {font-size:18px; text-align: left; font-weight: bold; background-color: #6D0B1A;color: #FFF2AF}'
    ]
  });
sIFR.replace(rockwell, {
    selector: 'h2'
    ,css: [
      '.sIFR-root {font-size:16px; font-weight: bold; background-color: #6D0B1A;color: #FFF2AF}'
    ]
  });
sIFR.replace(rockwell, {
    selector: '.quote label'
    ,css: [
      '.sIFR-root {font-size:13px;text-align: center; leading: -2; background-color: #2E050D;color: #FFF2AF}'
    ]
  });
sIFR.replace(rockwell, {
    selector: '.occ_column h3'
    ,css: [
      '.sIFR-root {font-size:14px;text-align: left; font-weight: bold; background-color: #2E050D;color: #FFF2AF}'
    ]
  });
sIFR.replace(rockwell, {
    selector: '.header span'
    ,css: [
      '.sIFR-root {font-size: 17px;text-align: center;background-color: #2E050D;color: #FFF2AF;}'
    ]
  });
sIFR.replace(rockwell, {
    selector: '.title'
    ,css: [
      '.sIFR-root {font-size: 18px;text-align: center;background-color: #2E050D;color: #FFF2AF;}'
    ]
  });

function activateMenuVisuals(){
	sIFR.replace(rockwell, {
	    selector: '.menu_holder_top span'
	    ,css: [
	      '.sIFR-root {font-size: 18px;font-weight: bold;text-align: center;background-color: #510813;color: #FFF2AF;}'
	    ]
	  });
	sIFR.replace(rockwell, {
	    selector: '.menu_contact_phone span'
	    ,css: [
	      '.sIFR-root {font-size: 22px;font-weight: bold;text-align: center;background-color: #510813;color: #FFF2AF;}'
	    ]
	  });
	sIFR.replace(rockwell, {
	    selector: '.menu_working_hours label, .menu_contact_address span'
	    ,css: [
	      '.sIFR-root {font-size: 14px;font-weight: bold;text-align: center;background-color: #510813;color: #FFF2AF;}'
	    ]
	  });
	sIFR.replace(rockwell, {
	    selector: '.menu_working_hours span'
	    ,css: [
	      '.sIFR-root {font-size: 12px;text-align: center;background-color: #510813;color: #FFF2AF;}'
	    ]
	  });

	if (window.XMLHttpRequest) $(".dish div").animate({ opacity: 0.5}, 1);
	
	var chineseDishesIntId = setInterval("slideChineseDishes()", 4000);
	enableIndDishSlider = setInterval("enableIndianDishes()", 2000 );
}

var enableIndDishSlider;

function enableIndianDishes(){
	clearInterval(enableIndDishSlider);
	var indianDishesIntId = setInterval("slideIndianDishes()", 4000 );
}

function slideChineseDishes() {
	var $active = $('#chinese-dishes-slideshow DIV.active');
	if ( $active.length == 0 ) $active = $('#chinese-dishes-slideshow DIV:last');
	
	// use this to pull the divs in the order they appear in the markup
	var $next = $active.next().length ? $active.next() : $('#chinese-dishes-slideshow DIV:first');

	$active.addClass('last-active');

	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active');
	});
}
function slideIndianDishes() {
	var $active = $('#indian-dishes-slideshow DIV.active');
	if ( $active.length == 0 ) $active = $('#indian-dishes-slideshow DIV:last');
	
	// use this to pull the divs in the order they appear in the markup
	var $next = $active.next().length ? $active.next() : $('#indian-dishes-slideshow DIV:first');

	$active.addClass('last-active');

	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active');
	});
}

function openVoucher(voucherId){
	window.open("vouchers/voucher-" + voucherId+ ".html", "voucherPopup", "height=670, width=480, top=" + ((screen.height - 670) / 2) + ",screenY="+((screen.height - 670) / 2)+",left=" + ((screen.width - 480) / 2) + ", screenX="+((screen.width - 480) / 2)+",scrollbars=no, status=no,resizable=no,menubar=no,toolbar=no,titlebar=yes");
}

function openChineseMenu(){
	var images = ['images/menu/chinese/menuChinesePg1.gif', 'images/menu/chinese/menuChinesePg2.gif'];
	var titles = ['Chinese menu', 'Chinese menu'];
	var descriptions = ['Page 1', 'Page 2'];
	
	$.prettyPhoto.open(images, titles, descriptions);
}

function openIndianMenu(){
	var images = ['images/menu/indian/menuIndianPg1.gif', 'images/menu/indian/menuIndianPg2.gif'];
	var titles = ['Indian Menu', 'Indian Menu'];
	var descriptions = ['Page 1', 'Page 2'];
	
	$.prettyPhoto.open(images, titles, descriptions);
}

function initializeContactMap(){
    if (GBrowserIsCompatible()) {
      var map = new GMap2(document.getElementById("map_holder"));
      map.addControl(new GSmallMapControl());
      map.setCenter(new GLatLng(51.574429, -0.370381), 16);

      var tinyIcon = new GIcon();
      tinyIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
      tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
      tinyIcon.iconSize = new GSize(12, 20);
      tinyIcon.shadowSize = new GSize(22, 20);
      tinyIcon.iconAnchor = new GPoint(6, 20);
      tinyIcon.infoWindowAnchor = new GPoint(5, 1);
      
      // Set up our GMarkerOptions object literal
      markerOptions = { icon:tinyIcon };
      
      var point = new GLatLng(51.574322,-0.370381);   
      map.addOverlay(new GMarker(point, markerOptions));
    }
}

function getSportLabel(id){
	var sportLabel;
	switch(id){
		case '1': sportLabel = 'Cricket'; break;
		case '2': sportLabel = 'Football'; break;
		case '3': sportLabel = 'Golf'; break;
		default: sportLabel = 'Golf';
	}
	return sportLabel;
}


