/*
 * SimpleModal Basic Modal Dialog
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2009 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: basic.js 212 2009-09-03 05:33:44Z emartin24 $
 *
 */

$(document).ready(function () {

	if ($("a").hasClass('basic'))
	{
		popup_init('a.basic');
	}
	if ($("a").hasClass('deal'))
	{
		popup_initDeal('a.deal');
	}
});

function popup_initDeal(domChunk){
	$('#basic-deal input.deal, #basic-deal a.deal').click(function (e) {
		e.preventDefault();
		$('#basic-deal-content').modal();
		if(this.rel)
		{
			var url = this.rel;
			popup_show(url);
			this.blur();
			return false;
		}
	});
}

function popup_init(domChunk){
	$('#basic-modal input.basic, #basic-modal a.basic').click(function (e) {
		e.preventDefault();
		$('#basic-modal-content').modal();
		if(this.rel)
		{
			var url = this.rel;
			popup_show(url);
			this.blur();
			return false;
		}
	});
}

function popup_show(url)
{
	var baseURL;
	if(url.indexOf("?")!==-1)//if there is a query string involved
	{
		baseURL = url.substr(0, url.indexOf("?"));
	}
	else
	{ 
		baseURL = url;
	}

	var queryString = url.replace(/^[^\?]+\??/,'');
	var params = popup_parseQuery( queryString );

	if(params['inlineId']=='click2call')
	{ 
		document.getElementById('vendorId').value=params['vendorId'];
		document.getElementById('merchant').value=params['merchantNumber'];
	}
	else if(params['store_id'])
	{
		document.getElementById('categoryList').value=params['categoryIds'];
		document.getElementById('stores_ids').value=params['store_id'];
		document.getElementById('selected_stores').value=params['store_name'];
	}

}

function popup_parseQuery ( query ) 
{
    var Params = {};
    if ( ! query ) {return Params;}// return empty object
    var Pairs = query.split(/[;&]/);
    for ( var i = 0; i < Pairs.length; i++ ) 
	{
		  var KeyVal = Pairs[i].split('=');
		  if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
		  var key = unescape( KeyVal[0] );
		  var val = unescape( KeyVal[1] );
		  val = val.replace(/\+/g, ' ');
		  Params[key] = val;
    }
    return Params;
}


function dynamic_data(ids,divid,cat)
{
	for( var i in vendors)
	{ 
		if(vendors[i]['id']==ids)
		{
			if(divid=='setdeal')
			{
			//document.getElementById('deal_verify').innnerHTML='';
				if(document.getElementById('categoryList').value=='')
				{
					document.getElementById('categoryList').value=cat;
				}
				else
				{
					var cat=document.getElementById('categoryList').value;
				}
					//document.getElementById('deal_verify').style.display='none';
				
				//document.getElementById('storeid').value=cat+'_'+vendors[i]['id']+'_'+vendors[i]['name'];
				document.getElementById('selected_stores').value=vendors[i]['name'];
				document.getElementById('stores_ids').value=ids;
			}
		}
	}
}

