function ajaxUpdatePriceBulk(clipIds, price){
	$('#clip_price_bulk_span').hide();
	$('#clip_price_bulk_span_loading').show();	
	jQuery.ajax({
	    type: "GET",
	    url: "/ajaxproducer/updatepricebulk/",
	    dataType:"html",
	    data:{"clip_ids":clipIds, "price":price},
	    success:function(response){
	    	if(response == 'force_login'){
	    		document.location.href = '/login/?target=/mysearch';
	    	}else if(response !=''){
	    		alert(response);
	    	}
	    },
	    error:function (xhr, ajaxOptions, thrownError){
	    	//alert("Price can not be changed, please try again!");
	    }    
	});	
	setTimeout("document.location.href = document.location.href", 700) 		
}
function ajaxUpdatePriceDelay(clipId){
	$('#clip_price_loading_'+clipId).hide();
	$('#clip_price_span_'+clipId).show();	
}
function ajaxUpdatePrice(clipId, price, oldPrice){
	$('#clip_price_span_'+clipId).hide();
	$('#clip_price_loading_'+clipId).show();	
	jQuery.ajax({
	    type: "GET",
	    url: "/ajaxproducer/updateprice/",
	    dataType:"html",
	    data:{"clip_id":clipId, "price":price},
	    success:function(response){
	    	if(response == 'force_login'){
	    		document.location.href = '/login/?target=/mysearch';
	    	}else if(response !=''){
	    		alert(response);
		    	$('#clip_price_'+clipId).val(oldPrice);
	    	}
	    },
	    error:function (xhr, ajaxOptions, thrownError){
	    	alert("Price can not be changed, please try again!");
	    	$('#clip_price_'+clipId).val(oldPrice);	
	    }    
	});	
	setTimeout("ajaxUpdatePriceDelay("+clipId+")", 500);		
}
function ajaxAddHappenings(head, desc, data){
	jQuery.ajax({
	    type: "GET",
	    url: "/ajaxhappenings/index/?head="+head+"&desc="+desc+"&data="+data,
	    dataType:"html",
	    data:{},
	    success:function(response){		   
	    },
	    error:function (xhr, ajaxOptions, thrownError){}    
	});
}
/* addtocart ajax function call */
function ajaxReloadCart(){
	if(document.location.href.search(/\/cart/i) > 0 || document.location.href.search(/\/clipbin/i) > 0){
		document.location.href = document.location.href;
		return true;
	}
	return false;
}
function ajaxReloadClipbin(){
	if(document.location.href.search(/\/clipbin/i) > 0){
		document.location.href = document.location.href;
		return true;
	}
	return false;
}
function ajaxAddTocart(clipId, price, button, searchPhrase) {
	
	if(!searchPhrase) searchPhrase = '';
	
	$.getJSON(
			'/ajaxcart/add/',
			{clip_id : clipId, price : price, search_phrase: searchPhrase},
			function(json) {				
				try {pageTracker._trackPageview("/addtocart/" + clipId);} 
				catch (err) {}	
				
				if(ajaxReloadCart())
					return false;
				
				if ($('#add' + clipId)) {
					if(button != false){
						$('#add' + clipId).html("<a class='addToCart' style='cursor:pointer;background:none;' onclick=\"javascript:ajaxRemoveFromcart("
								+ clipId
								+ ",'"
								+ price				
								+ "', '"+button+"');\">" 
								+"<img src='/images/remove-from-cart.gif' width='424' height='55' alt='Remove from cart' />"
								+"</a>");
					}else{
						$('#add' + clipId).html("<a class='removeFromCart' style='cursor:pointer' onclick=\"javascript:ajaxRemoveFromcart("
									+ clipId
									+ ",'"
									+ price				
									+ "', false);\">&nbsp;</a>");
						$('#add' + clipId+'_clipbin').html("<a class='removeFromCart' style='cursor:pointer' onclick=\"javascript:ajaxRemoveFromcart("
								+ clipId
								+ ",'"
								+ price				
								+ "', false);\" title=\"Remove from cart ("+price+"&nbsp;&euro;)\">&nbsp;</a>");
					}
				}				
				if ($('#numclips') && $('#incart') && $('#buy_button')) {
					$('#numclips').html(json.clipCount);
					$('#incart').attr("class", "incart_visible");
					$('#incart').attr("className", "incart_visible");
					$('#buy_button').attr("class", "buy_button_added");
					$('#buy_button').attr("className", "buy_button_added");
					$('#buy_button').attr('href', '/cart');
					$('#buy_button').attr('onclick', '');
				}
				showMiniCart(json);	
				expandClipBinPanelForce();				
				ajaxAddHappenings("Cart_Add", clipId, "");
			});	
	return false;
}
function ajaxRemoveFromcart(clipId, price, button) {
	$.getJSON(
			'/ajaxcart/remove/',
			{clip_id : clipId, price : price},
			function(json) {		
				
				if(ajaxReloadCart())
					return false;
				
				if ($('#add' + clipId)) {
					if(button == 'sd'){
						$('#add' + clipId).html("<a class='addToCart' style='cursor:pointer;background:none;' onclick=\"javascript:ajaxAddTocart("
								+ clipId
								+ ",'"
								+ price				
								+ "', 'sd');\">" 
								+"<img src='/images/add-SD-format-to-cart.gif' width='424' height='55' alt='Add to shopping cart' alt='' />"
								+"</a>");
					}else if(button == 'hd'){
						$('#add' + clipId).html("<a class='addToCart' style='cursor:pointer;background:none;' onclick=\"javascript:ajaxAddTocart("
								+ clipId
								+ ",'"
								+ price				
								+ "', 'hd');\">" 
								+"<img src='/images/add-HD-format-to-cart.gif' width='424' height='55' alt='Add to shopping cart' alt='' />"
								+"</a>");
					}else{
						$('#add' + clipId).html("<a class='addToCart' style='cursor:pointer' onclick=\"javascript:ajaxAddTocart("
									+ clipId
									+ ",'"
									+ price				
									+ "', false);\">" 
									+ "&nbsp;"
									+"</a>");
						$('#add' + clipId+'_clipbin').html("<a class='addToCart' style='cursor:pointer' onclick=\"javascript:ajaxAddTocart("
									+ clipId
									+ ",'"
									+ price				
									+ "', false);\" title=\""+price+"&nbsp;"+json.currency+"&nbsp;Add&nbsp;to&nbsp;cart\">&nbsp;</a>");
					}
				}
				if ($('#numclips') && $('#incart') && $('#buy_button')) {
					$('#numclips').html(json.clipCount);
					$('#incart').attr("class", "incart_hidden");
					$('#incart').attr("className", "incart_hidden");
					$('#buy_button').attr("class", "buy_button");
					$('#buy_button').attr("className", "buy_button");
					$('#buy_button').attr('href', 'javascript:ajaxAddTocart('+clipId+', "'+price+'");');
				}
				showMiniCart(json);
				
				ajaxAddHappenings("Cart_Remove", clipId, "");
			});	
	return false;
}
function ajaxloadCart(){	
	jQuery.ajax( {
		type : "GET",
		url : '/ajaxcart/load/',
		dataType : "html",
		success : function(response) {		
			if(document.getElementById('cart_display')){
				$("#cart_display").html(response);
				var x = document.getElementById('cart_display').getElementsByTagName("script");   
				for(var i=0;i<x.length;i++)  
				{  
				     eval(x[i].text);  
				}
			}
		},
		error : function(xhr, ajaxOptions, thrownError) {
			/* $('#resultdisp').html('Unable load cart ' + thrownError);*/
		}
	});
}
function showMiniCart(json){	
	if ($('#commercial') ) {
		if(json.clipCount > 0){
			var totalOut = json.totalPrice+ " &nbsp;"+ json.currency;
			if(json.grandTotal != json.totalPrice){
				totalOut = '<strike style="color:red;">'+totalOut+'</strike>';
				totalOut = totalOut+" &nbsp;"+json.grandTotal+ " &nbsp;"+ json.currency;
			}
			/*
			$('#commercial').html("<div id=\"shoppingCart\"><h4><a href="
							+ "/cart>Shopping&nbsp;cart</a></h4><div class=\"content\"><div id=\"shoppingdesc\">You have <strong>"
							+ json.clipCount
							+ "</strong> clips in your shopping cart</div><div id=\"total\">Total: "
							+ totalOut
							+ "</div><div style=\"float:left;\"><a href="
							+ "/cart>Proceed&nbsp;to&nbsp;checkout</a></div>"
							+ "<div class=\"clearBoth\"></div></div></div>");
			*/
			$('#ClipBinPanel_Cart_Summary').html('<img src="/images/spacer.png" width="1" height="1" class="shopping_cart_sprite" border="0"/> Cart&nbsp;&nbsp;('+json.clipCount+' clips - '+json.grandTotal+ "&nbsp;"+ json.currency+')');
		}else{
			//$('#commercial').html('');
			$('#ClipBinPanel_Cart_Summary').html('<img src="/images/spacer.png" width="1" height="1" class="shopping_cart_sprite" border="0"/> Cart&nbsp;&nbsp;(0 clips)');
		}
	}
	ajaxloadCart();
}
function ajaxAddToClipbin(userId, clipId) {
	jQuery.ajax( {
		type : "GET",
		url : '/ajaxclipbin/add/',
		dataType : "html",
		data : {clip_id : clipId},
		success : function(response) {
			
			if(ajaxReloadClipbin())
				return false;
			
			if(response){
				if($("#details_clipbinlist" + clipId)){
					$("#details_clipbinlist" + clipId).html("<a class=\"left none\" title=\"Remove from clipbin\" style=\"color: #DCDCDC; font-size: 13px;\""
												+" href=\"javascript:void(0);\" onclick=\"ajaxRemoveFromClipbin("
												+ "'"+userId+"'"
												+", "
												+ clipId
												+ "); return false;\"><img src='/images/spacer.png' class='clipbin_remove_sprite' width='1' height='1' border='0' alt='Remove from clipbin'/></a>");
				}
				if($("#clipbinlist" + clipId)){
					$("#clipbinlist" + clipId).html("<a class=\"removeFromCanvas\" href=\"javascript:void(0);\" onclick=\"ajaxRemoveFromClipbin("
														+ "'"+userId+"'"
														+", "
														+ clipId
														+ "); return false;\">&nbsp;</a>");
				}				
			}
			
			ajaxloadClipbinById('unsaved');		
			expandClipBinPanelForce();
			
			ajaxAddHappenings("Clipbin_Add", clipId, "");
		},
		error : function(xhr, ajaxOptions, thrownError) {
			/* $('#resultdisp').html('Unable to add clips to Clipbin ' + thrownError);*/
		}
	});
}
function ajaxRemoveFromClipbin(userId, clipId) {
	jQuery.ajax( {
		type : "GET",
		url : '/ajaxclipbin/remove/',
		dataType : "html",
		data : {clip_id : clipId},
		success : function(response) {
			
			if(ajaxReloadClipbin())
				return false;
			
			if(response){
				if($("#details_clipbinlist" + clipId)){
					$("#details_clipbinlist" + clipId).html("<a class=\"left none\" title=\"Add to clipbin\" style=\"color: #DCDCDC; font-size: 13px;\"" 
													+" href=\"javascript:void(0);\" onclick=\"ajaxAddToClipbin("
													+ "'"+userId+"'"
													+", "
													+ clipId
													+ "); return false;\">"
													+ "<img src=\"/images/spacer.png\" class=\"clipbin_add_sprite\" onmouseover=\"this.className='clipbin_add_hover_sprite';\" onmouseout=\"this.className='clipbin_add_sprite';\" width=\"1\" height=\"1\" border=\"0\" alt=\"Add to clipbin\"/>"
													+ "</a>");
				}
				if($("#clipbinlist" + clipId)){
					$("#clipbinlist" + clipId).html("<a class=\"addToCanvas\" href=\"javascript:void(0);\" onclick=\"ajaxAddToClipbin("
														+ "'"+userId+"'"
														+", "
														+ clipId
														+ "); return false;\">&nbsp;</a>");
				}				
			}			
			
			ajaxloadClipbinById('unsaved');	
			ajaxAddHappenings("Clipbin_Remove", clipId, "");
		},
		error : function(xhr, ajaxOptions, thrownError) {
			/* $('#resultdisp').html('Unable to remove clip from clip bin' + thrownError); */
		}
	});
}
function ajaxloadClipbin(){
	ajaxloadClipbinById(false);
}
function ajaxloadClipbinById(clipbin_id){	
	var url_ = '/ajaxclipbin/load/';
	if(clipbin_id){
		url_ = url_+'?id='+clipbin_id;
	}
	jQuery.ajax( {
		type : "GET",
		url : url_,
		dataType : "html",
		success : function(response) {
			if($("#clipbin_display")){			
				if(response == ""){
					if(document.location.href.search(/\/video/i) > 0)
						$("#clipbin_display").html('<div style="padding:5px;padding-bottom:10px;">Clipbin is Empty...</div>');
					else
						$("#clipbin_display").html('<div style="padding:5px;padding-bottom:10px;">Clipbin is Empty...</div><input type="button" class="button" value="Search clips &gt;&gt;" onclick="document.location.href=\'/video\';" /><div style="height:15px;overflow:hidden;"></div>');
					$('#clipbin_manage_button').html('');
				}else{
				
					$("#clipbin_display").html(response);
					var x = document.getElementById('clipbin_display').getElementsByTagName("script");   
					for(var i=0;i<x.length;i++)  
					{  
					     eval(x[i].text);  
					}
				}
			}					
			ajaxloadClipbinCountById(clipbin_id);
		},
		error : function(xhr, ajaxOptions, thrownError) {
			/* $('#resultdisp').html('Unable load clipbin ' + thrownError); */
		}
	});
}
function ajaxloadClipbinCountById(clipbin_id){
	var url_ = '/ajaxclipbin/loadcount/';
	if(clipbin_id)
		url_ = url_+'?id='+clipbin_id;	
	jQuery.ajax( {
		type : "GET",
		url : url_,
		dataType : "html",
		success : function(response) {
			if($("#clipbin_count")){
				
				if(response == ""){
					$('#clipbin_count').html('(0 clips)');
				}else{				
					var reminderTxt = '';
					if(response>0 && (clipbin_id == 'unsaved' || !clipbin_id))
						reminderTxt = '<a href="/clipbin/add/clipBin/unsaved/" class="yellow" style="font-size:10px;">Remember to save your clipbin for next time!</a>';
					$("#clipbin_count").html('('+response+' clips) '+reminderTxt);						
				}
			}
		},
		error : function(xhr, ajaxOptions, thrownError) {
			/* $('#resultdisp').html('Unable load clipbin count' + thrownError); */
		}
	});
}
function submitexistSearch(urlval, controlval) {
	var activestatus = document.getElementById("activestatus").value;
	var q = document.getElementById("q").value;
	q = q.replace(/\s\s+/, ' ');
	// q = q.replace(/<\/?[^>]+(>|$)/g, "");
	// q = q.replace("\"", "");
	document.getElementById("q").value = q;

	if (controlval == null)
		controlval = 'search';
	if (activestatus == 'search' || activestatus == 'mysearch') {

		if (document.getElementById("calldata")) {
			var callfromparam = document.getElementById("calldata").value;

			var paramarray = Array();
			paramarray = callfromparam.split('&');
			var str = "";
			for (i = 0; i < paramarray.length; i++) {

				if (paramarray[i].search(/q=/) == -1
						&& paramarray[i].search(/page=/) == -1) {
					if (paramarray[i].search(/noresult=/) == -1) {
						if (paramarray[i] != '=') {
							if (str == '?' || str == '') {
								str = str + paramarray[i];
							} else {
								str = str + '&' + paramarray[i];
							}
						}
					}
				}

			}

			if (str != '') {
				document.frmnewsearch.action = urlval + "/" + controlval
						+ "?q=" + encodeURIComponent(q) + "&" + str;
			} else {
				document.frmnewsearch.action = urlval + "/" + controlval
						+ "?q=" + encodeURIComponent(q);
			}

			location.href = document.frmnewsearch.action;
		}
	} else {
		document.frmnewsearch.action = urlval + "/" + controlval + "?q="
				+ encodeURIComponent(q);
		location.href = document.frmnewsearch.action;
	}

}
/* function for tags show/hide option */
function showHide(shID) {
	if (document.getElementById(shID)) {
		if (document.getElementById(shID + '-show').style.display != 'none') {
			document.getElementById(shID + '-show').style.display = 'none';
			document.getElementById(shID).style.display = 'block';
			document.getElementById(shID + '-hide').style.display = 'block';
			if (document.getElementById(shID + '-show1').style.display != 'none') {
				document.getElementById(shID + '-show1').style.display = 'none';
			} else {
				document.getElementById(shID + '-show1').style.display = 'block';
			}
		} else {
			document.getElementById(shID + '-show').style.display = 'inline';
			document.getElementById(shID + '-hide').style.display = 'none';
			document.getElementById(shID).style.display = 'none';
			if (document.getElementById(shID + '-show1').style.display != 'none') {
				document.getElementById(shID + '-show1').style.display = 'none';
			} else {
				document.getElementById(shID + '-show1').style.display = 'inline';
			}
		}
	}
}
var clipbin_panel_closed = false;
function expandClipBinPanel(forceExpand){
	if(forceExpand && $("#ClipBinPanel").height() > 30)
		return false;
	
	if($("#ClipBinPanel #inside").height() > 38){
		 $("#ClipBinPanel #inside,#body_margin").animate({ 
		    height: "0px"
		  	}, 750,
		  	function() {
		  		$("body_margin").css('height', '0px');
		  		$("#ClipBinPanel_Header #maximize_div").html('<img src="/images/spacer.png" class="open_panel_sprite" id="maximize" title="open" width="1" height="1" border="0"/>');
		  		
		  		jQuery.ajax( {
		  			type : "GET",
		  			url : '/ajaxclipbin/expand/',
		  			dataType : "html",
		  			success : function(response) {			  				
		  			},
		  			error : function(xhr, ajaxOptions, thrownError) {
		  			}
		  		});  
		  	 });	
		 clipbin_panel_closed = true;
	}else{
		$("#ClipBinPanel #inside,#body_margin").animate({ 
		    height: "105px"
		  	}, 750,
		  	function() {
		  		$("body_margin").css('height', '105px');
		  		$("#ClipBinPanel_Header #maximize_div").html('<img src="/images/spacer.png" class="close_panel_sprite" id="maximize" title="close" alt="close" width="1" height="1" border="0"/>');
		  		jQuery.ajax( {
		  			type : "GET",
		  			url : '/ajaxclipbin/expand/',
		  			dataType : "html",
		  			data : {expanded : true},
		  			success : function(response) {			  				
		  			},
		  			error : function(xhr, ajaxOptions, thrownError) {
		  			}
		  		});  
		  	 }); 			
	}		
	return false;	
}

