//Site-specific JavaScript code goes in the SLE (Summit Live Events) namespace
//Summit Live Events namespace
var SLE = window.SLE || {
	Speakers : {},
	UI : {}
};

SLE.UI.config = function(){
	$('#menu-main-nav').superfish();
	$('#accordion').accordion({
    	autoHeight: false,
		navigation: true
    });
    $('.tabs').tabs();
    	// light box script
    	
    if(!$.browser.msie) {
		$('.nyroModal').nyroModal();
	} else if(parseInt($.browser.version, 10) != 6) {
		$('.nyroModal').nyroModal();
	} 
	
	$("body").delegate("a[href^='http']", "click", function(){
		var path = $(this).attr('href'),
		
		// IMPORTANT!!!
		// change this regex to specify which domains DO NOT open in a new window
		sameWindow = /miningindaba\.com|mining12\.mapyourshow\.com/gi; 
		
		if(path.match(sameWindow)){
			return;	
		}
		else{
			$(this).attr("target", "_blank");
		}
    });
	
}

SLE.UI.agenda = function(linkColor){
	var rows = $('tbody tr'),
    	tips = $('.tip');
	
	// hides the session description and speaker name
	tips.hide();
	
	// makes the session name clickable and sets style
	// toggles the description on click
	rows.each(function(i){
	    if($(this).has(tips).length > 0) {
		    $(this).find('.sessionName').css({
			'color' : linkColor,
			'cursor' : 'pointer',
			'text-decoration' : 'underline'
		}).click(function(){
		        $(this).next('.tip').toggle("350");
		    });
	    }
	});
	// controls the filtering for the event categories
	// appends instructions and a reset link to show all
	var legend = $('#key');
	var instructions = $('<strong/>', {
		text : 'Filter by: '
	}).prependTo(legend);
	var reset = $('<a/>', {
		text : 'Clear Filter'
		}).css({
			'color' : linkColor,
			'cursor' : 'pointer',
			'text-decoration' : 'underline'
		}).appendTo(legend).click(function(){
			$(rows).show();
	});
	legend.find('span')
		.css({
			'color' : linkColor,
			'cursor' : 'pointer',
			'text-decoration' : 'underline'
		})
	    .click(function(e){
			$(rows).show();
			var clicked = $(this).attr('class');
			$(rows).not('.' + clicked).hide();
		});
	$('#collapse-all').click(function(){
		tips.hide();
	});
	$('#show-all').click(function(){
		tips.show();
	});	
}

SLE.Speakers.filter = function(){
	var speakerContainer = $('#speaker-last-name'),
		speakerList = $('#speakers-alpha'),
		speakers = $('.speaker'),
		removeFilter = $('#clear-filter');
	
	removeFilter.button({
		icons : {
			primary : 'ui-icon-close'
		}
	}).hide();
	
	speakerContainer.autocomplete({
		minLength: 2,
		source: SLE.Speakers.nameList,
		select: function(event, ui){
			speakers.hide();
			$("." + ui.item.value).show();
			speakerList.addClass('filtered');
			removeFilter.show();
		}
	});
	
	removeFilter.click(function(e){
		removeFilter.hide();
		speakers.show();
		speakerContainer.val('');
		speakerList.removeClass('filtered');
	});
}

SLE.initializeMap = function(mapContainer) {
	var myOptions = {
	  zoom: 9,
	  center: new google.maps.LatLng(-33.966575,18.594383),
	  mapTypeId: google.maps.MapTypeId.HYBRID
	}

	var map = new google.maps.Map(mapContainer, myOptions);	
	return map;
}

SLE.infowindow = new google.maps.InfoWindow();
SLE.closeInfoWindow = function() {
	SLE.infowindow.close();
};

SLE.places = {
	"results": {
		"result": [
			{
				"desc": "",
				"title": "Cape Town International Convention Centre (CTICC)",
				"address": "1 Lower Long Street, Cape Town 8001, South Africa",
				"latitude" : "-33.91546110124891",
				"longitude" : "18.427140712738037"
			},
			{
				"desc": "",
				"title": "Vergelegen Wine Estate",
				"address" : "Lourensford Road, Somerset West, Cape 7130",
				"latitude" : "-34.076523269133574",
				"longitude" : "18.8899827003479"
			},
			{
				"desc": "",
				"title": "The Westin Grand Cape Town Arabella Quays",
				"address" : "Coen Steytler Avenue Foreshore, Cape Town 8001",
				"latitude" : "-33.91526522510934",
				"longitude" : "18.426368236541748"
			},
			{
				"desc": "",
				"title": "Cape Town International Airport",
				"address": "Private Bag X9002, Cape Town 7525",
				"latitude" : "-33.966575",
				"longitude" : "18.594383"
			},
			{
				"desc": "",
				"title": "Arabella Golf Club",
				"address": "R44 Road, Kleinmond 7195",
				"latitude" : "-34.34045",
				"longitude" : "19.02612"
			}
		 ]
	}
}

$(document).ready(function() { 
	var SpeakerBios = new ExpandCollapse($('.speaker h4'), '.speaker-bio');
	SLE.UI.config();
	SLE.UI.agenda('#1849AA');
	SLE.Speakers.filter();
	
    imagePreview();
});

$(window).load(function(){
	if($('#map').length){
		var mapInstance = SLE.initializeMap(document.getElementById('map'));
		$(SLE.places.results.result).each(function(){
			var latitude	= 	this.latitude,
				longitude	= 	this.longitude,
				that 		= 	this, //bind an instance of this pointer so it can be used in the event listener to handle the click event
				position 	= 	new google.maps.LatLng(latitude, longitude),
				// create individual markers
				marker  = new google.maps.Marker({
					position: position,
					map: mapInstance
				});
			google.maps.event.addListener(marker, 'click', function() {
				SLE.infowindow.setContent('<div class="map"><h3>' + that.title + '</h3>' + '<p>' + that.address + '</p></div>');
				SLE.infowindow.open(mapInstance,marker);
			});
		});
	}
});
