/* Logic for FS stores */

var activeStoreNum = 0;
var maCarte;
var maRue;
var myPano;   
var panoClient;
var nextPanoId;

function showStore() {
	if( $.url.param("id") ) {
		$("#snum-"+$.url.param("id")).addClass('active');
		matches = (/loadStoreInfo\((.+),'(.+)'/).exec($("#snum-"+$.url.param("id")).attr('onClick'));
		loadStoreInfo( matches[1], matches[2] );
	}
}

function showCareer() {
	if( $.url.param("id") ) {
		$("#snum-"+$.url.param("id")).addClass('active');
		matches = (/loadStoreCareers\((.+),'(.+)'/).exec($("#snum-"+$.url.param("id")).attr('onClick'));
		loadStoreCareers( matches[1], matches[2] );
	}
}

function loadStoreCareers( storeNum, province ) {
	if( storeNum > 0 ) {
		var html = '';
		with( stores[ province ][ storeNum ] ) {
			html+= '<table width="100%" border="0" cellspacing="5" cellpadding="0" id="locationTable">';
			html+=   '<tr>';
			html+=   '<td valign="top" class="address"><p><strong>'+ str[lang].address +'</strong></p>';
			html+=     '<p>';
			html+=       (name[lang]) ? (name[lang] +'<br/>'):'';
			html+=       street[lang] +'<br/>';
			html+=       city   +', ' + province +'<br/>';
			html+=       pc +'<br/>';
			html+=       (phone) ? (str[lang].tel+ phone ):'';
			html+=     '</p>';
			html+=     (remarks[lang]) ? (remarks[lang]):"";
			html+=   '</td>';
			html+=   '<td valign="top" class="storehours"><p><strong>'+ str[lang].store_hours +'</strong></p>';
			html+=     '<table border="0" cellspacing="0" cellpadding="0">';
				if( hours ) {
					var dayStart = "mon";
					for( var day in hours ) {
						if( hours[ day ] == hours[ dayStart ] ) {
							dayEnd = day;
							continue;
						}
						else {
							html+= '<tr><td>'+ (( dayStart == dayEnd ) ? (str[lang].abrvdays[ dayStart ]): ( str[lang].abrvdays[ dayStart ] +" - "+ str[lang].abrvdays[ dayEnd ] ) )+'</td><td>'+ hours[ dayStart ] +'</td></tr>';
							dayStart = dayEnd = day;
						}
					}
					html+= '<tr><td>'+ (( dayStart == dayEnd ) ? (str[lang].abrvdays[ dayStart ]): ( str[lang].abrvdays[ dayStart ] +" - "+ str[lang].abrvdays[ dayEnd ] ) )+'</td><td>'+ hours[ dayStart ] +'</td></tr>';
				}
				else
					html+= '<tr><td colspan="2">'+ str[lang].no_store_hours +'</td></tr>';

			html+=   '</table></td></tr>';
			html+=   '</table>';
		}

		$("#invite").css('display','none');
		$("#storeinfo").html( html );
		$("#careers").getPositions( lang, storePositions[ province ][ storeNum ], storeNum );
		$("#info").css('visibility', 'visible');
	}
}

function loadStoreInfo( storeNum, province ) {
	if( storeNum > 0 ) {
		var addr = 'Fitness+Source+-+';
		var html = '';
		
		with( ourStores[ province ][ storeNum ] ) {
			addr+= street +', '+ city +', '+ province +', '+ pc +((remarks.gmap.ll) ? ('@'+remarks.gmap.ll):"");
			addr = addr.replace(/ /g,"+");
			
			html+= '<table width="100%" border="0" cellspacing="5" cellpadding="0" id="locationTable">';
			html+=   '<tr>';
			html+=   '<td valign="top" class="address"><p><strong>'+ str[lang].address +'</strong></p>';
			html+=     '<p>';
			html+=       (name) ? name +'<br/>':'';
			html+=       street +'<br/>';
			html+=       city   +', ' + province +'<br/>';
			html+=       pc +'<br/>';
			html+=       (phone) ? (str[lang].tel+ phone ):'';
			html+=     '</p>';
			
			html+= ( !storePositions[ province ] ) ? "" : ( !storePositions[ province ][ storeNum ] ) ? "" : ( '<p class="links"><a href="' + str[lang].hrefPositions +"?id="+ storeNum +'">'+ str[lang].position + (( storePositions[ province ][ storeNum ].length > 1 ) ? "s":"") +'</a></p>' );
			
			html+=     (remarks) ? (remarks.msg):"";
			html+=   '</td>';
			html+=   '<td valign="top" class="storehours"><p><strong>'+ str[lang].store_hours +'</strong></p>';
			html+=     '<table border="0" cellspacing="0" cellpadding="0">';
				if( hours ) {
					var dayStart = "mon";
					for( var day in hours ) {
						if( hours[ day ] == hours[ dayStart ] ) {
							dayEnd = day;
							continue;
						}
						else {
							html+= '<tr><td>'+ (( dayStart == dayEnd ) ? (str[lang].abrvdays[ dayStart ]): ( str[lang].abrvdays[ dayStart ] +" - "+ str[lang].abrvdays[ dayEnd ] ) )+'</td><td>'+ hours[ dayStart ] +'</td></tr>';
							dayStart = dayEnd = day;
						}
					}
					html+= '<tr><td>'+ (( dayStart == dayEnd ) ? (str[lang].abrvdays[ dayStart ]): ( str[lang].abrvdays[ dayStart ] +" - "+ str[lang].abrvdays[ dayEnd ] ) )+'</td><td>'+ hours[ dayStart ] +'</td></tr>';
				}
				else
					html+= '<tr><td colspan="2">'+ str[lang].no_store_hours +'</td></tr>';

			html+=   '</table></td></tr>';
			html+=   '</table>';
			
		}
		
		$("#storeinfo").html( html );

		showAddress(addr, storeNum, province);
	}
}

function selectStore( storeNum ) {
	if( activeStoreNum > 0 )
		$('#snum-'+activeStoreNum).removeClass("active");
	
	activeStoreNum = storeNum;
	$('#snum-'+activeStoreNum).addClass("active");
}

//Pour afficher l'addresse lors du clic dans la liste des magasins
function showAddress(address, storeNum, province) {
	var maplink = 'http://maps.google.ca/maps?daddr=' + address;
	if(lang == "fr")
		pageTracker._trackPageview('/magasins.shtml?id='+storeNum);
	else
		pageTracker._trackPageview('/stores.shtml?id='+storeNum);
	
	selectStore( storeNum );
	$("#info").css('display','block');

	var contenuInfoBulle = '<div id="conteneurInfoBulle">' +
		'<h3>Fitness Source '+ ourStores[ province ][ storeNum ].name +'</h3>' +
		'<p>'+ ourStores[ province ][ storeNum ].street +'<br/>' +
		ourStores[ province ][ storeNum ].city +', '+ ourStores[ province ][ storeNum ].pc +'<br/>' +
		ourStores[ province ][ storeNum ].phone +'</p>' +
		'</div>';
	
	with(ourStores[ province ][ storeNum ].remarks) {
		var centreCarte = new google.maps.LatLng(gmap.lat, gmap.lng);
	    var infoBulle = new google.maps.InfoWindow({ content: contenuInfoBulle });
		var marqueurImage = new google.maps.MarkerImage('/img/UI/picts/fs_marker.png', new google.maps.Size(30,45), new google.maps.Point(0,0), new google.maps.Point(15,45));
		var marqueurOmbre = new google.maps.MarkerImage('/img/UI/picts/fs_marker_shadow.png', new google.maps.Size(45, 45), new google.maps.Point(0,0), new google.maps.Point(15, 45));
		var options = {
			carte : {
				center: centreCarte,
				zoom: 15,
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				streetViewControl: true
			},
			marqueur : {},
			panorama : {
				position: centreCarte,
				visible: false,
				pov: {
					heading: gstreet.yaw,
					pitch: gstreet.pitch,
					zoom: gstreet.zoom
				}
			}
		}
	}
	
	maCarte = new google.maps.Map(document.getElementById("map"), options.carte);
//	maRue   = new google.maps.StreetViewPanorama(document.getElementById("streetView"), options.panorama);
	maRue   = maCarte.getStreetView();
	maRue.setOptions(options.panorama);
	
	options.marqueur = {
		position: centreCarte,
		cursorRadius: 3,
		map: maCarte,
		title: "Fitness Source - " + ourStores[ province ][ storeNum ].name,
		icon: marqueurImage,
		shadow: marqueurOmbre
    }
	var marqueur = new google.maps.Marker(options.marqueur);
	google.maps.event.addListener(marqueur, 'click', function() {
		infoBulle.open(maCarte, marqueur);
	});

	$("#invite").css('display','none');
	$("#info").css('visibility', 'visible');
	$("#pop").html('<small><a href="' + maplink  +"&hl="+lang+"&ie=UTF8" + '" target="_blank">'+ str[lang].directions +'</a></small>');
}

/*
//Pour afficher le Street View du magasin
function showStreetView(address, storeNum, province) {
	with(ourStores[ province ][ storeNum ].remarks){
		if(gstreet.panoid) {
			var maplink = 'http://maps.google.ca/maps?ie=UTF8&amp;t=h&amp;hl='+lang+'&amp;layer=c&amp;daddr=' + address +'&amp;panoid='+ gstreet.panoid +'&amp;cbp='+ gstreet.cbp +'&amp;ll='+ gstreet.ll;
			var streetView = "";
			streetView += '<small><a href="#" onClick="getMyPOV(\''+ province +'\','+ storeNum +');">POV</a></small> ';
			streetView += '<small><a href="'+ maplink +'" target="_blank">'+ (lang == 'fr' ? 'Agrandir le plan':'Zoom View') +'</a></small><br/>';
			$("#myStreetView").html( streetView );
			var LL = gstreet.ll.split(",");
			var gLL = new GLatLng(LL[0],LL[1]);
			var myPOV = { yaw:gstreet.yaw, pitch:gstreet.pitch, zoom:gstreet.zoom };
			var panOptions = { latlng: gLL, pov: myPOV };

			myPano = new GStreetviewPanorama(document.getElementById("pano"));
			GEvent.addListener(myPano, "error", handleNoFlash);
var str  = '1 - GS - yaw:['+ panOptions.pov.yaw +'], pitch:['+ panOptions.pov.pitch +'] zoom:['+ panOptions.pov.zoom +'] - Pano yaw:['+ myPano.getPOV().yaw +'], pitch:['+ myPano.getPOV().pitch +'] zoom:['+ myPano.getPOV().zoom +']';
			myPano.setLocationAndPOV( gLL, myPOV );
    str += '<br/>2 - GS - yaw:['+ panOptions.pov.yaw +'], pitch:['+ panOptions.pov.pitch +'] zoom:['+ panOptions.pov.zoom +'] - Pano yaw:['+ myPano.getPOV().yaw +'], pitch:['+ myPano.getPOV().pitch +'] zoom:['+ myPano.getPOV().zoom +']';
			
			$('#popov').html( str );
	var myStr = 'center['+ maCarte.getCenter().toString() +'] ['+ maCarte.getStreetView().getPano() +']';
	$('#popov').html( myStr );
		}
		else {
			$("#myStreetView").html( "" );
		}
	}
}
*/			

function getMyPOV( province, storeNum ) {
	var p = myPano.getPOV();
	var l = myPano.getLatLng();
	with( ourStores[ province ][ storeNum ].remarks ) {
		$('#popov').html( 'yaw:['+ gstreet.yaw +'], pitch:['+ gstreet.pitch +'] zoom:['+ gstreet.zoom +']<br/>yaw:['+ p.yaw +'], pitch:['+ p.pitch +'] zoom:['+ p.zoom +']<br/>' );
//		$('#popov').html( 'msg:"'+ msg +'", gstreet:{ panoid:"'+ gstreet.panoid +'", cbp:"'+ gstreet.cbp +'", ll:"'+ l.lat() +','+ l.lng() +'", yaw:"'+ p.yaw +'", pitch:"'+ p.pitch +'", zoom:"'+ p.zoom +'" }, gmap:{ll:"'+ gmap.ll +'"}' );
	}
}

function handleNoFlash(errorCode) {
	if (errorCode == 603) {
		alert( (lang == "en" ? "Error: It appears that Flash is not supported by your browser":"Erreur : Flash semble inactif ou n'est pas pris en charge sur votre navigateur.") );
	}
	return;
}

