jQuery(document).ready(function($) {
  
  	$('.attr_box').children().find('input').hide();

        //1st May, 2011 - Start
        /*
         * Using search button instead of keyup
         */
		 
        $('.refnumber').after("<input type='button' class='search_ref' name='search_ref' id='search_ref' value='' />");
        $('.refnumber_ele').each(function(){
            title = $(this).attr("title");
            id = "";
            eid = "search_ref";
            if(title.indexOf("refnumber")!=-1){
                id = title.substring("refnumber".length);
                eid = eid + id;
            }
            $(this).after("<input type='button' class='search_ref' name='search_ref' id='"+eid+"' value='' />");
        })

        $(".search_ref").click(function(){
            var el = $(this).prev();
			
            if(el.val()){
                    if(jQuery.url.param("cPath")==10){
                            var field_id = 'montage';
                            $('.montage').remove();
                            el.after("<input type='hidden' class='montage' name='"+el.attr('name')+"' id='"+el.attr('id')+"' value='"+el.val()+"' />");
                    }else{
                            var field_id = el.attr('id');
                    }			//alert(field_id);
                    $('.ref_images').remove();
                    $.ajax({
                            type: "POST",
                            url: "externalfiles/searchimage.php",
                            data: "ref="+el.val()+"&field_id="+field_id,
                            async:false,
                            success: function(msg){
                                    //alert(msg);
                                    if(msg){
                                            el.parent().after('<div id="ref_images" class="ref_images"></div>');
                                            if(msg==0){
                                                    $('.ref_images').css('height','20px');
                                                    $('.ref_images').html("Please enter valid number");
                                            }else{
                                                    $('.ref_images').css('height','330px');
                                                    $('.ref_images').html(msg);
                                            }
                                    }
                            }
                    });
            }
            return false;
        })

	//1st May, 2011 - End
	
	$(".searched_images .draggable").draggable({opacity: 0.2});

	$(".attr_val").droppable({
		drop: function(helper) {
			var id=helper.draggable.attr("id");
			$(this).parent().find("input").val(id);
			$(this).html(helper.draggable.clone()).append('<span class="remove_icon" onclick="remove_attr(this)"><img src="includes/templates/SOUTHAM/images/icons/remove.png" alt="remove" /></span>');
			$(".attr_val").find("img.draggable").css('width','128px');
			$(".attr_val").find("img.draggable").css('height','128px');
		}
		
	});
	
	$("li.menu_list").mouseover(function() {
		$(this).find("ul.submenu").show();
	}).mouseout(function(){
		$(this).find("ul.submenu").hide();
	});
	
	$('#btnfind').click(function() {
		var code = $('#access_code').val();
		if(code!=''){
			$.ajax({
				type: "POST",
				url: "externalfiles/searchimage.php",
				data: "ref="+code,
				async:false,
				success: function(msg){
					$('.presentation_images').html(msg);
				}
			});
		}else{
			$('.presentation_images').html("<div class='noimage'>Please type Accsess code</div>");
		}
	});
	
	$('.frmAddCart').submit(function() {
		if(jQuery.url.param("cPath")==3 || jQuery.url.param("cPath")==4){		// server
			var s = 0;
			$(".attr_box :input[type='text']").each(function(index) {
				if($(this).val()){
					s = 1;
				}
			});
			if(s==0){
				alert("You must select your prints before add to shopping cart");
				return false
			}
		}
		return true
	});
	
	$('ul.top_nav > li:last-child > a').css('background','none');
	
	if(jQuery.url.param("cPath")!=6 && jQuery.url.param("cPath")!=7 && jQuery.url.param("cPath")!=8 && jQuery.url.param("cPath")!=9 && jQuery.url.param("cPath")!=10 && jQuery.url.param("cPath")!=11 && jQuery.url.param("cPath")!=12 && jQuery.url.param("cPath")!=13 && jQuery.url.param("cPath")!=14 && jQuery.url.param("cPath")!=15 && jQuery.url.param("cPath")!=16 && jQuery.url.param("cPath")!=17 && jQuery.url.param("cPath")!=18 && jQuery.url.param("cPath")!=19 && jQuery.url.param("cPath")!=20 && jQuery.url.param("cPath")!=21 && jQuery.url.param("cPath")!=22 && jQuery.url.param("cPath")!=23 && jQuery.url.param("cPath")!=24 && jQuery.url.param("cPath")!=25 && jQuery.url.param("cPath")!=26 && jQuery.url.param("cPath")!=27 && jQuery.url.param("cPath")!=28 && jQuery.url.param("cPath")!=39 ){
		$('.product_info_sugg').append("<p class='instruction'>Please enter your client reference number below. All images corresponding to your reference number will then be displayed. You can drag your preferred images to the boxes below to compose your selection.</p>");
	}
	
	if(jQuery.url.param("cPath")==6 || jQuery.url.param("cPath")==7){		// server
		$('#categoryIcon').remove();
		$('span.imgLink').remove();
	}
	
	if((jQuery.url.param("cPath")==4) || (jQuery.url.param("cPath")==5)){		//server
		//alert($('#categoryIcon').length);
		if($('#categoryIcon').length > 0){
			$('#categoryIcon').after("<div class='package_spec'><p class='first_p'>Photographs are framed in a black frame with a bronze finish panel. The frame includes a black mount with your university crest embossed in gold leaf.</p><p class='second_p'>You should receive your photographs within four weeks of your order being placed. Please allow an extra week if you are ordering canvassed prints.</p> <p class='third_p'> Framed packages CAN NOT be posted overseas.</p></div><br class='clearBoth' />");
		}else{
			$('#contentMainWrapper').children().find('.productGeneral:first').before("<div class='package_spec'><p class='first_p'>Photographs are framed in a black frame with a bronze finish panel. The frame includes a black mount with your university crest embossed in gold leaf.</p><p class='second_p'>You should receive your photographs within four weeks of your order being placed. Please allow an extra week if you are ordering canvassed prints.</p><p class='third_p'>Framed packages CAN NOT be posted overseas.</p></div><br class='clearBoth' />");
		}
	}

        $('.btnGo').click(function() {
                var uniId = $('#university').val();
                var path = jQuery.url.attr("protocol")+"://"+jQuery.url.attr("host")+jQuery.url.attr("path")+"?main_page=product_info&cPath=8&products_id="+uniId;
                if(uniId){
                    location.href = path;
                }
		
	});

        $('.btnMontageGo').click(function() {
                var uniId = $('#university').val();
                var path = jQuery.url.attr("protocol")+"://"+jQuery.url.attr("host")+jQuery.url.attr("path")+"?main_page=product_info&cPath=10&products_id="+uniId;
                if(uniId){
                    location.href = path;
                }

	});
		
		
  $('.btnFramingGo').click(function() {
                var uniId = $('#university').val();
                var path = jQuery.url.attr("protocol")+"://"+jQuery.url.attr("host")+jQuery.url.attr("path")+"?main_page=page&id="+uniId;
                if(uniId){
                    location.href = path;
                }

	});





  $('.btnFramingGos').click(function() {
                
				//var uniId = $('#universityType').val();
				
				var uniId =$("input[@name='radio']:checked").val()
                var path = jQuery.url.attr("protocol")+"://"+jQuery.url.attr("host")+jQuery.url.attr("path")+"?main_page=page&id="+uniId;
                if(uniId){
                    location.href = path;
                }


	  });


 $('.btnFramingGoAc').click(function() {
               // var uniId = $('#universityStyle').checked;
				 var uniId =$("input[@name='radio']:checked").val()
				
				var path = jQuery.url.attr("protocol")+"://"+jQuery.url.attr("host")+jQuery.url.attr("path")+"?main_page=product_info&cPath=41&products_id="+uniId;
                if(uniId){
                    location.href = path;
                }

	  });


 $('.PLbtnFramingGoAc').click(function() {
               // var uniId = $('#universityStyle').checked;
				 var uniId =$("input[@name='radio']:checked").val()
				
				var path = jQuery.url.attr("protocol")+"://"+jQuery.url.attr("host")+jQuery.url.attr("path")+"?main_page=product_info&cPath=42&products_id="+uniId;
                if(uniId){
                    location.href = path;
                }

	  });




});





function remove_attr(el){
	var ex = $(el);
	ex.parent().parent().find("input").val("");
	ex.parent().find("img").remove();
	ex.remove();
}

function select_item(){
	$('#frmfreeitem').submit();	
}
function delete_free_item(){
	$('#frmfreeitemdelete').submit();	
}

function select_montage(el){
	var val = $(el).val();
	var mon_val = $('.montage').val();
	$('.montage').val(mon_val+'-'+val);
}

