var itemid = 0;
jQuery.noConflict();
jQuery(document).ready(function ()
{
	//Navbar menu
	jQuery('.desktopNavbar ul > li > ul').css
	(
		{ display: "none", left: "auto" }
	);

	if (document.location.href.toLowerCase().indexOf('press-releases') > -1)
	{
		//We have a press release child.  highlight press releases.  This is a temporary fix that should be more robust
		//Perhaps it should be handled server side.
		jQuery('.desktopNavbar a[href="/vehicle-inspections/press-releases"]').addClass('currentpage');
	}

	//highlight the parents of the highlighted element
	jQuery('.desktopNavbar a.currentpage').parents('li').find('a').addClass('currentpage');

	//ensure no body scroll if we are the ebay lightbox
	if (document.location.href.toLowerCase().indexOf('ebay%20lightbox') > -1)
	{
		//jQuery(document).attr('scroll', 'no');
	}

	


	jQuery('.desktopNavbar > ul > li').hover
	(
		function ()
		{
			jQuery(this)
			.find('> ul')
			.stop(true, true)
			.slideDown('fast');
		},
		function ()
		{
			jQuery(this)
			.find('> ul')
			.stop(true, true)
			.hide();
		}
	);

	jQuery('.desktopNavbar > ul > li > ul li').hover
	(
		function ()
		{
			jQuery(this)
			.find('> ul')
			.stop(true, true)
			.show();
		},
		function ()
		{
			jQuery(this)
			.find('> ul')
			.stop(true, true)
			.hide();
		}
	);
	//End navbar menu


	jQuery(".open-dialog-new").click(function ()
	{
		//jQuery('#dialog-frame').attr("src", jQuery(this).attr('href'));
		jQuery("#ui-dialog-title-dialog-window").html(jQuery(this).attr('title'));

		//   Changed

		var objIframe = document.getElementById("dialog-frame");
		objIframe.src = jQuery(this).attr('href');
		//		

		checkButtons(jQuery(this), 'simple');

		if (jQuery("#video-container").is(":visible"))
		{
			jQuery('#video-container').slideUp('fast', function ()
			{
				jQuery('.second-box').slideDown('fast');
			});
		}

		jQuery("#dialog-window").dialog('open');
		return false;
	});


	var h = jQuery(window).height() - 100;

	jQuery("#dialog-window").dialog({
		autoOpen: false,
		modal: true,
		resizeable: false,
		//height: h,
		//width: 812
		height: 393,
		width: 613 //Added by behrreich
	});

	jQuery('#video-container').hide();

	jQuery('.open-video').click(function ()
	{
		jQuery('.second-box').slideUp(function ()
		{
			jQuery('#video-container').slideDown();
			// jQuery('#video-frame').src = "http://link.delvenetworks.com/media/?mediaId=29a882b3199a4557a3afef6aafb13244&amp;width=593&amp;height=361&amp;playerForm=7bf00fb0551d462b94066f94f3eb3aa2";
		});
		return false;
	});

	jQuery('.close-video').click(function ()
	{
		jQuery('#video-container').slideUp('fast', function ()
		{
			jQuery('.second-box').slideDown('fast');
		});
		return false;
	});

	jQuery(".btn-print-information").click(function ()
	{
		jQuery(".printable").print();
		return false;
	});

	//payment option page form submittal - behrreich
	if (document.location.href.toLowerCase().indexOf("payment-option") > -1 || document.location.href.toLowerCase().indexOf("test") > -1)
	{

		//Add jquery handler to form submit
		jQuery('#mainform').submit
		(
			function (e)
			{
				//Set the form action
				jQuery(this).attr('action', PayPalUrl);

				//See if we have checked the terms
				if (jQuery('#terms').attr('checked') == true)
				{
					//Hide buttons and show a message
					jQuery('#btn-continue').hide();
					jQuery('#btn-back').hide();
					jQuery('#errormsg').hide();
					jQuery('#paypalmsg').show();

					//Make an ajax call to kill the session
					var Url = ServiceUrl;
					jQuery.ajax
					(
						{ type: "get", url: Url, async: false,
							success: function (data)
							{
								console.log('success');
							}
						}
					);
				}
				else
				{
					jQuery('#errormsg').show();
					e.preventDefault();
				}


				//e.preventDefault();
			}
		)
	}

	//payment option page form submittal - behrreich
	if (document.location.href.toLowerCase().indexOf("ebay") > -1)
	{
		//logo placement for ie7
		if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 7)
		{
			jQuery('div.logos').css({ "position": "relative", "top": "-76px" });
			jQuery('div.back-listing').css({ "position": "relative", "top": "-56px" });
		}
	}

	/*
	//mouse tracking 
	var op = "no";
	jQuery(document).mousemove
	(
	function(e) 
	{
	if(e.pageY <= 5)
	{
	if(op == "no")
	{
	jQuery('#dialog-frame').attr("src", "http://www.google.com");
	jQuery("#ui-dialog-title-dialog-window").html("Test Lightbox");
							
	//checkButtons($(this));
						
	//if($("#video-container").is(":visible")){
	//	$('#video-container').slideUp('fast', function(){
	//		$('.second-box').slideDown('fast');
	//	});
	//}
					
	jQuery("#dialog-window").dialog('open');
							
	// var op = "yes"; // uncomment to prevent the lightbox from opening more than once.
							
	return false;
	}
	}
	}
	);
	*/

});

function checkButtons(me, type)
{
	if (type == 'simple')
	{
		if (jQuery("#agreebuttons").length)
		{
			jQuery("#agreebuttons").remove();
		}
	}
	else
	{
		if (me.hasClass('btn-buy-now'))
		{
			if (!jQuery("#agreebuttons").length)
			{
				var html = '<div class="agreebuttons" id="agreebuttons"><input name="agree-button" id="agree-button" value="I Agree" id="" type="submit"><input name="cancel-button" value="I Do Not Agree" id="cancel-button" type="button"></div>';
				jQuery(".ui-dialog").append(html);
				jQuery("#cancel-button").click(function(){
					jQuery("#dialog-frame").attr('src', '/vehicle-inspections/Thanks.aspx');								   
				});
				jQuery("#agree-button").click(function ()
				{
					var queryString = window.location.search.substring(1);
					//var Ebayhtml = '<a href="/vehicle-inspections/Ebay LightBox.aspx?client=ebay" class="btn-buy-now open-dialog" title="Buy Now" />';

					var client = queryString.split("=");
					var ebay = client[1];
					var getitem = queryString.split("&");
					var i = 0;
					var countquerystring;
					
					// loop for client=ebay query string
					for (countQuerystring = 0; countQuerystring < client.length; countQuerystring++)
					{
						if (client[i] == "ebay")
						{
							ebay = client[i];
						}
						else
						{
							if (client.length > 1)
								if (client[1].indexOf("&") != -1)
								{
									var querystrings = ebay.split("&");

									for (i = 0; i < querystrings.length; i++)
									{
										//				                alert(querystrings[i]);
										if (querystrings[i] == "ebay")
											ebay = querystrings[0];
									}
								}
						}
					}

					// loop for item number in query string
					//				    for (i = 0; i < getitem.length; i++) {
					//				        if (getitem[i].indexOf("id") != -1) {
					//				            //  alert(getitem[i]);
					//				            itemid = getitem[i].split("=")[1];
					//				            //alert(itemid);
					//				        }

					//			    }
					getitemid();

					var Ebayhtml = '<a href="/vehicle-inspections/Ebay LightBox.aspx?client=ebay&id="' + itemid + 'class="btn-buy-now open-dialog" title="Buy Now" />';
					if (ebay == "ebay")
					{
						// window.location = "/vehicle-inspections/Ebay LightBox.aspx?client=ebay";

						typeof (document.getElementById("agreebuttons")).setAttribute("href", "/vehicle-inspections/Ebay LightBox.aspx?client=ebay&id=" + itemid);

						typeof (document.getElementById("agreebuttons")).setAttribute("class", "btn-buy-now open-dialog");
						// alert("hello");
						var objDlg = document.getElementById("dialog-frame");
						//alert(objDlg.innerHTML)

						objDlg.src = "/vehicle-inspections/Ebay LightBox.aspx?client=ebay&id=" + itemid;
						var dvbutton = document.getElementById("agreebuttons");
						// alert(dvbutton)
						dvbutton.style.display = 'none';
						var dvArray = document.getElementsByTagName("div");
						for (var j = 0; j < dvArray.length; j++)
						{
							// alert(dvArray[j].className)
							if (dvArray[j].className == "ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix")
							{

								dvArray[j].style.display = 'none';

							}
						}
						for (j = 0; j < dvArray.length; j++)
						{
							// alert(dvArray[j].className)
							if (dvArray[j].className == "ui-dialog ui-widget ui-widget-content ui-corner-all  ui-draggable ui-resizable")
							{
								dvArray[j].style.width = "400px";
								dvArray[j].style.height = "200px";
								dvArray[j].style.top = "350px";
								dvArray[j].style.left = "500px";
							}
						}
					}
					else if (client[1] == "cars")
						window.location = "SellerInfoCar.aspx?client=cars";
					else if (client[1] == "wegolook")
					{
						window.location = "SellerInfoCar.aspx?client=wegolook";
					}
					else if (client[1] == "kbb")
					{
						window.location = "SellerInfoCar.aspx?client=kbb";
					}
					else
						window.location = "comprehensive-inspection.aspx";
				});
			}
		}else{
			if(jQuery("#agreebuttons").length){
				jQuery("#agreebuttons").remove();
			}
		}
	}
}


jQuery("#btnYes").click(function ()
{

	getitemid();

	if (itemid != 0)
		window.parent.location.href = '/SellerInfoAll/?client=ebay&id=' + itemid;
	else
		window.parent.location.href = '/SellerInfoAll/?client=ebay';

});
jQuery("#btnNo").click(function() {
//window.parent.location.href = '/paypal.aspx';
//var form1 = document.getElementById("mainform");
//form1.action = "https://www.paypal.com/cgi-bin/webscr";
//form1.submit();
getitemid();
if (itemid != 0)
    window.parent.location.href = '/payment-option.aspx?client=ebay&id=' + itemid;
else
    window.parent.location.href = '/payment-option.aspx?client=ebay';
});

function setTitle(title){
	jQuery('.ui-dialog-title').html(title);
	checkButtons('', 'simple');
}

jQuery.fn.print = function(){
	// NOTE: We are trimming the jQuery collection down to the
	// first element in the collection.
	if (this.size() > 1){
		this.eq( 0 ).print();
		return;
	} else if (!this.size()){
		return;
	}
 
	// ASSERT: At this point, we know that the current jQuery
	// collection (as defined by THIS), contains only one
	// printable element.
 
	// Create a random name for the print frame.
	var strFrameName = ("printer-" + (new Date()).getTime());
 
	// Create an iFrame with the new name.
	var jFrame = jQuery( "<iframe name='" + strFrameName + "'>" );
 
	// Hide the frame (sort of) and attach to the body.
	jFrame
		.css( "width", "1px" )
		.css( "height", "1px" )
		.css( "position", "absolute" )
		.css( "left", "-9999px" )
		.appendTo( jQuery( "body:first" ) )
	;
 
	// Get a FRAMES reference to the new frame.
	var objFrame = window.frames[ strFrameName ];
 
	// Get a reference to the DOM in the new frame.
	var objDoc = objFrame.document;
 
	// Grab all the style tags and copy to the new
	// document so that we capture look and feel of
	// the current document.
 
	// Create a temp document DIV to hold the style tags.
	// This is the only way I could find to get the style
	// tags into IE.
	var jStyleDiv = jQuery( "<div>" ).append(
		jQuery( "style" ).clone()
		);
 
	// Write the HTML for the document. In this, we will
	// write out the HTML of the current element.
	objDoc.open();
	objDoc.write( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" );
	objDoc.write( "<html>" );
	objDoc.write( "<body>" );
	objDoc.write( "<head>" );
	objDoc.write( "<title>" );
	objDoc.write( document.title );
	objDoc.write( "</title>" );
	objDoc.write( jStyleDiv.html() );
	objDoc.write( "</head>" );
	objDoc.write( this.html() );
	objDoc.write( "</body>" );
	objDoc.write( "</html>" );
	objDoc.close();
 
	// Print the document.
	objFrame.focus();
	objFrame.print();
 
	// Have the frame remove itself in about a minute so that
	// we don't build up too many of these frames.
	setTimeout(
		function(){
			jFrame.remove();
		},
		(60 * 1000)
		);
}

//  get Item Id
function getitemid() {
    var queryString = window.location.search.substring(1);   
    var getitem = queryString.split("&");
    var i = 0;
    // loop for item number in query string
    for (i = 0; i < getitem.length; i++) {
        if (getitem[i].indexOf("id") != -1 || getitem[i].indexOf("Id") != -1) {                  
            itemid = getitem[i].split("=")[1];           
        }
    }

}

//loop through the query string variables and return the client
function getClient()
{

	var returnvalue = "";
	var qsarr = new Array();
	var qs = location.search.substring(1);
	var singleqs = new Array();
	var str = "";
	qsarr = qs.split('&');


	for (i = 0; i < qsarr.length; i++)
	{
		singleqs = qsarr[i].split('=');
		if (singleqs[0].toLowerCase() == "client")
		{
			returnvalue = singleqs[1].toLowerCase();
		}
	}

	return returnvalue;
}
