function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('#');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

var arg=getUrlVars();


var dd, val;
 // read the window hash if present
var hash = window.location.hash;
//var cId = (hash) ? hash.substring(11) : '72157625024323950'; // default for portfolio
var cId = (hash) ? hash.substring(11) : '72157626853666951'; // default for portfolio

// section = case_studies change the set#
if(arg['section'] == 'clients_and_work') cId = '72157624984117781';


	
$(document).ready(function (){ 		
	var options = new Object();
	options.select = select_handler;
	options.init = init;
	
	$("#category").mcDropdown("#categorymenu", options); 
	
	function init() {
		//alert('init');
		dd = $("#category").mcDropdown();
		
		//set the dropdown to the selected if the hash exists
		if(cId) dd.setValue(cId, true);
		
		// get the new value
		val = dd.getValue();
		update_data();
	}
	
	function select_handler() {
		// clear the results div
		$("#db-results").html('');
		$("#top-replace").html('');
		
		// get the new value
		val = dd.getValue();
		
		// change the gallery images
		flickr.getSet(val[0], update_gallery);
		
		// update the url string
		window.location.hash ='#client_id='+val[0];					
	}		
		
	function update_gallery(data) {
		// update gallery data
		$('#galleria').galleria({
			data_source: data							
		});
		
		update_data();
	}
	
	function update_data() {
		// set the case study data
		$.ajax({
					type : "GET",
					//  url :	"http://zeus.rapportinc.ca/clients/rapport_website/db_call.php",
					url : "http://www.rapportinc.ca/db_call.php",
					data : "client_id="+val[0],
					dataType : 'html',
					success: function (sdata) {
						//alert(sdata);
						var divs = $(sdata).filter(function(){ return $(this).is('div') });		
										$(divs[0]).appendTo('#top-replace');
										$(divs[1]).appendTo('#db-results');										
									}
		});
	}
});
