// JavaScript Document
// Read a page's GET URL variables and return them as an associative array.
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;
}
//if a video is in the URL
var videoURL=getUrlVars()["video"];
function closePopUp(openFirst) {
	$('#blackBack').height($(document).height());
	$('#popContainer').fadeToggle(500);
	$('#blackBack').fadeToggle(500);
	//check if html 5 video is supported, if so, pause before removing
	var v = document.createElement("video");
	if(v.play) {
		if($('#videoPlayer').attr('id') && openFirst!='open') {
			$('#videoPlayer').get(0).pause();
		}
		
	}
	//if not an open, remove content
	if(openFirst!='open') {
		$('#blackBack').remove();
		$('#popContainer').remove();
	}
	//enable the close buttons
	$('#closeX, #popClose').click(function() {
		closePopUp();					
	});
}
$(document).ready(function() {
	//auto generates the base path, no local play
	var relURL=window.location.href.split('peninsularegent.com/');
	var basePath='';
	for(i=1; i<relURL[1].split('/').length; i++) {
		basePath+='../';
	}
	//if URL loaded, then enable close buttons
	if(videoURL){
		$('#closeX, #popClose').click(function() {
			closePopUp();					
		});
	}
	$('#midContent .videoLink, #midContent .videoNoStyle, #leftNav .videoLink').click(function() {
		//get the links href
		var href=$(this).children('a').attr('href');
		if(!href) {var href=$(this).attr('href');}
		//determines what the href is
		var vars = [], hash;
		var hashes = href.slice(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];
		}
		//loads the video popup
		var addPop = $.ajax({
			type: "POST",
			url: "http://peninsularegent.com/scripts/ajax.videoPop.php",
			data: 'display=yes&video='+vars["video"],
			async: false,
			success: function(data) {
				//when loaded, add to the DOM, enable click functions
				$('body').append(data);
				$('#closeX, #popClose').click(function() {
					closePopUp();					
				});
				//show it
				closePopUp('open');
			}
		}).responseText;
		//no click if JS is enabled
		return false;
	});
	$('#moreQuestions').click(function() {//contact page survey
		$('#survey').fadeToggle(750);
	});
	$('#buyersExtra').click(function() {//contact page survey
		$('#buyersGuideExtra').fadeToggle(750);
	});
	$('#eventsExtra').click(function() {//contact page survey
		$('#eventsExtraBox').fadeToggle(750);
	});
});
